Skip to content

toNtpTimestamp

toNtpTimestamp(isoString: string): bigint

Convert an ISO 8601 instant string to a 64-bit NTP timestamp (RFC 5905).

  • The result packs an unsigned 32-bit count of seconds since 1900-01-01 in the high bits and a 32-bit fraction in the low bits, where one fractional unit is 2^-32 s (~233 ps).
  • The seconds field wraps. It is unsigned 32-bit, so the timestamp rolls into a new era every 2^32 seconds (~136.19 years). Era 0 runs from 1900-01-01 to 2036-02-07T06:28:15Z; the next second is 0n again. The era is not part of the value and is not recoverable from it — fromNtpTimestamp takes it as an explicit argument. This is the wire format’s own ambiguity, not a GMT limitation, so instants outside era 0 wrap here rather than being rejected.
  • Sub-nanosecond precision is not invented: nanoseconds floor onto the 2^-32 s grid, so a round trip through fromNtpTimestamp is exact for any nanosecond-precision instant.
  • Returns 0n on invalid input. 0n is also the NTP epoch itself (1900-01-01, era 0) and every era boundary after it — validate the string first with isValidInstant when the two must be told apart. RFC 5905 §6 also makes a zero timestamp “a special case representing unknown or unsynchronized time”, so the sentinel reads as unsynchronized to an NTP peer.
ParameterTypeDescription
isoStringstringISO 8601 instant string (e.g. “2024-03-10T12:00:00Z”)

64-bit NTP timestamp as a bigint, or 0n on invalid input

import { toNtpTimestamp } from "@northguild/gmt/precision/convert";
toNtpTimestamp("1900-01-01T00:00:00Z") // 0n — the NTP epoch
output 

precision/convert/toNtpTimestamp.ts