7

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?

Thomas Owens
  • 79,623
  • 18
  • 192
  • 283
Jorge Mendoza
  • 73
  • 1
  • 4

3 Answers3

12

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.

Michael Borgwardt
  • 51,037
  • 13
  • 124
  • 176
  • 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
5

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.

Robert Harvey
  • 198,589
  • 55
  • 464
  • 673
  • 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.](http://stackoverflow.com/a/6841479/102937) – 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](http://en.wikipedia.org/wiki/Unix_time). – Gilles 'SO- stop being evil' Oct 30 '12 at 17:12
3

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.

Michael Brown
  • 21,684
  • 3
  • 46
  • 83
  • -1 : "Unix systems, match the Java framework" - where does that idea come from? – mattnz Oct 30 '12 at 03: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. – Michael Brown Oct 30 '12 at 07:38