‹ BackHN Continuity

Thread

Footguns with Postgres “at time zone 'UTC'”

169 points · 114 comments · birdculture

  1. beybol · · focus · HN ↗
    I don't rely on time calculations at the database level. I handle them in the application based on UTC time stored in the database and the user's time zone. This shifts the problem to the application code, where I can control it precisely and make conscious decisions about how to handle specific business requirements, such as when a day ends or how to deal with events across different time zones. It also makes it possible to properly test all cases with unit tests.
    1. ahoka · · focus · HN ↗
      I've seen a product where they used UTC to store opening hours. They had to rewrite all dates via a script twice a year.
      1. GJim · · focus · HN ↗
        Why would the dates need rewriting?
        1. sokoloff · · focus · HN ↗
          I suspect they meant more generally “the date&time field”, but a store in Boston that opens at 7:30 AM and closes at 7:30 PM Eastern time (ET) closes on different UTC date than it opens when ET is EST but opens and closes on the same date when ET is EDT, so it’s plausible that the dates actually needed to be updated.
        2. ahoka · · focus · HN ↗
          Daylight saving changed the UTC time, because opening hours were always in local time.
      2. beybol · · focus · HN ↗
        I don't know the exact use case, but it sounds weird. If UTC is the single source of truth, then when you display the data and want to change some business assumptions, you only need to change the application logic. The underlying data remains unchanged.

        One scenario I can imagine is when you want to store the results of business calculations in the database. In that case, once the algorithm changes, you may also need to update the stored data. This may be necessary for performance reasons.

        In all other cases, calculating the result on the fly solves the problem and does not require a database update when the business rules change.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.