UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

55 lines • 3.08 kB
import { Temporal } from "./temporal.js"; /** * Parse a `±HH:MM[:SS]` UTC offset to the nanoseconds local time runs ahead of UTC. * * Temporal exposes no offset parser of its own — `offsetNanoseconds` is only ever read off a * `ZonedDateTime`, which needs a zone GMT does not have here. So the offset is read the one * way Temporal will read it: as the offset half of an instant string anchored at the epoch. * `1970-01-01T00:00:00-04:00` *is* `1970-01-01T04:00:00Z`, so the epoch nanoseconds of that * instant are the offset with its sign flipped. The regex proves the shape and Temporal does * the parsing, so no offset digits are ever sliced by hand. * * @param offset `±HH:MM` or `±HH:MM:SS` offset string * @returns nanoseconds ahead of UTC (negative west of it), or null when `offset` is not one * * @example parseUtcOffsetNanoseconds("-04:00") // -14400000000000n * @example parseUtcOffsetNanoseconds("+05:45") // 20700000000000n * @example parseUtcOffsetNanoseconds("Z") // null — a designator, not an offset */ export declare function parseUtcOffsetNanoseconds(offset: string): bigint | null; /** * Format nanoseconds ahead of UTC as a canonical `±HH:MM[:SS]` offset string. * * The inverse of `parseUtcOffsetNanoseconds`, and the canonicaliser both halves of the * offset-instant pair route through — `+05:45:00` and `+05:45` name one offset, and only one * of them is what GMT stores. * * - Emits the `:SS` field only when the offset is not a whole number of minutes, matching * `Temporal.ZonedDateTime.prototype.offset`. * - Returns null for an offset finer than a second, or at or beyond ±24 hours — neither is a * value any zone or standard that stores this pair has ever used. * * @param offsetNanoseconds nanoseconds local time runs ahead of UTC * @returns `±HH:MM[:SS]` offset string, or null when the value is not one GMT stores * * @example formatUtcOffset(-14400000000000n) // "-04:00" * @example formatUtcOffset(0n) // "+00:00" * @example formatUtcOffset(-2670000000000n) // "-00:44:30" * @example formatUtcOffset(1n) // null — finer than a second */ export declare function formatUtcOffset(offsetNanoseconds: bigint): string | null; /** * The offset an instant string's own digits were written in, recovered without slicing them. * * Recovers the offset written in the body from the distance between two wall clocks: the digits * as written, and the same instant read in UTC. A `Z` or `z` body is written in UTC, so its offset is * `+00:00`. * * @param instant the instant `body` names * @param body the ISO 8601 string with any bracketed annotations removed (`isoStringBody`) * @returns the written offset as `±HH:MM[:SS]`, or null when it is finer than a second * * @example writtenOffset(Temporal.Instant.from("2024-07-15T12:00:00+05:30"), "2024-07-15T12:00:00+05:30") // "+05:30" * @example writtenOffset(Temporal.Instant.from("2024-07-15T12:00:00Z"), "2024-07-15T12:00:00Z") // "+00:00" */ export declare function writtenOffset(instant: Temporal.Instant, body: string): string | null;