🚀 Unix Timestamp & Epoch Time: Conversion + 2038 Risk12 min read2,334 words

🚀 Unix Timestamp & Epoch Time: Conversion + 2038 Risk

Learn Unix timestamp & epoch time conversions (seconds/milliseconds) across 5 environments. Avoid timezone, precision and the 2038 problem—see examples.

Unix timestampepoch timetimestamp conversionUnix time milliseconds2038 problem
🚀 Unix Timestamp & Epoch Time: Conversion + 2038 Risk

Unix Timestamps: How They Work and How to Use Them

A timestamp looks simple: one integer for a moment in time. In practice, that integer might be seconds or milliseconds (or something even smaller). It might get parsed with the wrong timezone. It might not even fit in the type or field you're using. Those are the kinds of issues that turn into date bugs - annoying to reproduce, easy to ship.

Epoch time, also called Unix time or a Unix timestamp, counts time from a fixed starting point: 00:00:00 UTC on January 1, 1970. The idea is handy because a number is easy to compare, store, and transmit. But that number isn't a complete date-time format. By itself, it doesn't tell you the unit or how you should display it.

This guide explains what epoch time means, how to convert Unix timestamps in common programming environments, and what to check before storing or using them. It also covers the edge cases that tend to show up in real systems: unit mismatches, timezones, leap seconds, negative values, and the 2038 problem.

What does epoch time mean?

Epoch time is a count of time units from the Unix Epoch: midnight UTC on January 1, 1970. In the common Unix/POSIX convention, that count is expressed in seconds. For example, 1700000000 corresponds to 2023-11-14 22:13:20 UTC.

A Unix timestamp doesn't carry a timezone. It identifies an instant. Timezone only matters when you convert that instant into a human-readable clock time. The same timestamp can show up as different local times in New York, London, or Tokyo - but it still refers to the same underlying moment.

You'll also see these terms used interchangeably: epoch time, Unix time, Unix timestamp, and POSIX time. In day-to-day development, they usually mean "seconds since the Unix Epoch." Still, "epoch" might also show up as milliseconds or nanoseconds, so the term alone doesn't guarantee the unit. Check the interface, the schema, or the field name.

Why leap seconds are treated differently

The mental model most people start with is "seconds since 1970." It's close, but it's not the full physical picture. POSIX time treats each day as exactly 86,400 seconds and doesn't assign a separate timestamp to leap seconds. The Linux time(2) manual and Python docs describe this limitation too.

The practical trade-off is that date arithmetic stays predictable in software. If you're just converting between days and seconds, you can divide a difference by 86,400 to get a POSIX day count. But across a leap second, epoch differences won't perfectly match physical elapsed time. Most apps don't need that level of precision anyway. Scientific and metrology systems may use a different time scale designed for it.

A few reference values

These examples are handy for sanity-checking conversions:

Epoch seconds UTC date and time What it shows
0 1970-01-01 00:00:00 The Unix Epoch
86400 1970-01-02 00:00:00 One POSIX day later
1000000000 2001-09-09 01:46:40 One billion seconds
1234567890 2009-02-13 23:31:30 A familiar sequence of digits
1700000000 2023-11-14 22:13:20 A practical test value
2147483647 2038-01-19 03:14:07 Maximum signed 32-bit integer

All values above are UTC. If a command-line tool prints a different clock time, check the machine's timezone before concluding the conversion is wrong.

How do you convert a Unix timestamp?

Converting a Unix timestamp is often a one-liner. The tricky part is picking the right unit and making timezone handling explicit. Below are examples for converting "current time" and known values in a few common environments.

Shell with GNU Coreutils

On systems with GNU Coreutils, date +%s prints the current epoch time in seconds. The -u flag requests UTC output when converting a value:

# Current epoch time in seconds

date +%s

# Convert seconds to a UTC date

date -u -d @1700000000

# Tue Nov 14 22:13:20 UTC 2023

If you start with milliseconds, divide by 1,000 before passing it as seconds:

date -u -d @$((1700000000000 / 1000))

Note: this relies on GNU date; other Unix-like systems may use different options.

Python

