Tuesday, August 12, 2014

Where does 116444736000000000 come from?

While dealing with time conversions functions of various languages / technologies, e.g. Windows API or Java, you might come across the magic constant 116444736000000000. You can also find it in several code snippets on the web - however it never nearly explained where it comes from... This I wanna change, here and now!

A time value is simply a counter of ticks since an defined start of time, called epoch. Not only the "epoch date" differs between various systems, but also the counter resolution, that is how often it is updated.

Windows uses in most functions the FILETIME structure, which represents the actual time as the number of 100-nanosecond intervals since January 1, 1601 (UTC).

Other platforms like Java/Android or some Unix functions represent a time value as the number of seconds elapsed since 00:00:00 January 1, 1970.

To convert between both worlds, you need an offset factor and that is... 116444736000000000. This value used as a FILETIME value, meaning 100ns intervals since 01-01-1601, represents exactly the data 01-01-1970!

Proof?
We know that 1ms = 1000000 ns = 10000 * 100 ns.
So 116444736000000000*1/100 ns = 11644473600000 ms = 11644473600 s.
Between 01-01-1601 and 01-01-1970, there are exactly 134774 days.
134774 * 24 (hours per day) * 3600 (seconds per hour) = 11644473600 s.