toNanoseconds
Signature
Section titled “Signature”toNanoseconds(isoString: string): bigintConvert an ISO 8601 instant string to nanoseconds since the Unix epoch.
- Requires an offset designator (
Z,±HH:MM), asTemporal.Instantdoes — date-only and offset-less strings are rejected. - Accepts a bracketed IANA time zone annotation and a
[u-ca=...]calendar annotation. The calendar is ignored, asTemporal.Instant.fromignores it (an instant has no calendar). - The offset fixes the instant; a bracketed zone only resolves a rounded one. An offset written to the minute that is the bracketed zone’s sub-minute offset rounded (
-00:45[Africa/Monrovia], for −00:44:30) names the instant the zone gives, asTemporal.ZonedDateTime.fromreads it, so a zoned string Temporal wrote is read back as the instant it was written for, except a wall time repeated inside a sub-minute offset change, which reads as its first pass (seeisValidInstant). Otherwise the bracket is not checked: a zone that does not exist, or that disagrees with the offset, changes nothing. - ISO 8601 extended format before any annotation:
<date>T<time>, thenZor±HH:MM[:SS[.fraction]]. Basic format, a space or lower-casetseparator, a lower-casezand an hour-only time or offset return0n, althoughTemporal.Instant.fromreads them. The offsets make this wider thanisValidUtc, which requiresZ. - Rejects leap seconds (
"2016-12-31T23:59:60Z"), which Temporal would silently clamp — in every separator and format variant it accepts,"20161231 235960Z"included. - Returns
bigint, notnumber: nanoseconds since the epoch passedNumber.MAX_SAFE_INTEGERin April 1970, so anumbercannot hold them. - Returns
0non invalid input.0nis also the epoch itself — useisValidInstant, which accepts exactly this grammar, when the two must be told apart. NotisValidUtc: it gates on GMT’s stricter<date>T<time>Zshape and returnsfalsefor the offsets and bracketed zones this function accepts, so validating with it discards valid input.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
isoString | string | ISO 8601 instant string (e.g. “2024-03-10T12:00:00.123456789Z”) |
Returns
Section titled “Returns”nanoseconds since the Unix epoch as a bigint, or 0n on invalid input
Examples
Section titled “Examples”import { toNanoseconds } from "@northguild/gmt/precision/convert";
toNanoseconds("1970-01-01T00:00:00Z") // 0n