Time matters: when UTC time is just not enough
lazystone.github.io
lazystone.github.io
Look I know TZ's and dates are hard, I work with them quite regularly, I have written code to handle things like recurring items, and I have written a lot of code around knowing what TZ to show a date in to an end user (in my case you have to deal with things happening in a participant's TZ, their supervisor's TZ, our call center's TZ, and whatever the TZ is of the browser the end user is accessing the site from, all of which could be different). A bad UTC time due to mismatched timezone databases is by far the least of my worries, in fact it's not even on my radar nor do I plan to add it to it. You HAVE to standardize, period. UTC makes the most sense (epoch timestamps if you can get away with it) and it's up to the client to format that however they want (moment.js is your friend if you are a web dev). Yes, keep your TZ databases up to date and know which DB your software is using but use UTC. Anything else is a recipe for disaster.
No disasters so far. ;)
Why? On March 13 last year, Chile announced a new daylight savings change, to take place on May 15. [1] Let's say I already had an appointment somewhere for 3:00pm on May 21. When the timezone change was announced, that reservation is still at 3:00pm local. It is not still at the same time in UTC.
utc_time is derived from local_time and timezone, not the other way around.
(I am generally a fire-breathing advocate for UTC everywhere)
[1] http://codeofmatt.com/2016/04/23/on-the-timing-of-time-zone-...
As a former commercial pilot, we use UTC exclusively when flight planning and reporting waypoints - no matter where we are in the world.
When I report that we expect to reach waypoint 'X' at 0451, then anybody listening in ANYWHERE in the world knows exactly how long before I get there, or how long overdue I am. No ambiguity whatsoever.
To this day, in my new life as a developer, I ensure that all my servers are set to UTC and I always use UTC as my time reference datum.
But honestly, when I was first learning to fly, it was a difficult thing to get used to. Like most pilots back then, I got one of those watches with two faces and set one (usually the primary/largest one) to UTC. After getting used to dealing purely in UTC for a while, I was amazed that more people didn't do it.
I stopped looking at time as being defined by the position of the sun, but as a reference datum. If a colleague called out "See you at the pub at oh-two hundred hours", I never thought "2 in the morning??" but rather "Oh, that is 1 hour and 45 minutes from now - cool!".
But even now when we schedule meetings with (IT) colleagues in other countries, I will always reiterate/exchange the meeting times in UTC so that there is no confusion at all when we start the sessions.
As for how you should store times, yes you should always store time as UTC. This is a different problem.
It's specific times you want UTC, if I setup a meeting then I want someone in another time zone to call in at the same time.
Your example may work fine on a server that you control, but let's say your use case is a phone alarm program. You can't guarantee that your application will know when it's 7:30AM in the local time, but you can guarantee what the time is in UTC and not need network or the device's help to do it.
So now the time format you store depends on the architecture of your platform? What if you have to send this data between platforms now? Am I going to have to convert back and forth for each message? No thanks. I'll just store everything as UTC and stay sane.
In a very real example there was once a product which let you schedule events by specifying a start date and a recurrence interval, say "daily". The software converted the start time to UTC in the zone of the schedule creator and stored that. But the conversion was done at the creation time which means it was done with a particular offset (say -5 hours) and when the relationship between the creators time zone and UTC changed as a result of DST changes the schedule ended up one hour of from what the creator intended. And the information about which timezone the thing had originally been scheduled in was lost and could not be retrieved.
This is 2017. Are you not putting all of your transactions in an ordered table in a database with creation timestamps? That information should not be lost and should be easy to figure out, in UTC. Figuring out the conversion of some other time/date is a solved problem.
There should never be a question of when some work happened if you're following sane practices.
I do huge ETL projects that live or die on the accuracy of our timing data. Give me date(time)s in UTC and I'm happy. Anything else? Pain.
If I have a meeting every day at 10am local time with someone in Arizona, their meeting suddenly jumps by an hour when my time changes. (Similarly, if it's my Arizonian colleagues who set the meeting to be at 10am daily, I'm suddenly meeting them at 11am my time once spring hits.)
(Date)Times stored in the schedule (start times, end times, trigger times, clock times) are not instants in UTC, they are time specification in a calendar system which can only be interpreted as specific instants in the context of a particular time zone. Which is stored explicitly and separately from the calendar date-times.
This crucial separation of a time specifications in a calendar system from instants in time is one thing which many date/time libraries get wrong and which Noda-/JodaTime gets right.
You are either in DST or not - that's why we have PST and PDT - one is a daylight time and reflects a different offset from standard.
Most people are in the same timezone but when not, it's the responsibility of the attendee to know that it's shifted - and the calendaring system should know to update it accordingly.
The example (which is not so uncommon) that an appointment is set in timezone X, author of the service storing the appointment helpfully converts to UTC, timezone of the originating country changes and then the "return" from UTC to source timezone is incorrect, because now UTC-0600 is actually UTC-0700.
In short, any conversion that reduces precision is flawed. Dropping timezones after normalizing them to a canonical format makes it impossible to restore the conversion.
Of course it can't happen by magic, and you rely on the authors of the timezone libraries (a thankless task in itself), but if you remove a dimension of your data, nobody can ever help you.
A calendar appointment isn't identified by a UTC instant; it's identified by a predicate.
(1) event scheduled for 7:00pm local time in a certain place on a certain date, that needs to be at that local time regardless of whether the government changes to time zone of that locality, or changes DST rules.
(2) event at 7:00pm Central Time ona certain date, that needs to occur at that time in the regardless of DST date changes.
(3) event at 7:00pm CST, which has a fixed offset from UTC.
But it's only a small step farther to allow scheduling an appointment relative to an arbitrary time zone, instead of the one you happen to currently be in. Which naturally includes UTC, for situations where astronomical time is what you really care about.
Dropping timezones after normalizing them to a canonical
format makes it impossible to restore the conversion.
Except this isn't true.Normalizing TimeZones converts them to ZULU/GMT. You can de-normalize ZULU/GMT to any timezone easily and well within the UTC standard by converting them back to the local timezone. No data is lost, and the standard makes this process transparent.
Actually converting from Zulu to Local should always be done on the client side as their locality settings _should_ include a timezone.
In practice there are edge-cases that you can't account for when the theory meets the practice of the messy, real world.
For example, last year Turkey decided, with a couple of days notice, to delay the daylight savings switch [1]. If you did things in UTC, you would have no idea which of your dates was now wrong, which ones that needed to be fixed.
Storing UTC might be useful in its own right, but if you're looking for correctness, you need to store the details and not discard the extra information.
What time encoding format _can_ deal with changes like this? Are their libraries that update often enough?
Also provided the Host OS had the proper time offset the UTC conversion would work fine, even for cases of fractional-hour timezones. So this can would be covered if the client updated their TZ locality.
Don't let the title throw you off, UTC is fine. Just be aware that when you convert the UTC time that you store back to local time, the conversion might be off depending on where you convert it. If your API is hosted on a server in Amsterdam and your users in Japan, that won't work. If your app is run on a tablet bought a few years ago, it will have an old timezone database. As the article highlights:
> Do date/time transformation on the server side, where you can control it.
> So, you had everything right, but result is wrong. Life is unfair. Deal with it.
It mentions a workaround, but not a solution.
There's no cookie cutter solution, you need to think about your particular problem and do what's best.
So if your event was in 3 PM Eastern Time, it would be written to the database as 3 PM Pacific Time.
Did it result in bugs? Yeah, sure. Should it have been fixed to use UTC? Of course. But it wasn't the end of the world -- people lived with it for years and it worked well enough. (Engineers grumbled about the code, of course.)
Does not solve the problem for local time (stores in one country will be open from moscow to greenwich, in other - samoa to tokyo), but helps to put global time in perspective. E.g. if its berlin, you can guess, that folks in Berlin have around 5 hours more of work time, and folks in Moscow will be done in 4, and Japan is enjoying nice evening already.
Umm, no, as an experience programmer I don't touch UTC until I hit something a human might read at this moment, otherwise I ship unix timestamps. I'm not that insane.
The UI which is currently displayed has the best information available to use the correct timezone, calendar, font, notation format and language to display time.
Not hitting frontend but needs a human-readable timestamp for insane reasons? UTC. All databases are UTC, all servers are UTC.
I'm not gonna touch time unless absolutely necessary and if I'm going to do it, I'm going to do it with a long pole and acid-resistant gloves on.
Seconds since the Epoch is unambiguous, eschews time zones, daylight savings time and other man-made concepts (it makes sense to Martians too). Plus it only requires an INTEGER type in the database which every RDBMS provides (so is migration safe). You also create demand for your future colleagues (or future self) around 2038. Or you can use BIGINT if that bothers you.
I only know this because I work on a balloon experiment and some of the quantities our GPS reports are not in UTC Time (so don't have leap second corrections).
It covers repeating events, historical accuracy, ranges, events that are fixed in localtime but variable in UTC, and more.
TL;DR: Always store UTC datetime + TimeZone, sometimes you need to store a little more information.
http://www.quirksmode.org/blog/archives/2009/04/making_time_...
one can dream.
(lame-metaphors-r-us)