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.
Reading a seconds value as milliseconds lands you in January 1970, while reading milliseconds as seconds sends you far into the future.
Negative values are allowed and represent moments before the epoch.
When you receive a number from another system, check the documentation for the unit first and use the digit count if the documentation is silent.
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.
Korea Standard Time is nine hours ahead of UTC, so converting the same number gives a clock reading nine hours later than UTC.
Regions with daylight saving time change their offset twice a year, so simply adding or subtracting hours can be wrong.
Even if you only need a date, remember that near midnight the date differs by time zone.
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.
Exactly 24 hours later and the same time tomorrow are different values on the day daylight saving time changes.
To find the difference between two moments, convert both to timestamps in the same unit and subtract.
Some systems store fractions of a second, so decide whether you truncate or round when converting to whole 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.
Features that look into the future, such as expiry dates or maturity calculations, can be affected first, so they are worth checking.
An unsigned 32-bit value extends the limit to the year 2106 but loses the ability to represent moments before 1970.
Enter 2147483647 in the Unix timestamp converter on this site and see the boundary moment for yourself.
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.
For token expiry and schedules, where error matters, confirm that the server clock is synchronized automatically.
When writing times to logs, record both a human-readable form and the number so tracing later is easy.
For a number of unclear origin, read it as both seconds and milliseconds in the converter and choose the one that makes sense.