I'm a fairly intelligent guy, can follow the plots of Primer, Terminator, and other scifi time travel movies. I have a decent understanding of relativity, spatial reference frames, etc.
But any time I have to deal with slightly more-than-simple time issues in any language, my brain starts more or less falling to pieces. Python and js have good libraries that help, but its still a confounding bugger of an issue.
- Always store times and calculate differences/offsets in UTC.
- Never attempt to convert a local time to UTC (it is not a one-to-one mapping!).
- Depending on your application, convert any dates to a given timezone at the moment they are displayed.
Now Javascript throws a monkey-wrench into the whole affair, because it assumes Dates should be handled in the user's local timezone, which, if you're dealing with timezone issues, is certainly not what you want. So attempting to convert a Date object from one timezone to the next is surely impossible to do without introducing bugs unless you understand the rules for every timezone you're dealing with.
Luckily, a guy named Olson created a database of timezone rules for us mortals to use in our timezone-aware applications. In another stroke of good fortune, some smart people wrote a Javascript library [1] that parses this database for you and can convert your Javascript Date object between any timezone on the planet. So you do all your calculations using this Date object in UTC mode, then convert to the desired timezone at the time they are displayed.
The most i've ever hurt my brain was reasoning about a timezone-aware fleet management system, and what it meant when a vehicle was driven from one timezone to the next while properly billing the time driven. We ended up going with an 80% solution that we know is wrong in some cases, but that we an actually explain to users.
I'm a fairly intelligent guy, can follow the plots of Primer, Terminator, and other scifi time travel movies. I have a decent understanding of relativity, spatial reference frames, etc.
But any time I have to deal with slightly more-than-simple time issues in any language, my brain starts more or less falling to pieces. Python and js have good libraries that help, but its still a confounding bugger of an issue.