News for hackers. Stuff that matters.
Re:Rob Pike's Rules of Programming (1989) by OliverJones on Wednesday August 12, 2020 @04:39PM (#24136674)
In long-lived systems (systems that run for many years) it's almost impossible to choose the "right data structures" for the ages. The sources and uses of your data will not last nearly as long as the data itself.

What to do about this? Two things:

STORE YOUR TIMESTAMPS IN UTC. NOT US Pacific or any other local timezone. If you start out with the wrong timezone you'll never be able to fix it. And generations of programmers will curse your name.

Keep your data structures simple enough to adapt to the future. Written another way: respect the programmers who have to use your data when you're not around to explain it.

And, a rule that's like the third law of thermodynamics. You can never know when you're designing data how long it will last. Written another way: your kludges will come back to bite you in the xxx.

Re:Rob Pike's Rules of Programming (1989) by aserafini on Wednesday August 12, 2020 @05:16PM (#24137090)
Sometimes storing in UTC is simply not correct. For example a shop opening time. The shop opens 10am local time, whether DST or not. Their opening time is 10am local time all year but their UTC opening time actually changes depending on the time of year!
Re:Rob Pike's Rules of Programming (1989) by wtetzner on Wednesday August 12, 2020 @05:56PM (#24137489)
But a shop opening time is not a timestamp, so I think the original advice is still good. A timestamp is the time at which some event happened, which is different than a date/time used for specifying a schedule.

For example, if you wanted to track the history of when the shop actually opened, it would make sense to store a UTC timestamp.

Re:Rob Pike's Rules of Programming (1989) by TheCoelacanth on Wednesday August 12, 2020 @09:37PM (#24139136)
> A timestamp is the time at which some event happened, which is different than a date/time used for specifying a schedule.

Correct, but that makes this a rule with much more limited applications than many people are going to interpret it as.

Re:Rob Pike's Rules of Programming (1989) by account42 on Thursday August 13, 2020 @04:42AM (#24141206)
Yes, but scheduled times can look like timestamps. It might be tempting to store a date+time+location as just a UTC timestamps but timezones can and do change so the UTC timestamps for that scheduled time is not fixed.
Re:Rob Pike's Rules of Programming (1989) by dragonwriter on Thursday August 13, 2020 @09:53AM (#24143327)
> A timestamp is the time at which some event happened,

It's important to the advice to make explicit that the use of “timestamp” in that sense is intended, because “timestamp” is also in many contexts “the data type that combines date and time of day and, perhaps optionally, time zone information”. The application of “timestamps” in the latter sense is not limited to when they represent “timestamps” in the former sense.

Re:Rob Pike's Rules of Programming (1989) by Joker_vD on Thursday August 13, 2020 @06:05AM (#24141680)
And the time offset that was in effect when the event happened, allows you to easily answer questions like "Did the shop open late, i.e. after 10 AM local time?".
Re:Rob Pike's Rules of Programming (1989) by karmakaze on Wednesday August 12, 2020 @05:52PM (#24137444)
The most interesting case of this I encountered was for photo 'timestamps' on a global sharing site. UTC was being used and I was proposing a change to local time. There was great debate as many drank the UTC juice and stopped thinking.

It was when I showed them that we also have a 'shot at' location then proceeded to show Christmas eve photos showing the UTC time converted to the viewers local timezone (not always evening, not always Dec 24) alongside where the photo was taken. Just as in space-time a photo needs both a time and a place.

