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.

135 lines • 6.91 kB
import { Temporal } from "./temporal.js"; import { Disambiguation } from "../types/disambiguation.js"; import "../types/index.js"; import { ZoneFrame } from "./zoneFrame.js"; /** One local period, `[start, end)`, as epoch nanoseconds. */ export type UnitSpan = { start: bigint; end: bigint; }; /** The local unit a count `Period` is made of. */ export type CalendarUnit = "day" | "week" | "month" | "year"; /** * The local periods of one kind in one reference zone. * * `containing(x)` is the period `x` falls in, `start ≤ x < end`; null when that period, or the * one after it, cannot be represented. Results are cached, so a call that asks about the same * periods again (every candidate window of `dutyViolations`) resolves each boundary once. */ export type UnitScale = { basis: "operatorDay" | "calendar"; containing: (x: bigint) => UnitSpan | null; /** * The period `periods` after the one `span` is (before it when negative), found without * stepping through the periods between: `undefined` where it cannot be, and the periods must be * walked. Null when that period cannot be represented. */ jump?: (span: UnitSpan, periods: number) => UnitSpan | null | undefined; /** The periods `periodAfter` has walked, in order, so no walk is made twice in a call. */ chain: PeriodChain; }; /** * Periods walked one after another, each by its place in the walk: `first` and `last` are the * places of the earliest and latest kept. */ export type PeriodChain = { byPlace: Map<number, UnitSpan>; byStart: Map<bigint, number>; first: number; last: number; }; /** A chain holding nothing yet. */ export declare const emptyChain: () => PeriodChain; /** Parse a `dayStartTime`: an ISO PlainTime string `isValidTime` accepts, or null. */ export declare function parseDayStartTime(value: unknown): Temporal.PlainTime | null; /** * Thrown when `"reject"` meets a wall time that matters and that the zone repeats or skips. * Caught by the public function, which returns its sentinel. */ export declare class WallClockRejected extends RangeError {} /** A wall clock resolved under a policy, and whether `"reject"` refuses it. */ export type ResolvedWallClock = { at: bigint; rejected: boolean; }; /** * The instant a local date and wall time name in a reference zone, resolved as `resolveLocal` * resolves it under `disambiguation`. * * - `"compatible"` reads a skipped time after the gap and a repeated one at its first pass; * `"earlier"` and `"later"` take that side in both cases. * - Under `"reject"` the instant is the `"compatible"` one, and `rejected` says whether the zone * repeats or skips that wall time, so a caller rejects only a wall time its answer uses. * - A stored offset (`±HH:MM:SS`) has no transition, so nothing is ever rejected there. * * @param frame the reference zone, from `zoneFrame` * @param date the local date * @param time the local wall time * @param disambiguation the policy for a repeated or skipped wall time * @returns the instant and whether it is rejected, or null when it is outside Temporal's range * * @example resolveWallClock(zoneFrame("UTC"), Temporal.PlainDate.from("1970-01-02"), Temporal.PlainTime.from("00:00"), "compatible") // { at: 86400000000000n, rejected: false } */ export declare function resolveWallClock(frame: ZoneFrame, date: Temporal.PlainDate, time: Temporal.PlainTime, disambiguation: Disambiguation): ResolvedWallClock | null; /** The local date an instant falls on in a reference zone. */ export declare function localDateOf(frame: ZoneFrame, x: bigint): Temporal.PlainDate; /** A date some days after another, or null past Temporal's date range. */ export declare function datePlusDays(date: Temporal.PlainDate, days: number): Temporal.PlainDate | null; /** * The operator-designated days of a reference zone: each starts at `time` on a local date, * resolved under `disambiguation` as `resolveLocal` resolves it, and runs to the next date's start. * * - Every policy maps wall times to instants without reversing them, so the starts never go * backwards and the days tile the timeline. A date whose start resolves onto the next date's * start (a date the zone deleted) opens no day of its own. * - Under `"reject"`, a day whose start or end is a repeated or skipped wall time throws * `WallClockRejected`; a day bounded by ordinary wall times is answered as `"compatible"`. * * @param frame the reference zone, from `zoneFrame` * @param time the local wall time each day starts at * @param disambiguation the policy for a start time the zone repeats or skips * @returns the operator days * * @example operatorDayScale(zoneFrame("UTC"), Temporal.PlainTime.from("04:00"), "compatible").containing(0n) // { start: -72000000000000n, end: 14400000000000n } */ export declare function operatorDayScale(frame: ZoneFrame, time: Temporal.PlainTime, disambiguation: Disambiguation): UnitScale; /** * The local calendar days, ISO weeks (Monday first), months or years of a reference zone, as * `floorToZone` and `bucketRange` find them: real boundaries, never a truncated wall clock * resolved again. * * @param frame the reference zone, from `zoneFrame` * @param unit the calendar unit * @returns the periods * * @example calendarScale(zoneFrame("UTC"), "day").containing(1n) // { start: 0n, end: 86400000000000n } */ export declare function calendarScale(frame: ZoneFrame, unit: CalendarUnit): UnitScale; /** * The period `periods` after `span` in a scale (before it when negative), `span` itself for 0. * * - A few periods away, it steps through them. Further, it takes the scale's `jump` where the * scale can jump there, and otherwise walks, keeping each period walked in the scale's chain, * so a call that asks again, or asks for a neighbour, walks no period twice. * - Null when a period on the way cannot be represented. * * @param scale the periods * @param span a period of the scale * @param periods how many periods on, at most `2 × MAX_PERIOD_COUNT` either way * @returns the period, or null * * @example periodAfter(calendarScale(zoneFrame("UTC"), "day"), { start: 0n, end: 86400000000000n }, 1) // { start: 86400000000000n, end: 172800000000000n } */ export declare function periodAfter(scale: UnitScale, span: UnitSpan, periods: number): UnitSpan | null; /** * The `count` periods of a scale that end with the one `anchor` falls in. * * @param scale the periods * @param count how many, a positive integer * @param anchor the instant whose period is the last * @returns the span from the first period's start to the last's end, or null when a period * cannot be represented * * @example periodsEndingAt(calendarScale(zoneFrame("UTC"), "day"), 2, 1n) // { start: -86400000000000n, end: 86400000000000n } */ export declare function periodsEndingAt(scale: UnitScale, count: number, anchor: bigint): UnitSpan | null;