What is Unix Time?
Unix time represents an instant relative to 1970-01-01 00:00:00 UTC, without counting leap seconds. Seconds are the basic unit, but APIs use integer or fractional seconds, milliseconds, and other representations. It is common in operating systems, logs, databases, and APIs.
Overview of Unix Time
POSIX seconds since the epoch account for each day as 86,400 seconds. This is not a count of every physically elapsed second including leap seconds.
Key Points
- Epoch: January 1, 1970 00:00:00 UTC = 0
- Unit: usually seconds (sometimes milliseconds, microseconds, or nanoseconds)
- The same instant has the same Unix timestamp in Tokyo and New York. A time zone is used when converting to or from local date/time.
Epoch, UTC, and Time Zones
1735689600 seconds represents 2025-01-01 00:00:00 UTC, or 09:00:00 in JST. A zone or offset is also needed when converting local input into a timestamp.
Common Misunderstandings
- Do not add nine hours to create a “JST Unix timestamp”. That changes the instant.
- Daylight Saving Time (DST) is handled by local time zone rules, not by Unix Time itself.
Representation (Seconds, Milliseconds, Nanoseconds)
Check both unit and numeric type. Python time.time() returns fractional seconds, while JavaScript Date.now() returns milliseconds. More digits do not imply a more accurate clock.
Typical Units
- 2025-01-01 00:00:00 UTC is 1735689600 seconds or 1735689600000 milliseconds.
- Microseconds scale seconds by 10^6; nanoseconds by 10^9. Present-day epoch nanoseconds exceed JavaScript Number safe-integer precision; consider BigInt or strings.
Quick Rule of Thumb
- Ten versus thirteen digits is only a present-era hint. Older dates, negative values, and fractions exist; use the API contract instead of automatic digit-count detection.
Conversion Examples
These examples convert the fixed value 1735689600. Use GNU date in a POSIX-like shell, Python 3, a Node.js or browser JavaScript console, and PHP CLI respectively.
Convert with GNU date
Options differ from the date command bundled with macOS. Both lines display the same instant.
date -u -d @1735689600 +"%Y-%m-%dT%H:%M:%SZ"
TZ=Asia/Tokyo date -d @1735689600 +"%Y-%m-%dT%H:%M:%S%:z"
Convert with Python
from datetime import datetime, timezone
print(datetime.fromtimestamp(1735689600, tz=timezone.utc).isoformat())
# 2025-01-01T00:00:00+00:00
Convert with JavaScript
const seconds = 1735689600;
console.log(new Date(seconds * 1000).toISOString());
// 2025-01-01T00:00:00.000Z
Convert with PHP
<?php
$dt = new DateTimeImmutable("@1735689600");
echo $dt->setTimezone(new DateTimeZone("Asia/Tokyo"))
->format("Y-m-d\\TH:i:sP"), PHP_EOL;
// 2025-01-01T09:00:00+09:00
Check SQL time-zone behavior
MySQL FROM_UNIXTIME displays in the session time zone. PostgreSQL to_timestamp returns timestamp with time zone, whose display follows the session TimeZone. Check connection settings instead of assuming UTC output.
Pitfalls (Year 2038, Leap Seconds, Time Zones)
Be aware of these pitfalls when dealing with Unix Time in real-world systems.
Year 2038 Problem (32-bit time_t)
- The signed 32-bit maximum, 2147483647, represents 2038-01-19 03:14:07 UTC. It is still representable; the next second, 03:14:08, is out of range.
- A 64-bit CPU or OS does not rule out 32-bit seconds in a database column, protocol, or application. Inspect each time type and range.
Leap Seconds
- POSIX time does not count leap seconds. Clock behavior around a leap second depends on the synchronization method and implementation.
- For elapsed-duration measurements, prefer a monotonic clock such as Python time.monotonic over wall-clock timestamps that can be adjusted backwards.
Time Zones
- Use UTC-based values for completed-event instants. For a recurring “9 a.m.” schedule, also retain local time and the regional time zone.
- Watch out for unit mismatches (seconds vs milliseconds) in APIs and DBs
Best Practices
Consistency across logging, APIs, databases, and UI helps prevent unit/time-zone related bugs.
Recommendations
- Store completed events in an unambiguous instant representation, such as epoch seconds or an offset-bearing timestamp.
- Document interface specs: unit (sec/ms) and time zone
- Include boundary cases in testing (leap years, month-end, midnight, unit mismatches)
- For observability, log both UTC and local time when possible
FAQ
Q. Is Unix Time in JST?
- There is no separate JST timestamp value. Convert the same Unix timestamp to a UTC or JST date/time.
Q. What is the difference between 10-digit and 13-digit values?
- This often indicates seconds versus milliseconds for present-day values, but digits alone cannot determine the unit.
Q. Does the Year 2038 problem affect me?
- It affects components using signed 32-bit epoch seconds. A 64-bit OS alone is not proof that every component is safe.
Where Unix Time is Used
Examples
- Timestamps in server logs and monitoring
- JWT issued-at (iat) and expiry (exp), using the specified unit. Reading a value does not establish that its signature is valid.
- Database event times, sorting, and range queries
- Frontend relative time display (e.g., "5 minutes ago")
Specifications and related reading
RFC 7519 NumericDate / GNU date / Python datetime / Python time / ECMAScript Date / PHP DateTime / MySQL / PostgreSQL
Storage and time zones / Time zone list