News for hackers. Stuff that matters.
Re:Timezone updates need to be fixed by vemv on Thursday January 28, 2016 @12:18PM (#10989522)
Seems a misguided practice to me.

* Store datetime values in UTC. Else you get inconsistent values, which has unpredictable consequences.

* Detect user's expected timezone using one of : a) user preference setting, b) the region the app is supposed to be running for, c) IP geolocation. Choice depends on the app requirements. Don't default to any timezone unless considered 100% safe.

* Present DB values in the UI using said detected timezone.

* UI-wise, slways display timezone information for datetime values. At the very least in a tooltip.

Re:Timezone updates need to be fixed by laut on Thursday January 28, 2016 @12:40PM (#10989721)
It is not misguided, it is a good practice. Read more about why here: http://www.creativedeletion.com/2015/03/19/persisting_future...
Re:Timezone updates need to be fixed by Someone1234 on Thursday January 28, 2016 @01:03PM (#10989924)
One edge case doesn't disprove a rule, and timezone/DST rules changing is definitely an edge case.

For internationalisation storing in UTC is still the gold standard, since it remains consistent with itself across time and DST zones.

Storing in local and then converting from one timezone to another timezone on demand has far more edge cases and gotchas. Plus it too isn't immune from rule changes, if you store in local time (e.g. 10 am) but DST rules changes, from an outside observer's perspective (someone in any other timezone) the meeting's time has now changed.

So even just using that edge case you're talking about exchange one downside (local time changes) to another downside (remote time changes).

Re:Timezone updates need to be fixed by jasode on Thursday January 28, 2016 @01:46PM (#10990240)
>For internationalisation storing in UTC is still the gold standard, since it remains consistent with itself across time and DST zones.

Saving as UTC doesn't really solve the issue (although it seems like it does).

The issue that the previous posters (Khao, laut) didn't make explicit is that we have 2 concepts of datetimes:

1) scientific datetime (which UTC is unambiguous, except for obscure things such as leap seconds)

2) socially-constructed datetime (which "local" time + TZ resolves most (but not all) of the ambiguities)

Scientific(UTC) datetime works great for past events like server logs, and some types of future events such as celestial events. For example, the upcoming solar eclipse in August 21 2017. Regardless of what future DST rules are decreed by the government, the moon is going to move in front of the sun at a 16:48:33 UTC time.

Socially-constructed future datetime such as appointments don't work the same way. For example, we presume that a Super Bowl on the east coast in the year 2025 will have a kick off of 6:30pm but we can't encode that as UTC unambigously because we don't know today what DST rules will be in effect 10 years from now. In 2016, we don't know whether to encode it as 22:30UTC or 23:30UTC.

That said, if the planning horizon is short (such as sending out alerts for an upcoming conference call to participants spanning timezones), one can encode it as UTC internally and then decode it to local wall-clock time. It may be simpler that way with less bugs.

Re:Timezone updates need to be fixed by feld on Thursday January 28, 2016 @02:11PM (#10990439)
I disagree. Storing as UTC is the only sane way. It will never get corrupted. Calculating the timezone difference for the user is not that hard and won't result in a database full of mistakes you have to scramble to fix.
Re:Timezone updates need to be fixed by laut on Thursday January 28, 2016 @05:12PM (#10991827)
Did you read the article? If a user wants to save a local time and you then convert it to UTC and save that instead, then it can in some cases be impossible to later find out what the entered local time was.

The users will rightfully expect their time to stay 14:00 Santiago time if they enter 14:00 Santiago time.

Re:Timezone updates need to be fixed by Avernar on Thursday January 28, 2016 @03:23PM (#10990994)
Not for events like shows, movies, work hours, etc. An event starts and stops at a specific local time. If the DST rules change then the UTC version will be wrong but the one stored in local time + TZ will be correct.

Intervals should be stored in UTC. If you're supposed to drop the control rods in a reactor in 24 hours you store the time it's supposed to happen in UTC. DST transitions and rule changes would mess this up if stored in local time.

Timestamps for logs and files should also always be UTC.

Re:Timezone updates need to be fixed by feld on Thursday January 28, 2016 @04:57PM (#10991705)
UTC is an anchor. It does not change, it is never wrong. Your application or environment may live under different rules because of location but that problem can be solved by the timezone-aware application.

If I am scheduling an event at 08:00 and my TZ is -5, the event is at 13:00 UTC. If my timezone changes due to a new law UTC is not wrong; the event is still going to happen at 13:00 UTC! If the event was previously stored as a local time and not UTC the EVENT is wrong! You solve this by handling the TZ conversion elsewhere and making sure it always has the latest rules.

Always store in UTC. Your argument makes no sense.

Re:Timezone updates need to be fixed by laut on Thursday January 28, 2016 @05:21PM (#10991894)
If the user expects 8:00 local time then it should stay 8:00 local time. That is at least how people would want their applications to work 99% of the time. If someone enters 8:00 and then the software suddenly changes it to something else, that is confusing.

If you actually look at what happens when timezone laws change, people continue to anchor to their local times.

Re:Timezone updates need to be fixed by feld on Thursday January 28, 2016 @05:48PM (#10992084)
Congratulations you literally just rescheduled the event. This is not a timezone issue. The date should still be stored as UTC.
Re:Timezone updates need to be fixed by jasode on Thursday January 28, 2016 @06:06PM (#10992225)
>Congratulations you literally just rescheduled the event.

What does that mean to reschedule? Run a SQL batch job on the database to shift the UTC times by 1 hour?!? Which rows? All of them? Or the rows for a particular TZ? (Better make sure that SQL WHERE clause is error free.) You're just recreating the ambiguity problem but from a different direction.

The issue you keep missing is the UTC is ambiguous when attempting to store the scheduling intentions of the user. If the user wants to store a future socially-constructed datetime, storing it purely as UTC will throw away information.

Re:Timezone updates need to be fixed by Avernar on Thursday January 28, 2016 @06:00PM (#10992175)
Wrong. Let's say an event is scheduled for 6pm daily in Toronto (EST -0500). You store it as 11pm UTC. Now eventually we switch to EDT -0400 due to DST. Now your system will say the event is at 7pm local time.

YOU just rescheduled the event.

Works the same with dates and not just times. If you store each of those events individually and the politicians move the DST switch date (or an error was corrected in the TZ database) then some of your event times will now be an hour off.

Re:Timezone updates need to be fixed by feld on Thursday January 28, 2016 @06:12PM (#10992275)
The event was literally moved to a different time UTC. I'm glad we agreed that it was rescheduled.

UTC is still the correct way to store it if you want it to be timezone-aware.

If every event a specific application needs to be at local time regardless of what the timezone is, stop storing any reference to a timezone.

Re:Timezone updates need to be fixed by Avernar on Thursday January 28, 2016 @07:01PM (#10992538)
> The event was literally moved to a different time UTC. I'm glad we agreed that it was rescheduled.

I think I see the confusion here. If you went to a group of people and said the movie you were all going to was rescheduled from 11pm UTC to 10pm UTC because of DST you'd get a bunch of "Huh?" and "What local time was it rescheduled to?" You'd answer it was rescheduled from 6pm to 6pm local time. You'd get a bunch of laughs at your "geek humour" while they secretly thought you were nuts. :)

Social events like meetings, movies, etc. are not considered rescheduled when DST starts/stops or the TZ rules get changed.

If you stored the event as UTC you'd have to alter your database when DST starts/stops or the TZ rules changed (is your system going to compare the old and new rules automatically now?).

Re:Timezone updates need to be fixed by Avernar on Thursday January 28, 2016 @06:43PM (#10992450)
We don't agree. It was not rescheduled in reality. Your system would have said it was rescheduled to 7pm Toronto time while in reality the event would still be 6pm Tronto time. The people using your site/app/whatever would be an hour late.

UTC is the correct was to store timestamps (things that happened) and intervals (things that need to happen x seconds/minutes/hours from now).

For future events at a specific geographical location you need to store it as local time + timezone. Many databases have a time+timezone type.

Re:Timezone updates need to be fixed by halostatue on Thursday January 28, 2016 @11:52PM (#10993575)
I would go as far to say that the timezone stored should be a named, geographically-based timezone (e.g., America/New York or Canada/Eastern) as opposed to a timezone offset. (If you store the timezone as EDT or EST, you have still encoded -0400 or -0500.)
Re:Timezone updates need to be fixed by Avernar on Friday January 29, 2016 @12:14AM (#10993641)
The postgres documentations says exactly that. Other databases might do something else so it's best to read the documentation.
Re:Timezone updates need to be fixed by feld on Friday January 29, 2016 @12:38PM (#10996814)
but a DST change is a government-mandated rescheduling of the entire lives of its citizens within that jurisdiction :-)
Re:Timezone updates need to be fixed by jasode on Friday January 29, 2016 @03:36PM (#10998295)
I find it hard to believe that you can't see how people are obviously using the word "rescheduling" in relation to local wall-clock time. The wall-clock time as the reference point is how everyday people commonly use "reschedule". When we flip DST on and off, nobody says "schools and banks got opening times got rescheduled."

And yet, you keep emphasizing "rescheduling" in relation to UTC instead of local time. We already know that DST changes changes the underlying absolute time in UTC. And that's the very reason why you cannot store a future datetime as UTC!

Here's a trick question to put your advice about storing future appointments as UTC to the test:

You want to save a datetime for the New York Times Square midnight countdown to ring in the New Year on December 31, 2018 @ 11:59:45pm. As far as the user is concerned, his/her brain thinks in terms of local wall clock time and therefore, that would be 2018-12-31 23:59:45 ET. (Notice it is "ET" and not "EST" or "EDT")

Do you think you can save that user-specified datetime to the database as UTC?

Do you think it's 2019-01-01 04:59:45UTC ? (Wrong answer.)

Do you think it's 2019-01-01 03:59:45UTC ? (Wrong answer.)

If you don't see why both of those UTC choices are incorrect, please study the previous posts.

To recap the problems with UTC:

-- you don't know in 2016 what future UTC timestamp to write to the database. The user doesn't know either. Nobody knows. It depends on what the government does in the next 3 years.

-- UTC implies scientific precision and writing it to the database for future datetimes will incorrectly advertise that you have the clairvoyance about future rules. You don't.

-- UTC is correct abstraction level for past events (server logs, audit logs) and future non-human generated scientific datetimes such as moon phases and solar eclipses. UTC is the incorrect abstraction level for human generated appointments' datetimes.

Attempting to save a socially-constructed future "wall-clock" datetime as UTC is throwing away information the user gave you. Stop advising people to do that.

Re:Timezone updates need to be fixed by Khao on Thursday January 28, 2016 @12:24PM (#10989591)
In our case, the admins who create/manage those events have to specify the event location, so we fetch the timezone for that location and if there's any mismatch, they can change the timezone manually. Since we migrated all the data, we have not received a single bug report for an issue that was caused by DST or from converting UTC times to local timezones.
Re:Timezone updates need to be fixed by vemv on Thursday January 28, 2016 @12:51PM (#10989821)
I retract my opinion - the approach is clearly correct for that business domain.

The key difference is here:

> The thinking is that if a client is shopping for a class in a different timezone, why would we convert the class's time to the client's timezone? It makes no sense.

Roughly speaking, basically there are two kind of apps:

- apps where datetime values need to be localized ("this message was sent at 18:00")

- apps where datetime values need to reflect a fixed timezone ("we will meet in Madrid, at 18:00 local time")

Re:Timezone updates need to be fixed by Khao on Thursday January 28, 2016 @01:05PM (#10989945)
In our app, everything that's a timestamp (invoices, payments, registering to an event, sending messages, create/update timestamps, etc..) is stored in UTC. Having both depending on the use case is really the best for us.