System time

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
Gumby

System time

Post by Gumby »

How do I get the current time in PM? Can I use the standard-headers in DJGPP or can I read it from a port or how does it work?
User avatar
Pype.Clicker
Member
Member
Posts: 5964
Joined: Wed Oct 18, 2006 2:31 am
Location: In a galaxy, far, far away
Contact:

Re:System time

Post by Pype.Clicker »

if you're not in the DjGpp-provided environment (e.g. using DPMI over DOS, etc), you shouldn't use any of DjGPP's headers, for your kernel doesn't have the library behind those headers.

If you want to get the "time of the day", you can read it out of the CMOS Real Time Clock through ports 0x70 & 0x71 (iirc. Check HelpPc or OSRC to get sure)
User avatar
Brendan
Member
Member
Posts: 8561
Joined: Sat Jan 15, 2005 12:00 am
Location: At his keyboard!
Contact:

Re:System time

Post by Brendan »

Hi,

There's a "chip" called the RTC (Real Time Clock) that keeps track of the time. It's accessed via the CMOS.

The problem is that there isn't an easy way of determining what the RTC keeps track of. In MS OS's the RTC is expected to be local time (what you'd see on your alarm clock). There are a few problems with this.

First, if the OS supports networking and timestamps this can cause problems because the timestamps on one computer can be different to the timestamps on another (e.g. if the computers are in different time zones). This can cause obvious problems with backup software and makefiles, and not so obvious problems with anything else.

The second problem is when the computer has more than 1 OS installed, because of daylight savings. If the OS's use local time, and any of the OS's automatically adjust for daylight savings then the RTC will become messed up for the other OS's. This is a problem with MS's OS's - if you dual boot between windows95 and windowsXP you can expect your time to be messed up twice a year. By default they will BOTH add (or take) an hour at the appropriate times.

Therefore if you want to avoid these problems your OS needs to use UTC (Universal Co-ordinated Time, formerly called Greenwich Mean Time), as does Unix OS's. It's the only solution that works properly (MS OS's don't work properly :) ). This means either expecting the RTC to be set to UTC always (as my OS does), or converting the time into UTC if the user tells the OS that it's not set to UTC during installation (as does Linux). If your OS expects the RTC to be set to UTC then other OS's that use local time will be messed up (but IMHO these OS's are broken anyway). If your OS goes the Linux way you'll have problems when other OS's change the time because of daylight savings - there's no way of avoiding it in this case.

Now, if your OS does use UTC then you need a way of converting UTC into local time. This involves adjusting UTC to into local standard time according to which time zone it is in, and then determining if local standard time needs to be adjusted for daylights savings. You could ask the user for enough information to do this, or you can create a database so that the user only needs to tell the OS where in the world they are when the OS is installed. Linux/Unix uses a time zone database for this (my OS has a database containing all regional information instead).

Of course you can avoid all these hassles by reverting to the way DOS handled it - ask the user what the current time is when the OS boots ;)

Now for the easy part - I've attached code that reads the current time (whatever it is) from the RTC :). You may want to read through some RTC programming documentation before reading the code (Ralf Brown's CMOS list will do - it's distributed with his interrupt list).


Cheers,

Brendan
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
Post Reply