@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.
34 lines • 2.75 kB
TypeScript
import { BusinessCalendar } from "../../types/business-calendar.js";
import "../../types/index.js";
/**
* Compose several jurisdictions' calendars into one whose working days are those they all
* share.
*
* - A date is a working day in the result only if it is a working day in **every** input: the
* weekend days union and so do the holidays, so a closure anywhere closes the merged
* calendar.
* - This is why the business-day engine is core rather than finance. FX settlement needs both
* currencies' calendars, an intermodal move the origin port's and the destination port's,
* and a cross-border rail path both national calendars — one implementation, four consumers.
* - `weekend` and `holidays` come back sorted ascending and de-duplicated, so the result is
* stable whatever order the inputs arrive in. Holidays sort chronologically, not
* lexicographically, so an expanded year such as `"+010000-01-01"` lands where it belongs.
* - `timeZone` is the **first** calendar's. The merged calendar spans localities that may
* disagree, and GMT does not invent a zone for it; the field records locality only and no
* business-day function reads it.
* - Returns `null` for an empty list — there is no `timeZone` to take — when `calendars` is
* not an array, when any element is not a valid `BusinessCalendar`, and when the merged
* weekend swallows all seven weekdays, leaving a calendar with no working day at all.
* `isValidBusinessCalendar` tells a legitimate result apart from that sentinel.
*
* @param calendars array of BusinessCalendar records to compose
* @returns merged BusinessCalendar, or null on invalid input
*
* @example mergeCalendars([{ weekend: [6, 7], holidays: ["2024-07-04"], timeZone: "Africa/Kigali" }, { weekend: [6, 7], holidays: ["2024-05-06"], timeZone: "Europe/London" }]) // { weekend: [6, 7], holidays: ["2024-05-06", "2024-07-04"], timeZone: "Africa/Kigali" }
* @example mergeCalendars([{ weekend: [6, 7], holidays: [], timeZone: "UTC" }, { weekend: [5, 6], holidays: [], timeZone: "Asia/Riyadh" }]) // { weekend: [5, 6, 7], holidays: [], timeZone: "UTC" } — only Sunday to Thursday survive
* @example mergeCalendars([{ weekend: [6, 7], holidays: ["2024-07-04"], timeZone: "UTC" }]) // { weekend: [6, 7], holidays: ["2024-07-04"], timeZone: "UTC" }
* @example mergeCalendars([]) // null
* @example mergeCalendars([{ weekend: [1, 2, 3, 4], holidays: [], timeZone: "UTC" }, { weekend: [5, 6, 7], holidays: [], timeZone: "UTC" }]) // null — no working day left
* @example mergeCalendars([{ weekend: [6, 7], holidays: [], timeZone: "Invalid/Zone" }]) // null
*/
export declare function mergeCalendars(calendars: BusinessCalendar[]): BusinessCalendar | null;