Python's time.time() returns seconds as a floating-point value. If you need an integer nanosecond value, use time.time_ns(). For display, pass a UTC timezone explicitly to datetime.fromtimestamp:

import time

from datetime import datetime, timezone

now_seconds = int(time.time())

now_nanoseconds = time.time_ns()

value = datetime.fromtimestamp(1700000000, tz=timezone.utc)

print(value.isoformat())

# 2023-11-14T22:13:20+00:00

Explicit timezone handling avoids guessing what the host machine is set to.

JavaScript

JavaScript's Date.now() returns milliseconds, not seconds. The Date constructor also expects milliseconds, so multiply seconds by 1,000. When converting to whole seconds, divide and floor:

const nowMilliseconds = Date.now();

const nowSeconds = Math.floor(nowMilliseconds / 1000);

console.log(new Date(1700000000 * 1000).toISOString());

// 2023-11-14T22:13:20.000Z

If you pass a seconds value directly to new Date(), JavaScript treats it as milliseconds and you'll get a date close to January 1970. That mismatch is usually the first sign you've got the units wrong.

Go

Go's time.Time has separate methods for common units. Unix() returns seconds; UnixMilli() returns milliseconds. The Go docs also make it clear that Unix() is measured from January 1, 1970 UTC, regardless of the time's location:

package main

import (

"fmt"

"time"

)

func main() {

now := time.Now()

fmt.Println(now.Unix(), now.UnixMilli())

fmt.Println(time.Unix(1700000000, 0).UTC())

// 2023-11-14 22:13:20 +0000 UTC

}

PostgreSQL

PostgreSQL can convert between timestamps and epoch seconds. For a timestamptz value, EXTRACT(EPOCH FROM ...) returns seconds since the Unix Epoch. to_timestamp converts epoch seconds back into a timestamp:

SELECT EXTRACT(EPOCH FROM TIMESTAMPTZ '2023-11-14 22:13:20+00');

SELECT to_timestamp(1700000000);

Be careful about whether the input is timestamp or timestamptz. A timestamp without a timezone doesn't point to a unique instant until you decide which timezone that clock reading belongs to.

How can you tell seconds from milliseconds?

Unit mismatches are probably the most common timestamp bug. The same instant can show up as values with very different magnitudes, and many APIs won't reject the wrong unit just because the number "looks weird."

Unit Typical digits today Example for 2023-11-14 22:13:20 UTC Common examples
Seconds 10 1700000000 POSIX time(), Go Unix()
Milliseconds 13 1700000000000 JavaScript Date.now(), Go UnixMilli()
Microseconds 16 1700000000000000 Go UnixMicro()
Nanoseconds 19 1700000000000000000 Python time_ns(), Go UnixNano()

Each step changes the value by a factor of 1,000. Converting seconds to milliseconds means multiply by 1,000; converting milliseconds to seconds means divide by 1,000. Ideally, do this at a clear boundary instead of sprinkling conversions throughout your codebase.

Use magnitude as a debugging clue, not a promise

For dates around today, digit count is a useful first check: 10 digits usually means seconds and 13 usually means milliseconds. But it's a heuristic, not a format guarantee. It can mislead for older dates, negative timestamps, or systems that represent time on a different scale. In a real system, you should get the unit from a documented contract - not from guesswork.

If you control an API, make the unit explicit in the name or schema. For example, created_at_epoch_seconds is less ambiguous than created_at. If you can, use a standard date-time type at boundaries where humans or multiple systems need to understand the value. ISO 8601 strings with an explicit offset (like Z for UTC) communicate both the date-time and its timezone context.

Check precision and numeric limits

Python recommends time.time_ns() when you need nanoseconds without the precision issues that come with floating-point values. JavaScript has a related gotcha: the normal Number type can't exactly represent every large integer. A 19-digit nanosecond timestamp is bigger than JavaScript's safe-integer range, so you'll need BigInt or store it as a string if you must keep it exact.

And don't upgrade precision just because your language offers it. Pick a resolution based on what you actually need, what your storage can support, and what downstream consumers can handle. If you only care about a business event to the nearest second or millisecond, extra precision can add complexity without delivering real value.

Which timestamp edge cases cause production bugs?

Most Unix timestamp problems aren't caused by the definition of the epoch itself. They come from assumptions around it: that values always fit, that clocks move steadily, and that a date string without a timezone has an obvious meaning. These are the checks worth building into a review.

Timezones belong in formatting, not in the epoch value

A timestamp points to the same instant everywhere. For example, epoch 0 is 1970-01-01 00:00:00 UTC. In New York, it appears as 1969-12-31 19:00:00 EST. In India, it appears as 1970-01-01 05:30:00 IST. The numeric value didn't change - only the display did.

A common failure happens earlier: an app parses a "local time" string as if it were UTC, or assumes the server's timezone matches the user's. If an input is meant to be local time, attach the intended timezone before converting it. For data exchanged between services, an ISO 8601 value with Z or an explicit offset reduces ambiguity a lot.

Dates before 1970 and negative timestamps

Many tools accept negative epoch values. GNU date, for instance, can treat -1 as 1969-12-31 23:59:59 UTC, and PostgreSQL supports negative epoch values for earlier timestamps. But support varies. POSIX doesn't define how negative values relate to UTC the same way it does for standard timestamps, and Python notes platform limitations for dates before the epoch and far in the future.

If your system handles historical records, test the real runtime, database, and operating systems you plan to deploy. And if a "date" is really just a calendar day (not a precise instant), storing something like 1965-04-12 can make more sense than storing midnight as an epoch-derived value.

The 2038 problem and storage width

A signed 32-bit integer tops out at 2147483647, which corresponds to 2038-01-19 03:14:07 UTC. The next second won't fit. Systems that store seconds in 32-bit fields can overflow or fail around that point. An unsigned 32-bit field lasts until 2106, but it still can't represent negative values.

Modern 64-bit environments usually avoid this immediately, but older database schemas, file formats, embedded devices, and protocol fields can quietly preserve a 32-bit limit long after the main application code has moved on. Audit the whole path: source type, serialization format, database column, API contract, and any downstream consumer. Updating one app won't fix a narrow field inside a file format.

Clocks can jump; timestamps aren't stopwatches

Epoch time is based on a wall clock. The system clock can be adjusted, which means subtracting two epoch readings is not always a reliable way to measure how long something took. For elapsed-time measurement inside your program, use the language's monotonic clock. Use wall-clock timestamps for event times and records that need to be compared across systems.

Finally, numeric ranges vary by language. JavaScript Date supports a bounded millisecond range and returns an invalid date outside it. Go's UnixNano() uses a signed 64-bit integer and is undefined outside a documented date range (roughly 1678 to 2262). Don't assume "bigger unit" automatically means "safer" - read the runtime docs.

Conclusion

A Unix timestamp is a compact way to identify an instant, not a self-describing date. If you want to use it safely, document the unit, make timezone assumptions explicit, store it in something that can hold the needed range, and use a monotonic clock for elapsed-time measurements.

If you want a simple rule: treat timestamps like values with a contract. Verify seconds versus milliseconds at every boundary, test the limits that matter for your data, and only convert to local time when presenting to a user.

Frequently Asked Questions

Usually, yes. Both commonly refer to the count of time from January 1, 1970 UTC, with leap seconds excluded. The main source of ambiguity is the unit: developers often mean seconds by “Unix timestamp,” while “epoch time” may refer to seconds, milliseconds or a finer resolution. Confirm the unit before using the value.
A Unix timestamp identifies an instant relative to the UTC-based Unix Epoch, but it does not contain a timezone setting. UTC is used when expressing the timestamp as a date and time. Local timezones affect how that instant is displayed, not the underlying timestamp value.
Check the API documentation, schema or field name first. For dates around the present, 10 digits usually indicates seconds and 13 usually indicates milliseconds, but digit count is only a debugging clue. Historical values, negative timestamps and other units can defeat the heuristic.
A signed 32-bit integer cannot represent Unix seconds beyond 2038-01-19 03:14:07 UTC. Systems that still store time in that kind of field may overflow or fail after that point. Check database columns, protocols, file formats and older devices, not just the main application’s time type.

Related articles