The Problem with Time & Timezones - Computerphile
Video Statistics and Information
Channel: Computerphile
Views: 3,654,854
Rating: undefined out of 5
Keywords: computers, computerphile
Id: -5wpm-gesOY
Channel Id: undefined
Length: 10min 12sec (612 seconds)
Published: Mon Dec 30 2013
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.
As a programmer you can just tell from the way he tells the story that he has dealt with this stuff before. You can hear the memories of the suffering in his voice.
I've had to deal with time zones, however not to the extent this guy has. I know (some of) his frustration. Pretty accurate. Even more frustrating is if you are interfacing with another service which gives you a client's local time (timezone offset not given, but adjusted for their timezone) and realize ... fuck.
While he touches upon using Unix Time-stamp and says that it doesn't cover the leap-second cases, it has been my experience that using that value is probably the best choice in 99.999% of all cases.
While anyone that has yet to work with them will feel a slight twang of panic due to this video, it's not THAT bad. Let me explain:
You are usually dealing with 3 cases when handling time:
In most programming languages, the easiest, headache-less approach is taking the Unix Time-stamp, and do a date conversion with a location-based timezone (so Asia/Tokyo or Europe/Amsterdam instead of, say, UTC+2) and you get a value that is "good enough" for 99.999% of cases.
Converting back into Unix Time-stamp works the same way; feed a date-time and a timezone and you can get Unix Time-stamp again. Unix Time-stamp is always timezone independent.
This means that 2005-01-01 05:00 in GMT and 06:00 in GMT+1 result in the same Unix Time-stamp.
Which all comes down to his original point. Don't do it yourself. Trust your programming language's implementation if it exists. If it does not exist, grab a package to handle it. In 99.999% of cases, the above is accurate enough.
Which is how you should do the final case; adding and substracting time. Use a language/package "Date" object and tell that to add/substract days/minutes/seconds of whatever it's been set to. You may thing "Oh, I need to add 2 days? I'll just add (3600 * 24 * 2) to the current Unix Stamp". Except that doesn't work when in those days, daylightsavings happens.
So again, for gods sake, use the standard/opensource packages. Both PHP and Java for example make this so ridiculously easy, you really have no excuse.
He was a good story teller.
What's worse than the code is the fact that governments, not content with the havoc and confusion of zones already, need to mess with their daylight savings/standards as well, which are zones. This means that once they do that, everyone needs to scramble to update the calendar in each of their systems, which dictates on which second the local zone will flip from one zone to another. So if you own a computer, you're at the mercy of your govt's political whim, then whoever provides your OS to give or sell you a new version of that table. Good luck with that.
He should do one about character encodings.
Jesus those are the bane of my existence.
An open letter to all aspiring programmers: Please store all time values as UTC/GMT or Unix time stamps or equivalent. Do not store them as localized time objects. Do not do math on them as localized time objects. If you display time to the user or receive a time from the user, only then should you convert it to something else using a premade library and whatever their OS spits at you. Convert it before anything else happens, and especially before it gets sent over a network of some kind.
How do all of these people keep getting his phone number?
So having recently read up on how bitcoins work, I've got a semi-related question. In bitcoin verification, it's incredibly important that transactions can be ordered chronologically. Having just touched on how hard it is to keep clocks in sync across the world, what would happen if verifying computers were out of sync?