Skip to content

parseNanoseconds

parseNanoseconds(value: string): bigint

Parse a decimal string produced by formatNanoseconds — typically one field of a JSON payload — back to nanoseconds since the Unix epoch.

  • Takes the string value, not JSON text: pass JSON.parse(payload).observedAt, not the payload itself. A quoted "\"123\"" is rejected, because the quotes are not part of a decimal integer.
  • Accepts only a canonical decimal integer — an optional - then digits with no leading zeros. Exponents, fractions, hex, separators, a + sign and surrounding whitespace are rejected rather than coerced, so a payload written by another language’s serialiser either reads back exactly or fails loudly.
  • Rejects values outside the range Temporal.Instant can represent (±8_640_000_000_000_000_000_000n).
  • Returns 0n on invalid input. 0n is also the epoch itself — isValidNanoPattern tells the two apart, and accepts exactly what this parses.
ParameterTypeDescription
valuestringdecimal string form of a nanosecond timestamp (e.g. “1710072000123456789”)

nanoseconds since the Unix epoch as a bigint, or 0n on invalid input

import { parseNanoseconds } from "@northguild/gmt/precision/parse";
parseNanoseconds("1710072000123456789") // 1710072000123456789n
output 

precision/parse/parseNanoseconds.ts