Guide

Unix timestamps and time zones: the mistakes that cause real bugs

Published · 9 minute read

Time handling bugs are rarely about arithmetic. They come from a small number of wrong assumptions: that a timestamp carries a time zone, that an offset is a time zone, that every day has 24 hours. This guide goes through those assumptions one at a time.

What a Unix timestamp is

A Unix timestamp is a count of seconds since 00:00:00 UTC on 1 January 1970, not counting leap seconds. It identifies an instant. It has no time zone: 1700000000 is the same moment everywhere on Earth. A time zone only enters when you turn that instant into a calendar date and wall-clock time for a person to read.

This is why timestamps are the safest thing to store and exchange for events that have already happened, such as "this row was created" or "this payment cleared".

Seconds or milliseconds?

The single most common timestamp bug is a factor of a thousand. Unix tools, most databases and JWT claims use seconds. JavaScript's Date.now() and Java's System.currentTimeMillis() use milliseconds. Some systems use microseconds or nanoseconds.

Digits (for dates around now)Unit
10seconds
13milliseconds
16microseconds
19nanoseconds

The symptoms are easy to recognise. Milliseconds read as seconds give a year tens of thousands of years away. Seconds read as milliseconds give a date in January 1970. If you see either, check the unit before anything else. A timestamp converter that accepts both will show you at a glance which one you have.

An offset is not a time zone

+05:30 is an offset: a fixed distance from UTC. Asia/Kolkata is a time zone: a set of rules that says which offset applies on which date, including every change governments have made in the past. For India the two happen to agree all year. For most of Europe and North America they do not:

Europe/Berlin   in January  →  +01:00
Europe/Berlin   in July     →  +02:00

If you store only the offset a user had when they signed up, you will be an hour wrong for half the year. Store the IANA zone name (Europe/Berlin, America/New_York). Abbreviations are worse than offsets: "IST" is used for India, Ireland and Israel, and "CST" for zones on three continents.

Days that are not 24 hours long

In a zone with daylight saving time, one day a year has 23 hours and one has 25. Two consequences follow.

So "add one day" and "add 24 hours" are different operations. Adding 24 hours to 09:00 on the day before a clock change gives 08:00 or 10:00 the next day. For anything a person thinks of as a calendar step (tomorrow, next week, the same time next month), do calendar arithmetic in the user's zone, not arithmetic on seconds.

Past events in UTC, future events in local time

"Always store UTC" is good advice for things that have happened. It is wrong for things that are planned.

Suppose someone books a meeting for 10:00 on a date next year in São Paulo. If you convert that to UTC today and store only the result, you have baked in today's rules. Governments change daylight saving rules, sometimes with only a few weeks of notice (Brazil abolished it in 2019). After a change, your stored UTC instant converts back to 09:00 or 11:00, and the meeting is wrong, although the person never changed anything.

For future events, store what the person actually meant: the local date and time, plus the zone name. Convert to an instant when you need one, using the rules current at that moment.

Strings: be explicit about the zone

ISO 8601 is the format to use for exchanging times as text, and a good string states its offset:

2026-03-14T09:30:00Z          ← UTC
2026-03-14T15:00:00+05:30     ← the same instant, written for India

Trouble starts when the offset is missing. Different systems make different guesses, and JavaScript makes two different guesses depending on the shape of the string:

new Date("2026-03-14")           // treated as UTC midnight
new Date("2026-03-14T00:00:00")  // treated as local midnight

For a user west of Greenwich, the first one displays as the evening of 13 March. This is the cause of the familiar "the date is one day off" bug. Fix it at the source: send an explicit offset for instants, and treat things that are truly just dates, such as a birthday, as dates with no time at all rather than as midnight somewhere.

"Today" depends on who is asking

A daily report, a "posted today" label and a daily usage limit all need a definition of when a day starts. Midnight UTC is early morning in Asia and the previous evening in the Americas. Decide on purpose whether a day means the user's day, the business's day or the UTC day, and write the choice down. Query by half-open ranges, start inclusive and end exclusive, so that an event at exactly midnight is counted once.

The year 2038

A signed 32-bit integer can count seconds up to 2,147,483,647, which is 03:14:07 UTC on 19 January 2038. One second later it overflows and wraps to 1901. Modern systems use 64-bit time, but 32-bit fields survive in old database columns, file formats and embedded devices. The problem already affects software that handles dates beyond 2038 today, such as long-term contracts and certificate expiry dates. If you define a column for timestamps, make it 64 bits.

Rules that prevent most of this

  1. Store past instants as UTC timestamps or UTC date-times, and know the unit.
  2. Store future, human-scheduled times as local time plus an IANA zone name.
  3. Convert to local time only at the edge, when displaying to a person.
  4. Never guess a zone: put the offset in every string you emit, and reject strings without one where you can.
  5. Use a time zone library and keep its zone data updated; never hand-roll offset arithmetic.
  6. Test with a zone that has daylight saving, a zone with a half-hour offset, and a date on each side of a clock change.

Tools used in this guide

More guides