Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I need to add a certain feature to a module in a given project regarding time calculation. For this specific case I'm using Java and reading through the documentation of the Date class I found out the time is calculated in milliseconds starting from January 1, 1970, 00:00:00 GMT. I think it's safe to assume there is a similar "starting date" in other languages so I guess the specific implementation in Java doesn't matter.

How is the time calculation performed by the computer? How does it know exactly how many milliseconds have passed from that given "starting date and time" to the current date and time?

share|improve this question
2  
The 'similar "starting date"' is called the UNIX epoch. –  Martijn Pieters Oct 30 '12 at 0:09
    
en.wikipedia.org/wiki/Epoch_%28reference_date%29#Computing Start times for various systems. –  stonemetal Oct 30 '12 at 12:45

3 Answers 3

up vote 10 down vote accepted

I think it's safe to assume there is a similar "starting date" in other languages so I guess the specific implementation in Java doesn't matter.

Pretty much all computers use this or a similar variation of Unix time.

How is the time calculation performed by the computer? How does it know exactly how many milliseconds have passed from that given "starting date and time" to the current date and time?

Typically, a computer includes one or more hardware devices that count time, usually based on a crystal oscillator (the same as used in most modern clocks). There are serveral variations/standards for such devices:

These timer devices usually have a separate battery that allows them to continue running when the computer is switched off. The computer interacts with them in two ways:

  • When it boots up, it reads the current time from the device
  • At regular intervals, the device causes a hardware interrupt that is handled by the OS to do any time-dependant tasks.

However, the crystal oscillators used in the hardware timers have limited precision and over time drift away from the correct time. For applications where it's important to have an accurate clock (or just to avoid the hassle of having to manually adjust it), computers regularly synchronize their time via the Network Time Protocol with a time server, which is connected (directly or indirectly) to a high-precision atomic clock.

share|improve this answer
    
Thanks for the links to the core concepts, now I have a better understanding of the overall process, it's a very interesting subject. –  Jorge Mendoza Oct 30 '12 at 15:37

There are 60 seconds in a minute, 60 minutes in an hour, and 24 hours in a day. That's 86400 seconds in a day. Multiply that by 1000, and you've got the number of milliseconds in a day.

You should now be able to work out the number of days from the beginning of time (January 1, 1970, 00:00:00 GMT). Multiply that by the number of milliseconds in a day, and you now have the internal number that the computer clock stores to represent the current date/time.

share|improve this answer
    
Except for leap seconds. You're 25 seconds off. –  MSalters Oct 30 '12 at 16:11
1  
@MSalters: My answer was intended to be a simple illustration, and doesn't include intricacies like the time zone change in Peking, China in 1927. –  Robert Harvey Oct 30 '12 at 16:13
    
That weirdness happened in another timezone and before 1970-01-01Z. I think leap seconds are the only ones that would apply specifically here, in this epoch. –  MSalters Oct 30 '12 at 16:32
    
@MSalters Yes and no. POSIX time shoves leap seconds under the carpet and pretends that all days have exactly 86400 seconds while keeping synchronized with UTC. The gory details make up most of the Wikipedia article. –  Gilles Oct 30 '12 at 17:12

The BIOS stores the time for you on the system clock. The OS provides a system function to retrieve the time in their format (Unix systems, match the Java framework, Windows uses a different "Epoch" --the start date-- but the Java Runtime Translates the result to match what it expects.

share|improve this answer
    
-1 : "Unix systems, match the Java framework" - where does that idea come from? –  mattnz Oct 30 '12 at 3:35
    
What I mean is that Unix systems use the same epoch as the java framework thus no translation necessary. If you want to get caught up in semantics then more power to you. –  Mike Brown Oct 30 '12 at 7:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.