formatNanoseconds
Signature
Section titled “Signature”formatNanoseconds(nanoseconds: bigint): stringFormat nanoseconds since the Unix epoch as a decimal string, for JSON and other text transports.
JSON.stringifythrows aTypeErroron abigint, so a nanosecond timestamp cannot go into a JSON payload as-is. This is the bridge every consumer needs; do not hand-roll it.- The output is a canonical decimal integer: an optional
-, then digits, no leading zeros, no exponent, no separators.parseNanosecondsis its exact inverse. - Put the result in JSON as a string, not a number. It is itself only the value —
{ observedAt: formatNanoseconds(n) }serialises to{"observedAt":"1710072000123456789"}. Emitted unquoted it is a JSON number, andJSON.parserounds it back through a double to1710072000123456800— the exact loss this namespace exists to prevent. - Rejects values outside the range
Temporal.Instantcan represent (±8_640_000_000_000_000_000_000n), so anything formatted here can be read back and rendered. - Returns "" on invalid input.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
nanoseconds | bigint | nanoseconds since the Unix epoch (bigint) |
Returns
Section titled “Returns”decimal string form of the value, or "" on invalid input
Examples
Section titled “Examples”import { formatNanoseconds } from "@northguild/gmt/precision/format";
formatNanoseconds(1710072000123456789n) // "1710072000123456789"