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.

57 lines • 3.16 kB
import { Disambiguation } from "../types/disambiguation.js"; import "../types/index.js"; import { ZoneFrame } from "./zoneFrame.js"; /** * A frame's offset as constant stretches over `[fromNs, toNs]`. `offsets[i]` holds from * `changes[i - 1]` (or `fromNs`) up to, not including, `changes[i]`; it includes * `frame.shiftNanoseconds`. Outside the range the first and last stretch are extended, so a * caller reads a time past `toNs` only after `extendTimeline`. */ export type OffsetTimeline = { frame: ZoneFrame; /** The first instant the timeline was built from. */ fromNs: bigint; /** The last instant whose offset is known to be complete. */ toNs: bigint; /** Epoch nanoseconds of each offset change, ascending. */ changes: bigint[]; offsets: bigint[]; }; /** * Build the timeline of `frame` over `[fromNs, toNs]`. A fixed-offset frame (an offset zone, * `UTC`, an offset with seconds) is one stretch and runs no search, unless `maxChanges` is given. * * With `maxChanges`, a range holding more changes than that returns `null` as soon as the * `maxChanges + 1`th is found, without searching further. * * Throws what Temporal throws for an instant outside its range; callers keep clear of it. */ export declare function offsetTimeline(frame: ZoneFrame, fromNs: bigint, toNs: bigint): OffsetTimeline; export declare function offsetTimeline(frame: ZoneFrame, fromNs: bigint, toNs: bigint, maxChanges: number): OffsetTimeline | null; /** * Make `timeline` complete up to `ns`, reading ahead so a walk that advances a day at a time * searches in chunks: it grows by at least its own length, up to three years. */ export declare function ensureTimeline(timeline: OffsetTimeline, ns: bigint): void; /** The offset in force at `ns`, in nanoseconds east of UTC. */ export declare function offsetOf(timeline: OffsetTimeline, ns: bigint): bigint; /** * TC39 `GetPossibleEpochNanoseconds`, read from the timeline: the instants whose wall clock is * `wallNs` (the wall clock read as UTC), ascending. A stretch contributes `wallNs - offset` when * that instant lies in the stretch; offsets differ by well under two days, so only the stretches * within two days either side can. */ export declare function possibleAt(timeline: OffsetTimeline, wallNs: bigint): bigint[]; /** * TC39 `GetEpochNanosecondsFor`: the instant a wall clock names, by * `DisambiguatePossibleEpochNanoseconds`. One candidate is the answer; two give the first * (`"compatible"`, `"earlier"`) or last (`"later"`); none moves the wall clock by the offset * change, back for `"earlier"` and forward otherwise. `undefined` where `"reject"` refuses. * Throws `RangeError` where a moved wall clock names nothing, which no zone's data produces. */ export declare function resolveWallNs(timeline: OffsetTimeline, wallNs: bigint, disambiguation: Disambiguation): bigint | undefined; /** * TC39 `GetStartOfDay`: the first instant of the local date `epochDay` days from 1970-01-01 - * its first candidate for midnight, else (midnight skipped) the change that ends the gap. */ export declare function startOfDayNs(timeline: OffsetTimeline, epochDay: number): bigint;