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.

83 lines (81 loc) • 6.73 kB
import { zoneFrame } from "../../internal/zoneFrame.js"; import { parseIntervalNanoseconds } from "../../internal/intervalNanoseconds.js"; import { parseScheduleDisambiguation, parseWeeklyPattern, runText, scheduleRunsWithin } from "../../internal/operatingSchedule.js"; // src/calendar/hours/recurringWindows.ts /** * Return the instants a weekly pattern of local windows is open inside `range`, read in * `timeZone`: the primitive under operating hours, curfews and recurring crew windows. * * - `weekly` maps ISO weekdays (1 = Monday … 7 = Sunday) to `LocalWindow`s of wall time. A * window whose `to` is at or before its `from` wraps past midnight and belongs to the date it * starts on: `{ from: "23:00", to: "06:00" }` under 5 is Friday 23:00 to Saturday 06:00, and * `{ from: "00:00", to: "00:00" }` is the whole day. No holidays or overrides; see * `operatingIntervals` for those. * - Each window edge is a local wall time resolved with `resolveLocal`. An edge in a skipped * spring-forward hour is shifted by the gap: forward, or back when it is read `"earlier"` (TC39 * DisambiguatePossibleEpochNanoseconds), so in a zone that skips midnight a date's early * window can open on the previous local date, before the date's own first instant; it still * belongs to its own date. * - A window "reaches the range" for `"reject"` when the widest span it could cover (its `from` * read `"earlier"`, its `to` read `"later"`) overlaps `range`. A rejected window elsewhere * does not matter. * - Windows are elapsed time between their resolved edges, so the wall-clock length is not the * real one on transition nights: in London, 23:00–06:00 is 8 hours across the fall-back * night and 6 across the spring-forward one, and 00:00–00:00 is 25 and 23 hours. * - A window whose edges resolve to an empty or inverted span on a transition night — only a * window shorter than the gap it straddles — is empty that day and left out. * - The result is clipped to `range` (half-open), sorted, and merged where windows overlap or * touch, so a run of whole-day windows is one interval. Endpoints are UTC instants ending in * `Z`, except a clipped edge, which is `range`'s own string. * - Local dates follow the zone's real days: a date the zone deleted (`Pacific/Apia` skipped * 2011-12-30) has no windows, and a date re-entered after a fall-back is counted once. * - `[]` is both a legitimate result (closed throughout, or an empty `range`) and the sentinel. * - Returns `[]` when `weekly` has a key other than `"1"`–`"7"` or a malformed window, when * `range` is not a valid `Interval`, when `timeZone` is invalid, 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, rather than a truncated list. * - **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 weekly windows by ISO weekday, `{ 1: [{ from: "09:00", to: "17:00" }], … }` * @param range `{ start, end }` record of ISO 8601 instant strings to expand the pattern inside * @param timeZone IANA name or UTC offset the windows are read in: a time zone identifier * (`+05:30`, `+0530`, `-08`) or a stored offset (`±HH:MM[:SS]`, what `getTimeZoneOffset` returns) * @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 recurringWindows({ 1: [{ from: "09:00", to: "17:00" }] }, { start: "2024-06-10T00:00:00Z", end: "2024-06-11T00:00:00Z" }, "Africa/Johannesburg") // [{ start: "2024-06-10T07:00:00Z", end: "2024-06-10T15:00:00Z" }] * @example recurringWindows({ 5: [{ from: "23:00", to: "06:00" }] }, { start: "2024-06-14T00:00:00Z", end: "2024-06-16T00:00:00Z" }, "UTC") // [{ start: "2024-06-14T23:00:00Z", end: "2024-06-15T06:00:00Z" }] — Friday night into Saturday * @example recurringWindows({ 6: [{ from: "23:00", to: "06:00" }] }, { start: "2024-10-26T00:00:00Z", end: "2024-10-28T00:00:00Z" }, "Europe/London") // [{ start: "2024-10-26T22:00:00Z", end: "2024-10-27T06:00:00Z" }] — 8 hours across the fall-back night * @example recurringWindows({ 7: [{ from: "02:30", to: "04:00" }] }, { start: "2024-10-05T12:00:00Z", end: "2024-10-06T12:00:00Z" }, "Australia/Sydney") // [{ start: "2024-10-05T16:30:00Z", end: "2024-10-05T17:00:00Z" }] — 02:30 does not exist; "compatible" reads it as 03:30 * @example recurringWindows({ 7: [{ from: "02:30", to: "04:00" }] }, { start: "2024-10-05T12:00:00Z", end: "2024-10-06T12:00:00Z" }, "Australia/Sydney", { disambiguation: "reject" }) // [] * @example recurringWindows({ 1: [{ from: "00:00", to: "00:00" }], 2: [{ from: "00:00", to: "00:00" }] }, { start: "2024-06-10T00:00:00Z", end: "2024-06-13T00:00:00Z" }, "UTC") // [{ start: "2024-06-10T00:00:00Z", end: "2024-06-12T00:00:00Z" }] — touching days merge * @example recurringWindows({ 8: [{ from: "09:00", to: "17:00" }] }, { start: "2024-06-10T00:00:00Z", end: "2024-06-11T00:00:00Z" }, "UTC") // [] — not an ISO weekday * @example recurringWindows({ 1: [{ from: "9am", to: "17:00" }] }, { start: "2024-06-10T00:00:00Z", end: "2024-06-11T00:00:00Z" }, "UTC") // [] — malformed window * @example recurringWindows({ 1: [{ from: "09:00", to: "17:00" }] }, { start: "2024-06-10T00:00:00Z", end: "2024-06-11T00:00:00Z" }, "Invalid/Zone") // [] * @example recurringWindows({ 4: [{ from: "08:00", to: "17:00" }] }, { start: "1970-01-01T00:00:00Z", end: "1970-01-02T00:00:00Z" }, "-00:44:30") // [{ start: "1970-01-01T08:44:30Z", end: "1970-01-01T17:44:30Z" }] */ function recurringWindows(weekly, range, timeZone, optionsArg) { try { const disambiguation = parseScheduleDisambiguation(optionsArg); const weeklyWindows = parseWeeklyPattern(weekly); const bounds = parseIntervalNanoseconds(range); const frame = zoneFrame(timeZone); if (disambiguation === null || weeklyWindows === null || bounds === null || typeof timeZone !== "string" || frame === null) return []; const runs = scheduleRunsWithin({ timeZone, frame, weekly: weeklyWindows, holidays: /* @__PURE__ */ new Int32Array(0), overrides: /* @__PURE__ */ new Map(), specials: /* @__PURE__ */ new Int32Array(0) }, bounds, disambiguation); return runs === null ? [] : runs.map(runText); } catch { return []; } } export { recurringWindows };