🌐 English

Home › Guides

Unix timestamps: units, time zones, and the 2038 problem

A Unix timestamp expresses a moment as one number, which is wonderfully simple. Yet confusion between seconds and milliseconds, or between time zones, often shifts results by hours or even decades. This guide covers the core ideas and the usual traps.

The reference point and the units

Unix time counts the time elapsed since 00:00:00 UTC on January 1, 1970, a moment called the epoch. For example 0 is the epoch itself, and 86400 is exactly one day later.

The same moment has different lengths depending on the unit. Seconds are currently about 10 digits, milliseconds about 13, and some systems use microseconds or nanoseconds. The number of digits is usually enough to guess the unit.

Time zones are not inside the number

A timestamp is always counted from UTC, so it carries no time zone. The number is identical whether it is read in Seoul or New York; only when showing it to a person do you convert it to local time.

The safest approach is to store timestamps or UTC values and convert to the user time zone only for display. If you store a local time string, you often lose the ability to know which zone it was in.

Leap seconds and calendar traps

Unix time always treats a day as 86400 seconds. Astronomical leap seconds are therefore not counted separately, and depending on how a system handles them the moment may repeat or be stretched.

In practice the length of months and years matters more than this. Assuming a month has 30 days and multiplying by seconds lets errors accumulate. For month-based arithmetic, use a tool that knows calendar rules instead of adding raw numbers.

The year 2038 problem

If time is stored in a signed 32-bit integer, the maximum is 2147483647, which is 03:14:07 UTC on January 19, 2038. One second later the value overflows to a negative number and is read as a date in 1901. This is the 2038 problem.

The fix is to store time in 64 bits. Most modern systems already do, but older devices, embedded equipment, file formats and database column definitions may still use 32 bits.

Habits that prevent bugs

Store time in UTC and convert only at the edges, on input and on output; this single rule removes most problems. Naming fields with meaning and unit, such as _at or _ms, helps a great deal too.

Clocks on different machines drift slightly. If you must decide order by comparing times from two servers, allow a tolerance for error or number events at a single place.