Time, Timezones, and Locale-Aware Data
Why you store timestamps in UTC and convert at the edge, the DST bugs that hit when you don't, and storing a user's timezone instead of assuming the server's.
Time, Timezones, and Locale-Aware Data
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- A timestamp stored as UTC is an unambiguous point in time; a timestamp stored as 'local time' with no timezone attached is a guess that depends on where you assume it came from, which is why UTC-at-rest is the practical default.
- Daylight saving time isn't just an inconvenience for humans — it means the same wall-clock time can refer to two different real instants once a year, and a naive timezone conversion in code will silently pick the wrong one.
- Storing a user's timezone as its own piece of data, rather than assuming the server's timezone or inferring it once at signup, is what makes a correct conversion possible later, including after the user travels or the timezone's own DST rules change.
- Getting storage and conversion right is a backend correctness problem; how a date or number actually displays to a given user is a separate, UI-level concern that belongs to locale formatting, not to this lesson.
Why “what time is it” is a harder question than it sounds
A timestamp with no timezone attached is not actually a complete piece of information — it’s a guess waiting for context. “2024-03-10 2:30 AM” means something different depending on whether that’s Eastern Time, Pacific Time, or UTC, and the difference isn’t a rounding error, it can be several hours. The embedded Computerphile video (fronted by Tom Scott, who’s covered this exact flavor of “simple-sounding thing turns out to be deeply nasty” territory for years) builds up the complexity of time and timezones through a series of escalating real examples, and it’s worth watching precisely because the examples feel absurd right up until you realize your own codebase has the same bug waiting in it.
For a backend developer, the practical upshot is this: any timestamp your system stores or passes between services needs to be unambiguous about what instant in time it refers to. “Unambiguous” is the whole game here, and it’s why the standard advice is so blunt: store it in UTC.
Why UTC at rest, and conversion only at the edge
UTC (Coordinated Universal Time) doesn’t observe daylight saving time and isn’t tied to any particular region — it’s a single, fixed reference point. A timestamp stored in UTC means the exact same instant no matter what timezone your server runs in, what timezone your database server runs in, or what timezone the person reading it happens to be in. That fixedness is precisely what you want for data at rest: an event log, an created_at column, a record of when a payment cleared — these are facts about a specific instant that happened, and they should never need reinterpreting later just because you moved your server to a different region or added a second one.
The conversion to a human-readable local time should happen as late as possible — at the edge, right before something gets displayed or read by a person — not baked into storage. Convert too early (storing “local time” directly) and you’ve thrown away the information needed to convert correctly later. This is also why relying on your database engine’s own timezone-conversion functions is a common trap: derickrethans.nl’s widely cited piece (linked below) on this exact topic argues that most databases don’t track DST rule changes reliably enough to trust for anything beyond trivial cases, and converting in application code using a proper timezone library, against the current IANA timezone database, is the safer default.
The DST bug that catches almost everyone once
Daylight saving time isn’t just mildly annoying for scheduling meetings — it introduces an actual correctness bug if you’re not careful. Twice a year, in places that observe it, the offset between local time and UTC changes. That means the same local wall-clock time can refer to two completely different real-world instants: when clocks “fall back” in autumn, the hour between 1:00 AM and 2:00 AM happens twice. A naive conversion that just adds or subtracts a fixed number of hours, instead of asking “what was the real UTC-offset on this specific date, in this specific timezone,” will get this wrong — sometimes by producing a timestamp an hour off, sometimes by failing outright on the ambiguous hour.
This is why storing “local time plus a fixed UTC offset” is also not quite enough for anything in the future. A meeting scheduled for six months from now, converted to UTC today using today’s DST offset, can end up at the wrong real-world hour if the local region’s DST rules change in the meantime (governments do this more often than you’d expect). The fix for genuinely future events is to store the timezone identifier itself (America/New_York, not just -05:00) alongside the local time, and compute the UTC instant at the moment it’s actually needed, using up-to-date timezone rules — not to bake in an offset that might go stale.
Store the user’s timezone, don’t assume the server’s
A related mistake is assuming everyone is in the same timezone as your server, or as whoever happened to write the original code. The fix is simple to state and easy to skip: store a user’s timezone as its own explicit field, set it from something real (their browser’s detected timezone, a profile setting) rather than inferring it once and never revisiting it. People travel. People change regions. A user’s timezone from a year ago being treated as still-current is exactly the kind of quiet, hard-to-notice bug that produces a support ticket reading “my reminder fired at the wrong time” with no obvious cause in the logs.
Where this stops being this lesson’s job
Everything above is about correctness: making sure the instant in time your system records and computes is actually the instant you meant, regardless of where anyone involved happens to be. It is a backend and database problem. How that correct instant then gets displayed to a given user — whether a date reads as 03/10/2024 or 10/03/2024, 12-hour or 24-hour clock, which calendar system, which language the month name appears in — is a UI-layer decision, not a storage one, and it’s covered by internationalization and localization rather than here. A useful way to keep the boundary straight: if getting it wrong corrupts the actual data, it’s this lesson’s problem; if getting it wrong just looks unfamiliar to a reader in a different region, it’s that one’s.
Check yourself
Why is storing a timestamp in UTC better than storing it in whatever timezone the server happens to be running in?
UTC is a single, fixed reference that never changes meaning regardless of where the server, the database, or the reader happens to be, while 'the server's local time' silently changes meaning if you ever move the server, add a second server in another region, or the server's own timezone rules shift — UTC removes an entire category of ambiguity at the point of storage.
Why does daylight saving time break a naive timezone conversion rather than just shifting it by a constant offset?
Because the offset between a local timezone and UTC isn't constant — it changes twice a year in places that observe DST — so converting a local time to UTC (or back) requires knowing the exact date, not just the named timezone, and the one-hour period when clocks fall back actually repeats a wall-clock hour, making a plain local timestamp genuinely ambiguous about which occurrence it means.
Why do you need to store a user's timezone as data, rather than just converting to UTC for storage and calling it done?
Because converting a stored UTC instant back to something a person can read requires knowing which timezone to convert it into, and for genuinely future events (a meeting scheduled for next month, a recurring reminder) you also need the timezone identifier itself, not just a fixed offset, since that region's DST rules — and therefore the correct real-world instant — can still change between now and then.
What's the boundary between this lesson's scope and locale-aware formatting on the frontend?
This lesson covers storing and converting timestamps correctly so the underlying instant in time is never ambiguous or wrong — a backend and database concern; how that instant gets displayed to a specific user (date order, 12- vs 24-hour clock, which calendar, which language) is a presentation decision made at the UI layer, which is covered in internationalization and localization, not here.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.