@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.
82 lines • 4.97 kB
TypeScript
import { BusinessCalendar } from "../types/business-calendar.js";
import { Temporal } from "./temporal.js";
import "../types/index.js";
/**
* Calendar days a single business-day walk may step before giving up — roughly 547 years, far
* past any real schedule. A walk that runs out returns the sentinel, never a partial answer.
*/
export declare const MAX_BUSINESS_DAY_STEPS = 200000;
/** A `BusinessCalendar` reduced to the lookups every walk needs. */
export type ResolvedBusinessCalendar = {
weekend: Set<number>;
/** Every holiday as an epoch day, sorted ascending, no repeats. */
holidays: Int32Array;
/** The holidays that fall on a working weekday: the only ones that change a count. */
closures: Int32Array;
};
/** Shared by every caller without a calendar: its arrays are empty and never mutated. */
export declare const DEFAULT_BUSINESS_CALENDAR: ResolvedBusinessCalendar;
export declare function parseWeekend(weekend: unknown): Set<number> | null;
/** Empty the holiday memo. Tests only. */
export declare function clearHolidayMemo(): void;
/** The epoch day of an ISO holiday string, or `null` when it is not one. */
export declare function holidayEpochDay(holiday: unknown): number | null;
/**
* Reduce a holiday list to its epoch days, sorted ascending without repeats, or `null` when it
* is not an array of ISO PlainDate strings. Shared with `OperatingSchedule.holidays`, which
* takes the same list.
*/
export declare function parseHolidays(holidays: unknown): Int32Array | null;
/**
* Reduce a caller-supplied `BusinessCalendar` to its lookups, or `null` when it is not one.
*
* `undefined` is rejected here; `resolveBusinessCalendar` is the entry point that treats an
* absent calendar as the default Saturday–Sunday week.
*/
export declare function parseBusinessCalendar(calendar: unknown): ResolvedBusinessCalendar | null;
/**
* Join a parsed weekend and a sorted, repeat-free holiday list into the lookups every walk
* needs, filtering out the holidays that fall on a weekend day.
*/
export declare function resolveParts(weekend: Set<number>, holidays: Int32Array): ResolvedBusinessCalendar;
/**
* Resolve an optional calendar: absent means the default Saturday–Sunday week with no
* holidays, anything else must be a valid `BusinessCalendar` or this returns `null`.
*/
export declare function resolveBusinessCalendar(calendar: BusinessCalendar | undefined): ResolvedBusinessCalendar | null;
/**
* True when `date` is neither a weekend day nor a holiday in `calendar`. `date` must be an
* ISO-calendar date: any other calendar throws a `RangeError`. Every caller passes a date it
* parsed from an ISO string or took from `toPlainDate()` of a zoned string parsed the same way.
*/
export declare function isBusinessDate(date: Temporal.PlainDate, calendar: ResolvedBusinessCalendar): boolean;
/** True when `epochDay` is neither a weekend day nor a holiday on a working weekday. */
export declare function isBusinessEpochDay(epochDay: number, calendar: ResolvedBusinessCalendar): boolean;
/** Business days in [from, to] (epoch days, from <= to); 0 when from > to. */
export declare function businessDaysWithin(from: number, to: number, calendar: ResolvedBusinessCalendar): number;
/**
* The first business day at or beyond `date`, walking in `direction`.
*
* `date` is an ISO-calendar date, as every caller validates. Returns `date` itself when it is
* already a business day, and `null` when the walk exceeds `MAX_BUSINESS_DAY_STEPS`. Throws a
* `RangeError` when the walk passes a Temporal range limit.
*/
export declare function businessDateFrom(date: Temporal.PlainDate, direction: 1 | -1, calendar: ResolvedBusinessCalendar): Temporal.PlainDate | null;
/**
* First business day strictly after `epochDay`: the epoch-day form of `stepBusinessDates` with
* a count of 1, so no `PlainDate` is built. Returns `null` when the walk exceeds
* `MAX_BUSINESS_DAY_STEPS`; throws a `RangeError` when it passes a Temporal range limit.
*/
export declare function nextBusinessEpochDay(epochDay: number, calendar: ResolvedBusinessCalendar): number | null;
/**
* Step `count` business days from `date` in `direction`, counting only days that are business
* days in `calendar`.
*
* `date` is an ISO-calendar date, as every caller validates. `date` itself is never counted, so
* a `count` of 0 returns `date` unchanged even when it is not a business day. Returns `null`
* when the walk exceeds `MAX_BUSINESS_DAY_STEPS`, and for a `count` that is not a whole number
* of days or is negative or NaN: a fractional or infinite `count` still walks first, as the
* day-by-day loop did, so it throws the same `RangeError` when it passes a Temporal range limit
* before finishing. Throws a `RangeError` when any walk passes a Temporal range limit.
*/
export declare function stepBusinessDates(date: Temporal.PlainDate, direction: 1 | -1, count: number, calendar: ResolvedBusinessCalendar): Temporal.PlainDate | null;