@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.
51 lines • 4.36 kB
TypeScript
import { Disambiguation } from "../../types/disambiguation.js";
import { Interval } from "../../types/interval.js";
import { OperatingSchedule } from "../../types/operating-schedule.js";
import "../../types/index.js";
/**
* Return the instants an `OperatingSchedule` is open inside `range`: its weekly windows with
* holidays closed and overrides applied, resolved in the schedule's zone.
*
* - Works date by date in `schedule.timeZone`. A date listed in `holidays` has no windows; a
* date with an override has exactly the override's windows, holiday or not. `holidays` may be
* a `BusinessCalendar`, whose `holidays` are read.
* - A window belongs to the date it starts on, so a holiday or override on Friday removes or
* replaces a Friday 23:00–06:00 window, and one on Saturday leaves it alone.
* - Window edges are local wall times; `recurringWindows` states the window rules in full.
* - The result is clipped to `range` (half-open), sorted, and merged where windows overlap or
* touch. Endpoints are UTC instants ending in `Z`, except a clipped edge, which is `range`'s
* own string.
* - `[]` is both a legitimate result (closed throughout, or an empty `range`) and the sentinel.
* - Returns `[]` when `schedule` is not a valid `OperatingSchedule` (a weekday key other than
* `"1"`–`"7"`, a malformed window, an invalid zone, holiday or override date, or two overrides
* for one date), when `range` is not a valid `Interval`, when `disambiguation` is not one of
* the four values, and when the range spans more than 10,000 local dates (about 27 years)
* counted from the date `range.start` falls on.
* - **Limit at an offset with seconds.** The instant is placed in the offset's whole-minute zone,
* moved by its seconds, and the moved instant must be inside Temporal's range. So within the
* offset's seconds (under a minute) of the first instant Temporal supports
* (`-271821-04-20T00:00:00Z`), for an offset west of UTC, this returns `[]`. An offset to the
* minute has no such limit.
*
* @param schedule `{ timeZone, weekly, holidays?, overrides? }` operating schedule
* @param range `{ start, end }` record of ISO 8601 instant strings to expand the schedule inside
* @param optionsArg How a window edge on a clock change is resolved
* @returns sorted, merged `{ start, end }` records of the open instants, or [] on invalid input
*
* @example operatingIntervals({ timeZone: "Africa/Kigali", weekly: { 4: [{ from: "09:00", to: "17:00" }], 5: [{ from: "09:00", to: "17:00" }] }, holidays: ["2024-07-04"] }, { start: "2024-07-04T00:00:00Z", end: "2024-07-06T00:00:00Z" }) // [{ start: "2024-07-05T07:00:00Z", end: "2024-07-05T15:00:00Z" }] — the holiday is closed
* @example operatingIntervals({ timeZone: "Africa/Kigali", weekly: { 4: [{ from: "09:00", to: "17:00" }] }, holidays: ["2024-07-04"], overrides: [{ date: "2024-07-04", windows: [{ from: "10:00", to: "12:00" }] }] }, { start: "2024-07-04T00:00:00Z", end: "2024-07-05T00:00:00Z" }) // [{ start: "2024-07-04T08:00:00Z", end: "2024-07-04T10:00:00Z" }] — the override wins
* @example operatingIntervals({ timeZone: "UTC", weekly: { 1: [{ from: "09:00", to: "17:00" }] } }, { start: "2024-06-10T12:00:00Z", end: "2024-06-10T14:00:00Z" }) // [{ start: "2024-06-10T12:00:00Z", end: "2024-06-10T14:00:00Z" }] — clipped to the range
* @example operatingIntervals({ timeZone: "UTC", weekly: { 1: [{ from: "09:00", to: "17:00" }] }, overrides: [{ date: "2024-06-10", windows: [] }] }, { start: "2024-06-10T00:00:00Z", end: "2024-06-11T00:00:00Z" }) // [] — closed by override
* @example operatingIntervals({ timeZone: "Invalid/Zone", weekly: {} }, { start: "2024-06-10T00:00:00Z", end: "2024-06-11T00:00:00Z" }) // []
*/
export declare function operatingIntervals(schedule: OperatingSchedule, range: Interval, optionsArg?: {
/**
* How a window edge in a repeated or skipped local hour becomes an instant, as `resolveLocal`
* resolves it. `"compatible"` takes the earlier instant of a repeated fall-back hour and the
* later one of a skipped spring-forward hour, and `"earlier"` and `"later"` take that side in
* both cases. `"reject"` returns `[]` when a window with such an edge reaches the range.
*
* @defaultValue `"compatible"`, Temporal's default.
*/
disambiguation?: Disambiguation;
}): Interval[];