Re:Rob Pike's Rules of Programming (1989) by rurounijones on Thursday August 13, 2020 @02:57AM (#24140673)
Sounds like the problem was images being uploaded with a timestamp without a timzeone, in which case neither solution would work.
Re:Rob Pike's Rules of Programming (1989) by karmakaze on Thursday August 13, 2020 @09:55AM (#24143344)
The timezone could be inferred from uploader's geoip as a fallback. The problem was that even if the timezone was known at time of upload it was converted to UTC and lost when stored.
Re:Rob Pike's Rules of Programming (1989) by Joker_vD on Thursday August 13, 2020 @06:01AM (#24141660)
For historical events, where the local time is important, the combination of "UTC timestamp" and "local time offset in effect at the moment the timestamp was taken" seems to be the choice. Allows you to easily learn what time the wall clock was showing at the moment.
Re:Rob Pike's Rules of Programming (1989) by karmakaze on Thursday August 13, 2020 @09:59AM (#24143387)
Databases have support for a single type that encodes exactly like this. In postgresql a timestamptz shows as 'yyyy-mm-dd hh:mm:ss.123456+1234' but internally it's stored as UTC unixtime and tz offset.
Re:Rob Pike's Rules of Programming (1989) by Joker_vD on Thursday August 13, 2020 @10:33AM (#24143718)
Doesn't it actually store the timezone's IANA name and uses the tzdata to do conversions? That implies slightly more work than storing just the effective timezone offset, but is probably more correct when it comes to the timestamps in the future.
Re:Rob Pike's Rules of Programming (1989) by karmakaze on Thursday August 20, 2020 @05:18PM (#24229108)
Docs aren't 100% explicit but it seems that it uses tzdata etc to convert to offset as necessary and stores offset--that wouldn't affect correctness as the conversion is done now not in the future.
Re:Rob Pike's Rules of Programming (1989) by Benjammer on Wednesday August 12, 2020 @05:50PM (#24137418)
Totally. "Store everything in UTC" is just another flavor of "pick a timezone to store everything." In a lot of cases, you probably need to go ahead and just store the fully qualified date including timezone/offset for each record.
Re:Rob Pike's Rules of Programming (1989) by account42 on Thursday August 13, 2020 @04:44AM (#24141218)
Even storing offset or timezone might not be enough if what you really want is some future date and time at a particular location. Timezones do change, including the regions they cover.

Still, for things that have already happended, storing them as a UTC timestmp is almost always the correct thing to do.

Re:Rob Pike's Rules of Programming (1989) by wvenable on Wednesday August 12, 2020 @05:47PM (#24137394)
I made that mistake early my career following this exact advice and I ended up with a lot things were randomly 1 hour off depending on when the record was created and the date entered.
Re:Rob Pike's Rules of Programming (1989) by drvd on Thursday August 13, 2020 @01:54AM (#24140352)
That is the difference between a clock reading and a timestamp.
Re:Rob Pike's Rules of Programming (1989) by zelly on Thursday August 13, 2020 @03:25AM (#24140818)
> STORE YOUR TIMESTAMPS IN UTC. NOT US Pacific or any other local timezone.

What difference does it make if the timestamp includes the timezone? The UTC value can be recovered. In some applications the timezone is useful e.g. when intraday times matter.

Re:Rob Pike's Rules of Programming (1989) by bluGill on Thursday August 13, 2020 @09:49AM (#24143277)
Odds are that you forgot to record the timezone at all, and it didn't matter until you already have users in different timezones who have saved data and they don't remember which timezone they saved everything in.

If you record the timezone you can convert. Even then, it is easier to use UTC just because everyone else does and so you can feed UTC into any third party library and it will work.

Re:Rob Pike's Rules of Programming (1989) by swift532 on Friday August 14, 2020 @04:27AM (#24155317)
Daylight savings might get in the way, especially if daylight savings rules change some time later.
Re:Rob Pike's Rules of Programming (1989) by OliverJones on Thursday August 13, 2020 @07:47AM (#24142322)
People ask "why UTC"? Good question. Here's why:

You can always translate UTC to a local time in a given timezone. With IANA zoneinfo, you can do that correctly even for historical data in places where timezone rules changed in the past.

You can always calculate elapsed times correctly by taking differences between UTC timestamps. With local times you can't. Because daylight time transitions.

If you started with a local service, UTC lets you expand globally without explaining to your new customers why your timestamps are not in their timezones.

Daylight time transition days. Because daylight time.

Oddball daylight transition rules. Because Indiana USA, from the legislature that almost wrote a law declaring the value of π to be 22/7.

Because almost everybody will understand your decision, even after you're gone.

Re:Rob Pike's Rules of Programming (1989) by combatentropy on Thursday August 13, 2020 @09:16AM (#24143008)
PostgreSQL stores it as UTC if the data type is "timestamp with time zone" --- https://www.postgresql.org/docs/current/datatype-datetime.ht...
Re:Rob Pike's Rules of Programming (1989) by pjettter on Thursday August 13, 2020 @11:11AM (#24144148)
In our system, contracts have an expiration date/time. The only actors are Swedish. 12:59:59 must always be 12:59:59 on a certain date. It may never become 6:59:59 when someone in the company traveled to NY and prints out the contract.