How computers represent time
Marcin Polkowski
Translated from Polish. Originally published on morony.pl on 2 Nov 2016.
Time is one of the basic "parameters" of the world, something we record (remember) and convert many times every day. These days that means it's processed by computers. You might think it's a simple matter, but a whole host of problems shows up right away. The idea for this post came from the pointless switch to winter time last Sunday.
First, time zones. As long as we stay within a single time zone, everything is fine – we can easily calculate the time difference between two dates. Well, up to a point: after all, in Poland we have two time zones – summer and winter. If we care about calculating time precisely, we have to take that into account. One solution is to store and compute dates and times in UTC (Universal Time Coordinated). Information about which time zone applies at a given moment and place is best taken from the operating system, which should have it "under control". It's not worth figuring this out yourself, because the number of exceptions is staggering. I recommend the video The Problem with Time & Timezones.
I still remember a geography quiz in high school where we calculated the flight time from San Francisco to Tokyo (and back) based on the departure and arrival times. The only way guaranteed to give the right answer was to convert to UTC.
Second, the data format. Computers work with numbers, people work with strings of characters. There are many formats for writing a date and time as a string, but that's just a representation "for humans". In memory, date and time can be stored in many ways, of which I think three are the most important:
- We can store the year, month, day, hour, minute, second and millisecond (or nanosecond, etc.) separately as integers, plus a time zone code (stored, e.g., as the number of minutes relative to UTC). This approach is precise and universal, but inefficient and memory-hungry. Does anyone still remember the Y2K problem?
- The second way is to store Unix epoch time, i.e. the number of seconds since 01-01-1970. There are two problems here: with int32 we get a very limited range of dates and a precision of one second.
- The third way, which I deal with every day, is storing a floating-point number giving the days since year 0. This is the method Matlab uses. Its big advantage is that it can describe any date in the history and future of the universe and can handle sub-second precision. Its drawback comes from the fact that floating-point precision varies with the value – I wrote about this a few weeks ago.
The third way of storing dates is definitely my favorite – it lets you operate on dates very quickly without losing precision. Floating-point operations are completely routine, so it'll be hard to find anything more efficient.
A lot depends on the programming language; each one takes a different approach. In Python especially you can see a huge amount of complexity. What are your experiences? Which format do you use most often?
As a bonus, I'd like to show you 3 animations I made. The first is fairly predictable, as it shows day length depending on the day of the year:
The next two are much more interesting, because they show sunrise and sunset times in the local time zone.
You can clearly see the switch from summer to winter time (at different times depending on the region) and how poorly the time zone fits in some regions.