British Columbia, Time Zones, and Postgres

(crunchydata.com)

114 points | by sprawl_ 7 hours ago

15 comments

  • jagged-chisel 4 hours ago
    Future events: store the local (at the event) date and time and timezone. You’ll keep the right context even if lawmakers decide to switch things up. You want to see your doctor at 8:30 AM on Monday September 14, 2026 whether it’s daylight saving time, or standard time or “they” decide on a fractional hour offset between the time you set the appointment and the time you attend the appointment.

    Past events: UTC timestamp.

    What format should you use? Human readable strings for longterm storage, because when things go wonky, it’s easier to debug.

    Note: nothing stops you from optimizing for queries by adding a field to store (or using a calculated index for) the integer epoch offset (e.g. unix timestamps), just make sure you know which field is authoritative.

    • lalaithion 2 hours ago
      What about virtual events between participants in different time zones? Whose do you keep stable if one has their clock moved under them?
      • lucisferre 52 minutes ago
        This is the critical problem with all of this.

        Daylight savings time changes, can't be globally banned fast enough really.

      • joshAg 1 hour ago
        If you're feeling nice, randomize whose time remains stable (to keep things fair), keep the organizer's time stable, or pick the time that minimizes the number of participants who will have the meeting time change.

        If you're feeling mean, randomize whose time remains stable (to make it hard to predict), move the meeting for the organizer, pick the time that maximizes the number of participants who will have the meeting time change, or split the difference and move everyone. Meeting was at 10 AM for Alice and 9 AM for Bob, but now it can be at either 11 AM for Alice and 9 AM for Bob or 10 AM for Alice and 8 AM for Bob? Now the meeting is at 10:30 AM for Alice and 8:30 AM for Bob.

    • mulmen 3 hours ago
      > What format should you use? Human readable strings for longterm storage, because when things go wonky, it’s easier to debug.

      You can just use a TIMESTAMP with no TZ data. It's functionally the same as using the string but simpler because you avoid all the string handling headaches and gain the benefit of avoiding to avoid double booking and date/time functions to answer questions like "how many appointments do I have in April?".

      • jagged-chisel 1 hour ago
        I am willing to concede to “human readable” and dropping “string” iff queries on TIMESTAMP are producing a human readable string (I believe they are … I haven’t been in postgres in at least six weeks and details like that don’t make a lasting impression in muh brain)
    • Xirdus 4 hours ago
      UTC for past events doesn't always work either. For example, historical employee punch-in times.

      UTC timestamps should only ever be used for points in time in the most literal sense, and nothing else.

      • drdexebtjl 3 hours ago
        Why not? It sounds like it would be correct even if the employee has a shift that includes a offset change.

        Future timestamps should be local because local timezone changes literally change the instant the event it will happen (relative to UTC). For past things, this can’t happen

        • Xirdus 2 hours ago
          Correct according to what? An employee who punched in at 9AM wouldn't show up as having punched in at 9AM anymore. Not unless you also store the exact timezone the UTC timestamps have been created with - but that's basically local timestamps with extra steps.
          • drdexebtjl 2 hours ago
            tzdata doesn’t change retroactively [1].

            If an employee clocks in at 2026-06-22 09:00 America/Sao_Paulo time, (which has a -03:00 offset today), and the server's clock is in UTC, the server will save 2026-06-22 12:00 to the database.

            If America/Sao_Paulo changes to -02:00 on 2027, it doesn’t affect conversions for past dates. You still get 2026-06-22 09:00 when trying to convert 2026-06-22 12:00 to local time in America/Sao_Paulo.

            edit: [1] unless it was wrong. in which case, you actually still want the UTC timestamp stored, so that you can just update tzdata and get correct local times, as opposed to saving the wrong local times in your database, that you now have to also fix.

            • Xirdus 32 minutes ago
              But which tzdata? Do you have the timezone or do you not have the timezone? If you have the timezone then why is your timestamp in UTC and not in the timezone that you have to store alongside the UTC timestamp?
      • Terr_ 3 hours ago
        If past timestamps (UTC or otherwise) are unreliable, then there is some kind of math-bug going on.
        • dqv 3 hours ago
          Not always a math bug. Sometimes a human bug. Tzdata can have errors (it's crowdsourced after all) that cause past UTC stamps to be incorrect because that incorrect tzdata was used at conversion time. And since most people aren't storing the tzdata version they're using with the stamp, it would be very difficult to make corrections without also corrupting other stamps.

          The bottom line is, if wall time is important, past or present, wall time needs to be stored.

          The only thing that can be guaranteed about a UTC timestamp is it's a UTC timestamp.

          • drdexebtjl 2 hours ago
            When was the last time tzdata was wrong about a period that already passed?

            Most of my career I’ve seen problems where it’s out of date, never where it’s up to date and wrong.

            • dqv 2 hours ago
              For the 2026a release:

                  Changes to past and future timestamps
              
                  Since 2022 Moldova has observed EU transition times, that is, it has sprung forward at 03:00, not 02:00, and has fallen back at 04:00, not 03:00.  (Thanks to Heitor David Pinto.)
              • drdexebtjl 2 hours ago
                Interesting. I could see that as an argument also for storing it in UTC, no?

                For example, if tzdata is 1 hour off, and you store your timestamps in UTC, it's immediately obvious that a local time is wrong because users will see events that just happened as having happened 1 hour ago. Update tzdata, and now everything is right.

                If you store the wall time, it _looks_ right, but fails if you attempt to compare/sort it with times in different timezones. To fix it, you need to actually modify the data in your database.

                • dqv 1 hour ago
                  Only for server-supplied timestamps.

                  Like the time clock example: sure what you're describing works if the user is just pressing a button to clock in and the server stores a UTC timestamp in response to a POST request or whatever.

                  But it's very common to need to backfill time. So the user backfills with their own supplied timestamps, those stamps get converted to UTC, tzdata changes a few months later, and HR is now asking for an explanation as to why they were late for those backfills and how it's possible they were working an hour after the shop closed.

                  It's never as simple as "just store it in UTC".

                  Conversion to UTC is lossy, so I prefer to keep up with the user-supplied time where appropriate.

          • __s 3 hours ago
            Seems like for airtightness you'd store utc alongside utc of when timestamp was stored alongside timezone
        • Xirdus 2 hours ago
          "UTC or otherwise" is important. Are you storing otherwise, or are you storing UTC? There are times where storing otherwise will lead to data loss in case the law changes whereas storing UTC would work (when you care about a literal point in time, like access logs). And there are times where storing UTC will lead to data loss in case the law changes whereas storing otherwise would work (when you care about wall clock time, like punch-in times).
    • joejoegobot 35 minutes ago
      [flagged]
  • chaidhat 2 hours ago
    Whenever I see a tz post, I want to remind ya'll that the `tzdata` package is using data from `eggert/tz` by Paul Eggert -- a crazy good UCLA professor. I took his course once and in the exams, he'd like to put in a question he didn't know the answer to himself.
  • rjrjrjrj 4 hours ago
    An added wrinkle is that parts of British Columbia use other timezones.

    The southeast corner follows Alberta time (previously MST/MDT but changing to MDT).

    Parts of the northeast and iirc a few other communities (eg Creston) have historically followed MST (no switch) and will now be effectively on the same time as Vancouver, albeit probably with a different TZ designation(?).

  • munk-a 4 hours ago
    This problem is not new and is a relatively minor exposure to the sort of issues that TZ conversion constantly needs to deal with. Different parts of the world have different dates that they adopt (or don't adopt) DST and some nations have changed this date in the past.

    Use a library, do not roll it yourself, do not try to outsmart tzdata... if you think you could then please volunteer for this project and either become a new Chronomancer[1] or get disabused of that misconception.

    1. It's a legal title, people actually have to call you a Chronomancer if you've contributed to tzdata, it's the law.

    • Terr_ 4 hours ago
      Indeed, recognizing Chronomancy will once have soon always been the case.
    • mulmen 3 hours ago
      What happens if I booked an appointment before the rules changed and TZDATA could be updated and deployed?
  • ivan_gammel 4 hours ago
    ANSI SQL has DATE and TIME types. Just use them for appointments bound to location. Conversion to current user timezone must happen in presentation layer and certainly does not belong to a database.
  • jedberg 5 hours ago
    I would contend that you shouldn't store anything but current unix timestamps in UTC in your database. If you must store time in some other way, then the two column method in the post will work, but leave it up to your software library to do it.

    I prefer to leave all the time conversions to software, wherein you only use battle tested libraries, and never do it by hand.

    Timezones are just too fraught with peril to try and do it on your own.

    Edit: changed some words to make clearer what I was saying.

    • ppchain 5 hours ago
      The issue described in the post is an example of when you cannot just rely on Unix timestamps. Specifically it comes down to which date is authoritative.

      A appointment with your dentist at 2pm Pacific Time in December 2026 has changed Unix timestamps in British Columbia. The dentist doesn't care about the Unix timestamp, she cares about the wall clock local time when you arrive for the appointment.

      • Terr_ 3 hours ago
        To frame it another way, your dentist has agreed to a particular triggering condition. (Whether they realize that's how their country works or not.)

        We can all make pretty-good guesses about when that condition will be fulfilled in N seconds, but until it actually finally happens, the true occasion could land somewhere else.

        "Three months later at 2PM where I live" is not so different than "when the thrush knocks during the setting light of Durin's Day." You can guess that it's N seconds from now, but you might be wrong.

      • jedberg 4 hours ago
        I edited my comment to make it clearer. I meant you should only directly store current timestamps, anything else you should leave up to a library to store as it sees fit.
        • ncruces 4 hours ago
          The post is for the otherworldly magician who wrote your library then.
      • ngaheer 4 hours ago
        "Which date is authoritative".

        I don't understand this. The consumer books in his/ her local time stamp i.e. 12 PM pacific. Gets stored as Epoch milliseconds (and is passed around as a data structure i.e. Date struct with UTC as the timezone) and the providers sees the time stamp 3 PM EST or 2 PM CST depending on it's timezone at runtime (interface the provider it works with).

        I don't understand why a specific timezone has to be "authoritative" here. What am I missing.

        • Xirdus 4 hours ago
          The appointment is for 12/1/26 2PM of whatever timezone the office is in at the time of the appointment.

          Between the time of booking and the time of the appointment, the government changed what timezone there will be at the time of the appointment. If you calculated the Unix timestamp at the time of booking using the old laws, by the time of the appointment the Unix timestamp would be off by an hour.

        • Terr_ 3 hours ago
          Clocks are social constructs, and your system needs to have some decision in it about whose clocks take precedence over other ones.

          No matter which of these you choose, there's a possibility that someone says: "Hey! That's wrong! I never agreed to that":

          1. The meeting shall be in precisely N seconds, no exceptions. (UTC, or something very close to it.)

          2. The meeting shall be when participant A's wall-clock shows X.

          3. The meeting shall be when participant B's wall-clock shows Y.

          At the present instant, you might have predicted values so that X Y and N all land on the same spot, but the prediction is not reliable and the equality will collapse if anybody's government makes timezone changes. Or if their country is taken over by another. Or splits due to civil war.

        • mgaunard 4 hours ago
          Never heard of DST? The authoritative time is constant in the local time zone, but needs to change in UTC twice a year.

          This is the exact reason people store time in local time zones.

          Also remember the date/time where DST switching occurs is entirely timezone-specific, and it's not necessarily the same pattern every year (as demonstrated with British Columbia).

        • Mogzol 4 hours ago
          You have to choose one timezone to be authoritative here, which one you choose depends on the situation, but if you don't choose one, then a change like the one happening in BC will cause the consumer and provider to be using mismatched times.

          If the consumer booked something for 12PM Dec 1 2026 PST, and this booking was made before BC's changes were announced, and the provider saved this using their local time (say EST), then they would have saved it as 3PM EST. But now with BC's changes, when the consumer shows up at 12PM their time, that will be 2PM EST, an hour before the provider was expecting.

          Was the consumer correct for showing up at the booked time according to their wall clock, or is the provider correct with their saved the time that was an hour later? The answer probably depends, but whichever you choose is the authoritative time.

        • em-bee 4 hours ago
          you booked an appointment in the future. before the day of your appointment arrives, british columbia changes the timezone. all appointments after that change now must follow the new timezone rules. therefore the time of your appointment relative to UTC is now different.
        • slyall 4 hours ago
          Except that the timezone in British Columbia has now changed. Lets say you stored the appointment for November in UTC.

          So you set the time to 10pm UTC to match 2pm British Columbia. But because the timezone for BC has changed that 10pm now matches 3pm in British Columbia

          So the Dentist is expecting you at 2pm and you come along at 3pm

    • ivan_gammel 4 hours ago
      It‘s a common mistake to store everything as UTC timestamps and shows lack of understanding of time domain. Local time exists and it is neither UTC or timezone-dependent. Doctor office opens at 8 a.m. regardless of whether it is DST or not. Appointments are made in local time. Store them in local time.
      • remus 3 hours ago
        > Appointments are made in local time. Store them in local time.

        You may just be illustrating a particular use case, but it is more ambiguous in the general case. For example if you have arranged a meeting with someone in another timezone then maintaing the local timezone could lead to a misalignment for one of the participants.

        • ivan_gammel 2 hours ago
          This is ok. When future local user time offset is unpredictable, pinning meeting time to a certain location is a good strategy. If at some point meeting shifts for some user, well-designed calendar app could warn them and let reschedule. It still works better than pure UTC because it is predictable for at least some of the users.
    • merb 5 hours ago
      In that case only storing utc did not work when you created a date in the future before you updated tzdata
      • jedberg 4 hours ago
        I edited my comment to make it clearer. I meant you should only directly store current timestamps, anything else you should leave up to a library to store as it sees fit.
        • phantom784 4 hours ago
          How would this solve the British Columbia issue as described in the article?
    • rini17 4 hours ago
      If you don't understand what the library is doing, and blindly put in local time without any consideration, you will get bitten someday. And all libraries use the same timezone database/logic anyway and run into same issues the author describes.
  • necro 3 hours ago
    Time is local Timestamp is a counter

    A unix timestamp does not have different timezones. It is a counter. No matter where u are in the world a timestamp call should give you the same numeric value at the same instant. It is not time zone adjusted. Store that number, unadjusted as the source of truth. You can get to any local time after that.

  • StayTrue 4 hours ago
    Dumb change on the part of British Columbia.

    Source: me, BC resident.

    • vojtapol 3 hours ago
      Why?
      • patmcc 2 hours ago
        No the OP, but I'm also in BC and dislike this change.

        The change was made (partly) based on a bullshit poll of residents, that asked basically "Hey which would you rather do, use year-round Daylight Savings Time or keep switching every six months?" - notably not including the option a lot of people wanted (and which is well-supported by a lot of research as the best option), which was "use year-round Standard Time".

        And then in a bunch of press conferences and answers to the public, they would say "oh, this is what people chose" from the poll.

        • empressplay 2 hours ago
          If we did that the sun would be coming up at 4am right now in Revelstoke. What's the point of the sun being up at 4am?

          On the other hand, I don't like it getting dark at 3:30 in the afternoon in Vancouver around Christmas. I know it means it will be darker later in the morning but you wake up in the dark that time of year already anyways.

          • KyleSanderson 2 hours ago
            Go for a run, hit the ski hill, all before you start work. The sun runs your health (circadian rhythm). I'm in Vancouver and this is the dumbest change. We're going to pay for this for a decade in premature deaths, and we'll end up on standard time anyway.
        • StayTrue 2 hours ago
          Correct and complete explanation.
      • emptybits 2 hours ago
        BC resident. I do look forward to not adjusting to a twice-annual time change. BUT, I would have strongly preferred sticking to Standard Time, year round, instead of DST. That option was apparently not on the table, and strangely got little mention or investigation by media.

        So a daily event approximating High Noon is literally gone forever here. Yes yes, depending on longitude, it was never there, but now it's even farther away. Life goes on...

  • ncruces 4 hours ago
    This strategy fails for appointments during that hour where the clock goes back: they are ambiguous, can refer to two different moments in time.

    That caveat aside: good.

    • jagged-chisel 4 hours ago
      I have yet to find a technological solution to this social problem.

      Also, I have yet to encounter this problem. For personal events, I sleep during this time. For company events, we always avoid this time.

      • ncruces 4 hours ago
        I encountered it when I was design the scheduling back-office for a LED video wall a few years ago when those became economical for a shop to own and run 24/7.

        The customer probably never noticed if I even did it “correctly” or couldn't be bothered if I didn't, but I remember I was bothered by it: (1) ensuring continuity of programming during the gap when it jumps forward (2) solving the ambiguity when it went backwards.

        Because obviously they wanted to think in local time.

      • rjrjrjrj 2 hours ago
        I have, in the context of time series charts. Lots of back and forth with QA.
      • mulmen 4 hours ago
        You're right that for the most part this is avoided by convention and scheduling time changes at quiet times of day.

        A bit contrived but consider you are a maintenance worker in a facility that uses isolated timekeeping devices. "Change the clock on the vault back one hour at 3:00am".

    • rawling 4 hours ago
      Sounds like the quoted RFC would help here. Storing the offset would make it unambiguous which of the two moments was meant. Your business logic would have to figure out what to do when the offset no longer exists (honour the clock time or convert to the new timezone) or is nonsense. The geographical reference would help decide what to do if you're not in a single location.
    • rini17 4 hours ago
      Humans would often fail such appointments too.
  • _whiteCaps_ 4 hours ago
    I just need to know what happens to our 9am standups in Vancouver when the other team is in SF. If I'm doing the math correctly it moves to 10am.

    Also, I've often picked a random city in Pacific time when setting timezones on hosts, so I guess it's going to cause me some headaches in the fall.

    • empressplay 2 hours ago
      Don't worry, WA, OR and CA probably aren't far behind.
  • pphysch 1 hour ago
    This is one of those cases where I would prefer to be antifragile and rapidly "patch the data" once as opposed to trying to perfectly solve problems like this before they arise. In all likelihood this will never happen in a particular timezone.
    • dqv 17 minutes ago
      > In all likelihood this will never happen in a particular timezone.

      You sure about that?

      https://lists.iana.org/hyperkitty/list/tz-announce@iana.org/...

      2026b - changes to future timestamps

      2026a - changes to past and future timestamps

      2025c - changes to past timestamps

      2025b - changes to past timestamps

      2025a - changes to future timestamps

      2024b - changes to past timestamps

      2024a - changes to future timestamps

      2023d - changes to past and future timestamps

      2023c - changes that changed future timestamps reverted

      2023b - changes to future timestamps

      I definitely prefer the... fragile? approach for this problem

  • xp84 3 hours ago
    I hope this gives us Americans the needed encouragement to do the same on the west US coast. Utter insanity to screw with the clocks twice a year instead of letting various institutions who have a compelling need, to publish "Summer hours" to suit them.
    • tadfisher 2 hours ago
      Apparently there is a Federal restriction: we can opt to eliminate DST, like Hawaii and Arizona, but we cannot unilaterally decide to adopt "permanent DST" as did British Columbia. So we'll have to figure out how to get both houses of Congress to pass a bill into law, and have the President sign it, without said institutions convincing the world that keeping your clocks 1 hour forward is woke propaganda and injects gay chemicals into tadpoles.
      • novemp 54 minutes ago
        Isn't the simple option to just move time zones? If you're not allowed to swap to PDT year round, just change to MST instead.
  • mulmen 3 hours ago
    The issue here seems to be that the behavior of tzdata (correctly) changes over time. Can all this complexity be avoided by storing the tzdata version in the timestamp itself so it can decoded with the same rules?
    • dqv 43 minutes ago
      It's just a different kind of complexity and one that requires having a library that can arbitrarily load different versions of tzdata.

      I've been thinking about it for a while though - a time zone conversion library that also accepts an additional "tzdata_version" argument.

  • acalvino4 5 hours ago
    [dead]