NTP timestamp converter.
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.
What is an NTP timestamp?
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:
- First 32 bits: integer seconds since
1900-01-01 00:00:00 UTC - Second 32 bits: fractional seconds (each unit ≈ 233 picoseconds)
You'll encounter NTP timestamps when:
- Debugging NTP synchronization issues in production
- Reading packet captures of NTP traffic (Wireshark, tcpdump)
- Working with PTP (Precision Time Protocol) or similar time-sync formats
- Analyzing logs from systems that store time in NTP format
The Year 2036 problem
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.
Conversion formulas
NTP → Unix seconds
unix_seconds = ntp_seconds - 2208988800
Unix seconds → NTP
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).
Code examples
Python
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
JavaScript
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;
}
C (parsing an NTP packet)
#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 fractional part (for the curious)
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.
Last updated: