noted.do
The Tech Guy

Time Zones Will Humble Every Programmer Eventually

Ask a non-programmer how hard it is to store a time and they'll look at you funny. It's a number. Hours, minutes, done. Then you build anything with users in more than one place and it slowly dawns on you that time is one of the messiest things software has to deal with.

The first surprise is that time zones aren't neat one-hour slices of the globe. India runs on UTC+5:30. Nepal is UTC+5:45. Parts of Australia are on half-hour offsets. Some countries span several zones and some huge ones use just one. And the boundaries are political, not geographic, which means governments can and do change them, sometimes with very little notice.

Then there's daylight saving time, which is where the real pain lives. Not every country does it. The ones that do don't switch on the same dates, and the northern and southern hemispheres go in opposite directions. So the gap between two cities can change several times a year. When clocks spring forward, a whole hour of local time just doesn't exist, so 2:30 AM on that date is a time nobody experiences. When they fall back, an hour happens twice, so "1:30 AM" on that night is ambiguous. If your app schedules a job for 2:30 AM every night, it'll either skip a day or run twice at some point, and you'll find out from a confused user.

This is also why "UTC-5" and "America/New_York" aren't the same thing. An offset is just a number. A named zone is a set of rules for which offset applies when, including all the historical changes. Those rules live in the IANA time zone database (often called tzdata or the Olson database), which gets updated regularly because the world keeps changing its mind. Your operating system and language runtime ship a copy, and keeping it updated is part of keeping clocks right.

There's more weirdness in the corners. Not every day is 24 hours long in local time; DST days are 23 or 25. Adding "one day" and adding "24 hours" give different answers across a DST change. Months have different lengths, so "one month after January 31" has no obviously correct answer. And there are leap seconds, though most application code gets to ignore those because operating systems smooth them over.

So what do you actually do? The rules that have served people well for years:

Store moments in UTC. When something happened (a signup, a payment, a log entry), save it as UTC, or as a timestamp type that stores an absolute moment. Convert to the viewer's local time only when you display it.

Store future local events as local time plus a zone name. This is the one that surprises people. If someone books a meeting for 9 AM in Chicago six months from now, store "9:00, America/Chicago," not the UTC equivalent you calculated today. If the DST rules change before then, the UTC version will be wrong, but the local version will still mean what the user meant.

Use zone names, not offsets, whenever you mean a place. Offsets are fine for recording a past moment, but they can't tell you what happens next spring.

Use a proper date library. Don't do math on time strings or add 3600 seconds and hope. PHP's DateTimeImmutable with DateTimeZone, Python's zoneinfo, and the newer Temporal API in JavaScript (where available) all understand zones and DST. Older JavaScript code often leans on a library like Luxon or date-fns-tz because the built-in Date object is pretty limited.

Set your servers to UTC. It removes a whole category of confusion in logs and scheduled jobs, and nothing on the server skips or repeats an hour.

Be careful at the database layer. Different databases treat timestamp types differently. Some store an absolute moment and convert based on the connection's time zone setting, some store whatever you give them with no zone at all. Know which one you're using, and set the connection time zone explicitly so there are no surprises.

And when you write tests, put a few dates right around DST changes in there, plus a user in an odd offset like +5:45. Those are the bugs that sit quietly for months and then show up twice a year.

My honest advice is to never write your own time zone logic. The people who maintain these libraries and the tz database have already found the edge cases. Lean on their work and spend your time on something more fun.

Comments

Be kind. See the rules.