Skip to content

isValidInstant

isValidInstant(value: string): boolean

Validate whether a string is an ISO 8601 instant string the precision/ namespace accepts.

  • The predicate toNanoseconds was missing. toNanoseconds returns 0n for both the epoch and a rejected string, and the only public validator close enough to reach for — isValidUtc — is narrower than this grammar: it gates on GMT’s stricter <date>T<time>Z shape and rejects the offsets and bracketed zones toNanoseconds parses. Validating with it discards valid input. This accepts exactly what toNanoseconds does, so the two never disagree.
  • Requires an offset designator (Z, ±HH:MM); a bracketed IANA zone may follow, but a bracket alone is not enough.
  • 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, as Temporal.ZonedDateTime.from reads it, so a zoned string Temporal wrote is read back as the instant it was written for, at the first and last instants of the range too, where the written offset’s own reading can fall just outside it (-271821-04-19T23:58:45-00:01[Europe/London] is the first instant). One exception, which is Temporal’s own: a wall time repeated inside a sub-minute offset change, written with a minute-rounded offset, reads as its first pass (for example 1952-10-15T23:59:59-11:20[Pacific/Niue], where the zone moved from −11:19:40 to −11:20:00, is the pass at −11:19:40). Otherwise the bracket is not checked: a zone that does not exist, or that disagrees with the offset, changes nothing. Validity never depends on the bracket.
  • ISO 8601 extended format before any annotation: <date>T<time>, then Z or ±HH:MM[:SS[.fraction]]. Basic format, a space or lower-case t separator, a lower-case z and an hour-only time or offset return false, although Temporal.Instant.from reads them.
  • Rejects leap seconds in every separator and format variant.
  • Reads RFC 9557 annotations as Temporal.Instant.from does: a calendar or elective annotation is ignored, and an unknown critical one ([!foo=bar]) is rejected.
  • Returns false for non-strings and empty strings.
ParameterTypeDescription
valuestringcandidate ISO 8601 instant string

boolean indicating whether toNanoseconds will parse it

import { isValidInstant } from "@northguild/gmt/precision/validate";
isValidInstant("2024-03-10T12:00:00Z") // true
output 

precision/validate/isValidInstant.ts