@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.
81 lines • 4.36 kB
TypeScript
import { EasterComputus } from "../types/holiday-rule.js";
import { ResolvedBusinessCalendar } from "./businessCalendar.js";
import { ResolvedObservancePolicy } from "./observance.js";
type Observed = ResolvedObservancePolicy | null;
/** A `HolidayRule` after validation: numbers checked, calendar canonical, policy resolved, dates as epoch days. */
export type ResolvedHolidayRule = {
kind: "fixed";
month: number;
day: number;
policy: Observed;
} | {
kind: "calendarFixed";
calendarId: string;
monthCode: string;
day: number;
policy: Observed;
} | {
kind: "nthWeekday";
month: number;
weekday: number;
n: number;
} | {
kind: "easterRelative";
offsetDays: number;
computus: EasterComputus;
} | {
kind: "dates";
days: Int32Array;
};
/**
* Validate a caller-supplied `HolidayRule` and reduce it to what the expansion needs, or `null`
* when it is not one. A `name` must be a string but is never output; properties a kind does not
* define are ignored, as TC39 option reading ignores unknown keys. Reads the rule's properties,
* so a hostile getter can throw: callers wrap this in `try`.
*/
export declare function parseHolidayRule(rule: unknown): ResolvedHolidayRule | null;
/** True when the rule's policy shifts some weekday to `"nextWorkingDay"`, which needs a calendar. */
export declare function ruleNeedsCalendar(rule: ResolvedHolidayRule): boolean;
/**
* The observed day of each occurrence of the rule whose unshifted date lies in ISO `year` (the
* occurrence itself when its policy does not move it), sorted, without repeats. Returns `null`
* when a shift fails (no calendar for `nextWorkingDay`, out of range, or longer than
* `maxShiftDays`). Throws a `RangeError` when a calendar cannot be read or a walk leaves
* Temporal's range. `year` outside -271821..275760 gives `[]`. `skipOutOfRange` is as in
* `observedDays`.
*/
export declare function holidayEpochDays(rule: ResolvedHolidayRule, year: number, calendar: ResolvedBusinessCalendar | null, maxShiftDays?: number, skipOutOfRange?: boolean): number[] | null;
/**
* Epoch days of every holiday the rules produce in years `from`..`to` (integers in
* -271821..275760, `from <= to`, as the caller checks), sorted without repeats; only the result
* is clipped to the window and to Temporal's range. Returns `null` when a shift fails or exceeds
* `MAX_SHIFT_DAYS` (in any expanded year) or a shift inside the window leaves Temporal's range.
* Throws a `RangeError` when a calendar cannot be read.
*
* Two passes, so the order of `rules` never changes which days are closed:
*
* 1. Every occurrence that does not walk is placed: unmoved, or moved by `{ next }` or
* `{ previous }`, which never read the calendar.
* 2. The `nextWorkingDay` occurrences walk, rule by rule, each to the first day after it that is
* neither a weekend day nor a holiday placed so far. A walk never lands on another holiday.
*
* The days pass 2 closes are those of linear probing, which do not depend on the order of the
* walks (Knuth, *TAOCP* vol. 3, §6.4). Only which walk takes which day does, and so the length
* of each shift: rules walk in list order, which a shift over 31 days can expose only through a
* run of 31 closed days. A rule's own occurrences are at least 353 days apart, so its walks never
* meet and one calendar per walking rule serves all its years.
*
* Margin. Years `to + 1` are expanded after the window: walks only move forward, and
* `previous`/`next` shifts never read the calendar. Before the window `M` years are expanded,
* `M = max(1, ceil(31 * (K + 1) / 365))` with `K` the rules that read the calendar. Each such
* rule walks once, against the days placed before it, so it can push a changed day at most 31
* days later, once; a change that starts in year
* `from - M - 1` (never expanded) moves at most 31 * (K + 1) days, which stays before `from`
* while `31 * (K + 1) <= 365 * M`. `M` is 1 for ten rules or fewer. In a margin year an observed
* day outside Temporal's range is dropped, since it could never fall in the window.
*
* Loop bound: rules x at most 400 + M + 1 years, plus one calendar per rule that uses
* `nextWorkingDay`.
*/
export declare function buildHolidayEpochDays(rules: readonly ResolvedHolidayRule[], weekend: Set<number>, from: number, to: number): Int32Array | null;
export {};