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 • 7.59 kB
import { Disambiguation } from "../types/disambiguation.js"; import "../types/index.js"; import { ZoneFrame } from "./zoneFrame.js"; import { IntervalNanoseconds } from "./intervalNanoseconds.js"; /** * A `LocalWindow` validated once: canonical PlainTime strings, the same times in nanoseconds * since local midnight, and whether it wraps midnight. */ export type ResolvedWindow = { from: string; to: string; fromNs: bigint; toNs: bigint; wraps: boolean; }; /** An `OperatingSchedule` reduced to the lookups the day walk needs. */ export type ResolvedSchedule = { /** The zone as the caller wrote it: a time zone identifier or a stored UTC offset. */ timeZone: string; /** How local time is read in that zone (`zoneFrame`). */ frame: ZoneFrame; /** Windows by ISO weekday; index 0 is unused. */ weekly: readonly (readonly ResolvedWindow[])[]; /** Holiday epoch days, sorted ascending; never mutated. */ holidays: Readonly<Int32Array>; /** Override windows by epoch day (days since 1970-01-01). */ overrides: ReadonlyMap<number, readonly ResolvedWindow[]>; /** Epoch days with a holiday or an override, sorted ascending, no repeats. */ specials: Readonly<Int32Array>; }; /** * Parse `OperatingSchedule['weekly']`: an object keyed by ISO weekday `"1"`–`"7"`, each holding an * array of `LocalWindow`s (or `undefined`, meaning closed). Each weekday is read with an ordinary * property get, as Temporal's `Get` reads a field list, so a non-enumerable or inherited weekday * applies. Any other own enumerable key, string or symbol, or a malformed window, returns `null`; * a non-enumerable one is not an entry, as `Object.assign` and spread do not copy it, so a * function carrying the weekdays is read without its built-in `length` and `name`. */ export declare function parseWeeklyPattern(value: unknown): ResolvedWindow[][] | null; /** Validate an `OperatingSchedule`, or `null` when any part of it is malformed. */ export declare function parseOperatingSchedule(value: unknown): ResolvedSchedule | null; /** * Read `disambiguation` from an options argument. Omitted means `"compatible"`, as in * `resolveLocal`; anything but the four Temporal values, or an options argument that is not an * object, returns `null`. */ export declare function parseScheduleDisambiguation(options: unknown): Disambiguation | null; /** * The last instant a search may answer with: `from` plus `within` (default one year), added in * the schedule's zone so `P1D` is one local day. It is clamped to the search cap: the last instant * before the local date `MAX_SCHEDULE_DAYS + 1` after the one `from` falls on, so a search answers * anywhere on the 10,000th date and nowhere after it. A horizon past Temporal's last instant is * that instant, since nothing after it can be answered. `null` when `within` is not a * non-negative ISO duration. */ export declare function parseSearchHorizon(fromNs: bigint, within: unknown, frame: ZoneFrame): bigint | null; /** * What the walk tells its caller. * * - `run` is a finished open run: sorted, disjoint, non-touching and never extended later. * - `day` announces that every window still to come starts at or after `bound`, and passes the * runs still `pending`: sorted and disjoint, each ending at or after `bound`, so a later window * may extend them but cannot add open time before `bound`. A caller can answer from them once * its answer lies at or before `bound`. * - Returning `true` from either stops the walk; stopping from `day` hands the pending runs to * `run` first. */ export type ScheduleVisitor = { run: (run: IntervalNanoseconds) => boolean; day: (bound: bigint, pending: readonly IntervalNanoseconds[]) => boolean; }; /** * How a walk ended. `unresolvedStart` is the earliest instant a window rejected under * `"reject"` could open at, among those that could still be open at or after `fromNs`; * `undefined` when there is none. A rejected window on a date the walk did not reach starts at * or after the last `bound` it announced. */ export type ScheduleWalk = { unresolvedStart: bigint | undefined; }; /** * The text of a run's edges: the range's own where the run was clipped to it, else the instant's * UTC string, written here and only for the runs a caller returns. */ export declare function runText(run: IntervalNanoseconds): { start: string; end: string; }; /** * The schedule's open runs clipped to `[startNs, endNs)`, in the range's own strings where a run * is clipped. `null` when the walk fails (see `walkSchedule`), or when a window rejected under * `"reject"` reaches into the range. */ export declare function scheduleRunsWithin(schedule: ResolvedSchedule, range: IntervalNanoseconds, disambiguation: Disambiguation): IntervalNanoseconds[] | null; /** * The first instant at or after `fromNs` that the schedule is open: `fromNs` itself inside an * open run, else the start of the next run. Answered as soon as no later window can open * sooner, without waiting for the run to close. `undefined` when none falls at or before * `horizonNs`, `null` when the walk fails or a window rejected under `"reject"` could open * sooner. */ export declare function firstOpenInstant(schedule: ResolvedSchedule, fromNs: bigint, horizonNs: bigint, disambiguation: Disambiguation): bigint | undefined | null; /** * The first instant at or after `fromNs` that the schedule is closed: `fromNs` itself outside * every open run, else the end of the run holding it. A closed `fromNs` is answered as soon as * no later window can cover it. `undefined` when the answer falls after `horizonNs`, `null` when * the walk fails or a window rejected under `"reject"` could keep the schedule open past it. */ export declare function firstClosedInstant(schedule: ResolvedSchedule, fromNs: bigint, horizonNs: bigint, disambiguation: Disambiguation): bigint | undefined | null; /** * The instant at which `amountNs` of open time has elapsed since `fromNs`: the earliest `X` * with exactly that much open time in `[fromNs, X)`. `fromNs` itself when `amountNs` is zero. * Answered as soon as no later window can add open time before `X`. `undefined` when `X` falls * after `horizonNs`, `null` when the walk fails or a window rejected under `"reject"` could add * open time before `X`. */ export declare function instantAfterOpenTime(schedule: ResolvedSchedule, fromNs: bigint, amountNs: bigint, horizonNs: bigint, disambiguation: Disambiguation): bigint | undefined | null; /** * An amount of open time in nanoseconds, from an ISO duration of hours and smaller units. * `null` for a negative duration, for any calendar unit (years, months, weeks or days — `P1D` * could mean 24 open hours or one working day, so it is not guessed), or invalid input. */ export declare function parseOpenTimeAmount(value: unknown): bigint | null; /** Epoch nanoseconds as a UTC instant string ending in `Z`. */ export declare function instantText(nanoseconds: bigint): string; /** * Open time in `[range.start, range.end)`, in nanoseconds; `null` exactly when * `scheduleRunsWithin` would be. * * Open time is additive over disjoint half-open pieces. Whole day buckets inside a stretch of * constant offset are summed in closed form (whole weeks of the weekly pattern, plus a correction * for each holiday and override); the pieces between them, the range's ends and the dates near an * offset change, are walked. */ export declare function openTimeWithin(schedule: ResolvedSchedule, range: IntervalNanoseconds, disambiguation: Disambiguation): bigint | null;