@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.
119 lines • 7.39 kB
TypeScript
import { Temporal } from "./temporal.js";
import { Disambiguation } from "../types/disambiguation.js";
import { Offset } from "../types/offset.js";
import { Overflow } from "../types/overflow.js";
import "../types/index.js";
/** Options accepted by `Temporal.ZonedDateTime.from`. */
export interface ZonedFromOptions {
disambiguation?: Disambiguation;
offset?: Offset;
overflow?: Overflow;
}
type ResolvedDisambiguation = NonNullable<Disambiguation>;
type ResolvedOffsetOption = NonNullable<Offset>;
/**
* The offset a zoned value carries: none (resolve the wall clock), a known offset, or an offset
* whose own instant lies outside the representable range (so no in-range candidate can match it).
*/
export type CarriedOffset = "absent" | "out-of-range" | {
nanoseconds: bigint;
};
/**
* `getTimeZoneTransition("next")` looks at most three years (366-day years) past its receiver;
* a `null` from further than this from the maximum is the polyfill's own horizon, not defect 2.
*/
export declare const NEXT_TRANSITION_HORIZON_NANOSECONDS: bigint;
/** Previous-transition steps walked back from the maximum instant; real zones need two or three. */
export declare const MAX_TRANSITIONS_BEFORE_MAXIMUM = 64;
/**
* 1847-01-01T00:00:00Z: `BEFORE_FIRST_DST` in js-temporal `lib/ecmascript.ts`. The polyfill never
* looks for a transition before it (defect 3).
*/
export declare const POLYFILL_TRANSITION_SEARCH_FLOOR = -3881520000000000000n;
/** RFC 9557 offset with a seconds field — TC39 `ToTemporalZonedDateTime` then matches exactly. */
export declare const UTC_OFFSET_WITH_SECONDS: RegExp;
/**
* True when the date part of a wall clock is inside the TC39 `CheckISODaysRange` limit, which
* Temporal applies to the local date (not only the instant) under `prefer` and `reject`.
*
* @example isWithinIsoDaysRange(Temporal.PlainDateTime.from("-271821-04-20T00:00:00")) // true
* @example isWithinIsoDaysRange(Temporal.PlainDateTime.from("-271821-04-19T23:59:00")) // false
*/
export declare function isWithinIsoDaysRange(wall: Temporal.PlainDateTime): boolean;
/** True for a date within `EDGE_WINDOW_DAYS` of either TC39 date limit. */
export declare function isNearRangeEdge(date: Temporal.PlainDate): boolean;
/** Offset time zones (`+10:00`) and `UTC` never consult tz data, so neither defect reaches them. */
export declare function isNamedTimeZone(timeZone: string): boolean;
/** TC39 `IsValidEpochNanoseconds`. */
export declare function isValidEpoch(epochNanoseconds: bigint): boolean;
/** UTC offset in force at an instant, reading the nearest limit for an instant outside the range. */
export declare function offsetAt(timeZone: string, epochNanoseconds: bigint): bigint;
/** TC39 `GetUTCEpochNanoseconds`: the wall clock read as if it were UTC (may lie outside the range). */
export declare function utcEpochNanoseconds(wall: Temporal.PlainDateTime): bigint;
/**
* TC39 `GetPossibleEpochNanoseconds` for a named zone, with 05ce7a3's clamp: the exact times whose
* wall clock in `timeZone` is `wall`, ascending — none in a gap, two in an overlap.
*
* @throws RangeError when a matching exact time lies outside the representable range
*/
export declare function possibleEpochNanoseconds(timeZone: string, wall: Temporal.PlainDateTime): bigint[];
/** TC39 `GetEpochNanosecondsFor`. */
export declare function epochNanosecondsFor(timeZone: string, wall: Temporal.PlainDateTime, disambiguation: ResolvedDisambiguation): bigint;
/**
* The first instant at or after `low` whose offset is `offset`, given that `low` has a different
* offset and `high` has `offset`: the transition between them, to the nanosecond.
*/
export declare function transitionBetween(timeZone: string, low: bigint, high: bigint, offset: bigint): bigint;
/**
* TC39 `GetStartOfDay`: the first valid wall-clock time on `date` in `timeZone` — midnight, or,
* when midnight is skipped, the transition that skips it.
*/
export declare function startOfDayEpochNanoseconds(timeZone: string, date: Temporal.PlainDate): bigint;
/**
* True when the polyfill's `GetStartOfDay` cannot be trusted for this date in a named zone: it
* lies before the transition search floor (defect 3), or its midnight is skipped while the
* runtime's transition search can miss the change that skips it (defect 5). A midnight that exists
* never reaches that search, so the polyfill's answer stands for it.
*/
export declare function needsOwnStartOfDaySearch(timeZone: string, value: Temporal.PlainDate | Temporal.ZonedDateTime): boolean;
/**
* `needsOwnStartOfDaySearch` for a measure that also reads the next day's start (`hoursInDay`,
* rounding to the day).
*/
export declare function needsOwnDayBoundsSearch(timeZone: string, zoned: Temporal.ZonedDateTime): boolean;
/**
* The first transition after `epochNanoseconds` in a named zone when it lies before the polyfill's
* search floor, which `getTimeZoneTransition("next")` skips; null for any instant at or after the
* floor, or when the offset is the same at both ends (then no zone changes in between, pinned by
* the all-zones test in `zonedWallClock.test.ts`, and the polyfill's own answer stands).
*/
export declare function missedNextTransition(timeZone: string, epochNanoseconds: bigint): bigint | null;
/**
* The last transition strictly before `epochNanoseconds` and before the polyfill's search floor,
* for a named zone where `getTimeZoneTransition("previous")` found none; null when the offset
* never changes between the minimum instant and there.
*/
export declare function missedPreviousTransition(timeZone: string, epochNanoseconds: bigint): bigint | null;
/** TC39 `RoundNumberToIncrement(value, 1 minute, "half-expand")`. */
export declare function roundToMinute(nanoseconds: bigint): bigint;
/** TC39 `InterpretISODateTimeOffset` for a named zone and a wall clock with a time of day. */
export declare function interpretISODateTimeOffset(timeZone: string, wall: Temporal.PlainDateTime, offset: CarriedOffset, disambiguation: ResolvedDisambiguation, offsetOption: ResolvedOffsetOption, matchMinutes: boolean): bigint;
/** A zone id as Temporal canonicalizes it, from an identifier or any RFC 9557 string. */
export declare function timeZoneIdOf(timeZoneLike: string): string;
/** Offset nanoseconds of a UTC offset string (`"+10:00"`, `"-04:56:02"`), parsed by Temporal. */
export declare function utcOffsetStringNanoseconds(offset: string): bigint;
/**
* `Temporal.ZonedDateTime.from`, correct across the whole representable range.
*
* Returns the polyfill's result whenever it has one. When the polyfill throws for a named zone
* within a month of either range limit, the TC39 result is recomputed (see the note at the top
* of this file); every other failure re-throws the polyfill's own error. A string whose offset
* is written with seconds is matched exactly, as TC39 requires (`matchSecondsOffsetExactly`), and
* a string with a `Z` designator is an exact instant under every `offset` option (`zExactOptions`).
*
* @param item RFC 9557 zoned string or ZonedDateTime-like property bag
* @param callerOptions Temporal `disambiguation` / `offset` / `overflow`
* @returns the resolved Temporal.ZonedDateTime; throws where TC39 Temporal throws
*/
export declare function zonedDateTimeFrom(item: string | Temporal.ZonedDateTimeLike, callerOptions?: ZonedFromOptions): Temporal.ZonedDateTime;
export {};