The Network Time Protocol uses timestamps measured in seconds since January 1, 1900 (UTC) — a 64-bit format with 32 bits for seconds and 32 bits for fractional seconds. This converter handles the seconds part for both directions.
The Network Time Protocol (NTP) is the standard for synchronizing computer clocks over the internet. NTP packets include a 64-bit timestamp consisting of two 32-bit halves:
1900-01-01 00:00:00 UTCYou'll encounter NTP timestamps when:
The 32-bit seconds field will overflow on February 7, 2036 at 06:28:16 UTC — earlier than the Unix Y2038 boundary. NTP version 4 partially addresses this by using "eras," but legacy NTP v3 and earlier implementations will fail at this boundary.
unix_seconds = ntp_seconds - 2208988800
ntp_seconds = unix_seconds + 2208988800
The constant 2208988800 is the number of seconds between 1900-01-01 00:00:00 UTC and 1970-01-01 00:00:00 UTC (exactly 70 years including 17 leap days).
from datetime import datetime, timezone
NTP_OFFSET = 2208988800
def ntp_to_datetime(ntp_seconds: int) -> datetime:
return datetime.fromtimestamp(ntp_seconds - NTP_OFFSET, tz=timezone.utc)
def datetime_to_ntp(dt: datetime) -> int:
return int(dt.timestamp()) + NTP_OFFSET
const NTP_OFFSET = 2208988800;
function ntpToDate(ntpSec) {
return new Date((ntpSec - NTP_OFFSET) * 1000);
}
function dateToNtp(date) {
return Math.floor(date.getTime() / 1000) + NTP_OFFSET;
}
#include <stdint.h>
#include <time.h>
#define NTP_TIMESTAMP_DELTA 2208988800ULL
time_t ntp_to_unix(uint32_t ntp_seconds) {
return (time_t)(ntp_seconds - NTP_TIMESTAMP_DELTA);
}
uint32_t unix_to_ntp(time_t unix_seconds) {
return (uint32_t)(unix_seconds + NTP_TIMESTAMP_DELTA);
}
The lower 32 bits of an NTP timestamp represent fractional seconds in 2-32 units. To convert to a decimal fraction:
fractional_seconds = ntp_fraction / 4294967296.0
The resolution is about 233 picoseconds per unit — far more precision than any practical computer clock can provide. In typical use you can ignore the fractional part entirely; it only matters for high-precision time sync.