toNtpTimestamp
Signature
Section titled “Signature”toNtpTimestamp(isoString: string): bigintConvert 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
0nagain. The era is not part of the value and is not recoverable from it —fromNtpTimestamptakes 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
fromNtpTimestampis exact for any nanosecond-precision instant. - Returns
0non invalid input.0nis also the NTP epoch itself (1900-01-01, era 0) and every era boundary after it — validate the string first withisValidInstantwhen 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.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
isoString | string | ISO 8601 instant string (e.g. “2024-03-10T12:00:00Z”) |
Returns
Section titled “Returns”64-bit NTP timestamp as a bigint, or 0n on invalid input
Examples
Section titled “Examples”import { toNtpTimestamp } from "@northguild/gmt/precision/convert";
toNtpTimestamp("1900-01-01T00:00:00Z") // 0n — the NTP epoch