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.

930 lines (928 loc) • 40.4 kB
import { Temporal } from "./temporal.js"; import { MAX_EPOCH_NANOSECONDS, MIN_EPOCH_NANOSECONDS } from "./epochNanoseconds.js"; import { durationFrom } from "./durationFrom.js"; import { plainDateOf, plainTimeOf } from "./inputChecks.js"; import { NANOSECONDS_PER_DAY } from "./foreignEpochs.js"; import { isObject, isOptionsArgument } from "./isObject.js"; import { frameEpochNanoseconds, frameWallClock, frameZoned, zoneFrame } from "./zoneFrame.js"; import { ISO_WEEKDAYS, epochDayOfDate, hasEpochDay, isoWeekdayOf, lowerBound } from "./epochDay.js"; import { parseBusinessCalendar, parseHolidays } from "./businessCalendar.js"; import { ensureTimeline, offsetOf, offsetTimeline, resolveWallNs, startOfDayNs } from "./offsetTimeline.js"; import { nextZonedBucketStart, zonedUnitStart } from "./zonedBucket.js"; import { coalesceIntervalNanoseconds } from "./intervalNanoseconds.js"; import { resolveLocalInstant } from "./resolveLocalInstant.js"; // src/internal/operatingSchedule.ts const DISAMBIGUATIONS = [ "compatible", "earlier", "later", "reject" ]; const WEEKDAY_KEYS = [ "1", "2", "3", "4", "5", "6", "7" ]; /** * Local dates a range may span, counted from the date its start falls on: about 27 years, the * same bound `bucketRange` puts on a day walk. A walk that runs out answers with the sentinel, * never a partial list. */ const MAX_SCHEDULE_DAYS = 1e4; /** * Day buckets one walk may step through: the lookback, the dates, the one date past them that * settles the last, and the extra bucket a fall-back into the previous date opens. The date * limit is the one that binds. */ const MAX_SCHEDULE_BUCKETS = 2 * MAX_SCHEDULE_DAYS; /** * How far before the first instant of interest the walk starts, in nanoseconds (72 hours). A * window is at most one local day of wall time, but a resolved edge can land a whole deleted day * later (`Pacific/Apia` skipped 2011-12-30), so a window that started up to two days earlier can * still be open. Three days clears that with room to spare. */ const LOOKBACK_NANOSECONDS = 259200000000000n; /** * Where an edge whose wall time lies past Temporal's range resolves: just outside it. Such an * edge has no string, so it is only ever clipped to a range or compared, never returned. */ const PAST_LAST_INSTANT = MAX_EPOCH_NANOSECONDS + 1n; const BEFORE_FIRST_INSTANT = MIN_EPOCH_NANOSECONDS - 1n; /** * The search horizon of `nextOpenAt`, `nextCloseAt` and `addOperatingTime` when the caller * gives no `within`: one calendar year in the schedule's zone. */ const DEFAULT_OPERATING_HORIZON = "P1Y"; /** Nanoseconds from midnight to a wall-clock time. */ function timeOfDayNanoseconds(time) { return BigInt(new Temporal.PlainTime().until(time).total("nanoseconds")); } function parseWindow(value) { if (!isObject(value) || Array.isArray(value)) return null; const { from, to } = value; const fromTime = plainTimeOf(from); const toTime = plainTimeOf(to); if (fromTime === null || toTime === null) return null; return { from: fromTime.toString(), to: toTime.toString(), fromNs: timeOfDayNanoseconds(fromTime), toNs: timeOfDayNanoseconds(toTime), wraps: Temporal.PlainTime.compare(toTime, fromTime) <= 0 }; } function parseWindows(value) { if (!Array.isArray(value)) return null; const windows = []; for (const candidate of value) { const window = parseWindow(candidate); if (window === null) return null; windows.push(window); } return windows; } /** * Parse `OperatingSchedule['weekly']`: an object keyed by ISO weekday `"1"`–`"7"`, each holding an * array of `LocalWindow`s (or `undefined`, meaning closed). Each weekday is read with an ordinary * property get, as Temporal's `Get` reads a field list, so a non-enumerable or inherited weekday * applies. Any other own enumerable key, string or symbol, or a malformed window, returns `null`; * a non-enumerable one is not an entry, as `Object.assign` and spread do not copy it, so a * function carrying the weekdays is read without its built-in `length` and `name`. */ function parseWeeklyPattern(value) { if (!isObject(value) || Array.isArray(value)) return null; const weekly = Array.from({ length: 8 }, () => []); const record = value; for (const key of Reflect.ownKeys(record)) if ((typeof key !== "string" || !WEEKDAY_KEYS.includes(key)) && Object.prototype.propertyIsEnumerable.call(record, key)) return null; for (const key of WEEKDAY_KEYS) { const day = record[key]; if (day === void 0) continue; const windows = parseWindows(day); if (windows === null) return null; weekly[Number(key)] = windows; } return weekly; } /** `holidays` as a date list or a `BusinessCalendar` (only its `holidays` are read). */ function parseScheduleHolidays(value) { if (value === void 0) return /* @__PURE__ */ new Int32Array(0); if (Array.isArray(value)) return parseHolidays(value); return parseBusinessCalendar(value)?.holidays ?? null; } function parseOverrides(value) { if (value === void 0) return /* @__PURE__ */ new Map(); if (!Array.isArray(value)) return null; const overrides = /* @__PURE__ */ new Map(); for (const candidate of value) { const entry = parseOverride(candidate); if (entry === null || overrides.has(entry[0])) return null; overrides.set(entry[0], entry[1]); } return overrides; } /** One override as its epoch day and windows, or `null` when it is malformed. */ function parseOverride(candidate) { if (!isObject(candidate) || Array.isArray(candidate)) return null; const { date, windows } = candidate; const day = plainDateOf(date); if (day === null) return null; const key = epochDayOfDate(day); const parsed = parseWindows(windows); return parsed === null ? null : [key, parsed]; } /** Validate an `OperatingSchedule`, or `null` when any part of it is malformed. */ function parseOperatingSchedule(value) { if (!isObject(value) || Array.isArray(value)) return null; const { timeZone, weekly, holidays, overrides } = value; const frame = zoneFrame(timeZone); if (typeof timeZone !== "string" || frame === null) return null; const weeklyWindows = parseWeeklyPattern(weekly); const holidayDates = parseScheduleHolidays(holidays); const overrideWindows = parseOverrides(overrides); if (weeklyWindows === null || holidayDates === null || overrideWindows === null) return null; return { timeZone, frame, weekly: weeklyWindows, holidays: holidayDates, overrides: overrideWindows, specials: specialDays(holidayDates, overrideWindows) }; } /** The sorted union of the holiday days and the override days. */ function specialDays(holidays, overrides) { const days = new Set(holidays); for (const day of overrides.keys()) days.add(day); return Int32Array.from(days).sort(); } /** * Read `disambiguation` from an options argument. Omitted means `"compatible"`, as in * `resolveLocal`; anything but the four Temporal values, or an options argument that is not an * object, returns `null`. */ function parseScheduleDisambiguation(options) { if (!isOptionsArgument(options)) return null; const value = options?.disambiguation; if (value === void 0) return "compatible"; return typeof value === "string" && DISAMBIGUATIONS.includes(value) ? value : null; } /** * The last instant a search may answer with: `from` plus `within` (default one year), added in * the schedule's zone so `P1D` is one local day. It is clamped to the search cap: the last instant * before the local date `MAX_SCHEDULE_DAYS + 1` after the one `from` falls on, so a search answers * anywhere on the 10,000th date and nowhere after it. A horizon past Temporal's last instant is * that instant, since nothing after it can be answered. `null` when `within` is not a * non-negative ISO duration. */ function parseSearchHorizon(fromNs, within, frame) { const text = within === void 0 ? DEFAULT_OPERATING_HORIZON : within; if (typeof text !== "string") return null; let duration; try { duration = durationFrom(text); } catch { return null; } if (duration.sign < 0) return null; const from = frameZoned(Temporal.Instant.fromEpochNanoseconds(fromNs), frame); const horizon = clampedToRange(() => frameEpochNanoseconds(from.add(duration), frame)); const cap = clampedToRange(() => frameEpochNanoseconds(from.toPlainDate().add({ days: 10001 }).toZonedDateTime(frame.timeZone), frame) - 1n); return horizon < cap ? horizon : cap; } /** An instant computed by `instant`, or Temporal's last instant when it lies past the range. */ function clampedToRange(instant) { try { const nanoseconds = instant(); return nanoseconds > 8640000000000000000000n ? MAX_EPOCH_NANOSECONDS : nanoseconds; } catch (error) { if (error instanceof RangeError) return MAX_EPOCH_NANOSECONDS; throw error; } } /** The local date of an instant in the schedule's zone. */ function localDateOf(schedule, ns) { return frameWallClock(Temporal.Instant.fromEpochNanoseconds(ns), schedule.frame).toPlainDate(); } /** * The local date `days` after the one `fromNs` falls on, or `null` when that is past Temporal's * last date: no walk from `fromNs` can reach it before time runs out. */ function dateLimit(schedule, fromNs, days) { try { return localDateOf(schedule, fromNs).add({ days }); } catch (error) { if (error instanceof RangeError) return null; throw error; } } /** * The day bucket the walk starts in: the one holding the instant 72 hours before `fromNs`, or * the earliest instant the schedule's frame can place when that is earlier. When the bucket's own * start is not representable, the walk starts at that instant instead, the earliest of its date * there is. * * The earliest instant is Temporal's first, except at an offset with seconds west of UTC: its * stand-in sits the offset's seconds before the instant (`frameZoned`), so the first instant it * can place is that many seconds after Temporal's first. A walk from before it throws * `RangeError`, the caller's sentinel, rather than start after its own instant. * * `atRangeStart` is true when the walk starts at that earliest instant. No day bucket lies * before it, so the windows of the local date before the bucket's own are not reached by walking * back: the walk carries them in (`resolveWindowsBefore`). */ function firstWalkBucket(schedule, fromNs) { const shift = schedule.frame.shiftNanoseconds; const earliestNs = shift < 0n ? MIN_EPOCH_NANOSECONDS - shift : MIN_EPOCH_NANOSECONDS; if (fromNs < earliestNs) throw new RangeError("the instant is before the first the frame can place"); const atRangeStart = fromNs - LOOKBACK_NANOSECONDS <= earliestNs; const startNs = atRangeStart ? earliestNs : fromNs - LOOKBACK_NANOSECONDS; const zoned = frameZoned(Temporal.Instant.fromEpochNanoseconds(startNs), schedule.frame); try { return { bucket: zonedUnitStart(zoned, "day"), atRangeStart }; } catch (error) { if (error instanceof RangeError) return { bucket: zoned, atRangeStart }; throw error; } } /** The windows that apply on an epoch day: an override, else nothing on a holiday, else the weekday's. */ function windowsOnDay(schedule, epochDay) { const override = schedule.overrides.get(epochDay); if (override !== void 0) return override; return hasEpochDay(schedule.holidays, epochDay) ? [] : schedule.weekly[isoWeekdayOf(epochDay)]; } /** The windows that apply on `date`. */ function windowsOn(schedule, date) { return windowsOnDay(schedule, epochDayOfDate(date)); } /** * Resolve one window edge with `resolveLocal`. An edge on a date Temporal cannot hold is already * outside the range. A wall time outside Temporal's range fails under every policy, so it * resolves just outside the range, on the side its date lies. `undefined` means `"reject"` * refused it. */ function resolveEdge(edge, timeZone, disambiguation) { if (typeof edge === "bigint") return { ns: edge, text: "" }; const local = `${edge.date.toString()}T${edge.time}`; const text = resolveLocalInstant(local, timeZone, disambiguation); if (text !== "") return { ns: Temporal.Instant.from(text).epochNanoseconds, text }; if (disambiguation === "reject" && resolveLocalInstant(local, timeZone, "compatible") !== "") return; return { ns: edge.date.year > 0 ? PAST_LAST_INSTANT : BEFORE_FIRST_INSTANT, text: "" }; } /** The local date after `date`, or `null` past Temporal's last date. */ function dateAfter(date) { try { return date.add({ days: 1 }); } catch (error) { if (error instanceof RangeError) return null; throw error; } } /** The local date before `date`, or `null` before Temporal's first date. */ function dateBefore(date) { try { return date.subtract({ days: 1 }); } catch (error) { if (error instanceof RangeError) return null; throw error; } } /** The widest span a window with an unresolved edge could cover: its start `"earlier"`, its end `"later"`. */ function widestSpan(startEdge, endEdge, timeZone) { return { start: resolveEdge(startEdge, timeZone, "earlier")?.ns ?? BEFORE_FIRST_INSTANT, end: resolveEdge(endEdge, timeZone, "later")?.ns ?? PAST_LAST_INSTANT }; } /** Where a window ends: its `to` on its own date, or on the next if it wraps midnight. */ function windowEndEdge(window, date, next) { const endDate = window.wraps ? next : date; if (endDate !== null) return { date: endDate, time: window.to }; return window.wraps ? PAST_LAST_INSTANT : BEFORE_FIRST_INSTANT; } /** * Resolve the windows of one local date to instants with `resolveLocal`. Each edge is resolved on * its own under `disambiguation`; a wrapping window's `to` is read on the next date. An edge pair * that resolves to an empty or inverted span (a window shorter than the gap it straddles) is * dropped. * * `date` is `null` for the date before Temporal's first, and `next` is `null` for the date after * its last. A window is a rule about the local clock, so it still holds on the side of it that * Temporal can reach: an edge on a date Temporal cannot hold lies just outside the range. * * Under `"earlier"`, an edge in a gap moves back by the gap's length (TC39 * DisambiguatePossibleEpochNanoseconds), so a date whose midnight is skipped can open before its * own first instant, on the previous date. No zone has jumped forward by more than a day, so a * window never starts before the previous date's first instant. * * Under `"reject"`, a window with an ambiguous or nonexistent edge is not guessed: it is returned * in `unresolved` as the widest span it could cover (its start read `"earlier"`, its end * `"later"`), so the caller can refuse an answer it might change and ignore it elsewhere. */ function resolveDateWindows(schedule, windows, date, next, disambiguation) { const records = []; const unresolved = []; const { timeZone } = schedule; for (const window of windows) { const startEdge = date === null ? BEFORE_FIRST_INSTANT : { date, time: window.from }; const endEdge = windowEndEdge(window, date, next); const start = resolveEdge(startEdge, timeZone, disambiguation); const end = resolveEdge(endEdge, timeZone, disambiguation); if (start === void 0 || end === void 0) { unresolved.push(widestSpan(startEdge, endEdge, timeZone)); continue; } if (start.ns < end.ns) records.push({ start: start.ns, end: end.ns, startText: start.text, endText: end.text }); } return { records, unresolved }; } /** * Resolve the windows of the local date before `date`, the first date of a walk that starts at * the earliest instant its frame can place. * * A window that opened on that date and wraps past midnight is still open on `date`, although * the instant it opened at is before the range. The previous date follows its own override or * holiday like any other. West of UTC the first date is Temporal's first, `-271821-04-19`, and * the date before it cannot be held or named: no override or holiday can be written for it, so * the weekly pattern applies, by the weekday before `date`'s. */ function resolveWindowsBefore(schedule, date, disambiguation) { const previous = dateBefore(date); return resolveDateWindows(schedule, previous === null ? schedule.weekly[date.dayOfWeek === 1 ? 7 : date.dayOfWeek - 1] : windowsOn(schedule, previous), previous, date, disambiguation); } /** * Resolve the windows a walk gains on reaching `date`: the date's own and, for the first date of * a walk from the start of the range (`carryIn`), those of the date before it. */ function resolveVisit(schedule, date, carryIn, disambiguation) { const own = resolveDateWindows(schedule, windowsOn(schedule, date), date, dateAfter(date), disambiguation); if (!carryIn) return own; const before = resolveWindowsBefore(schedule, date, disambiguation); return { records: [...before.records, ...own.records], unresolved: [...before.unresolved, ...own.unresolved] }; } /** The state a walk shares between the two ways of reaching dates: pending runs and the bound. */ function startWalk(fromNs, visitor) { let pending = []; let unresolvedStart; let bound; const finish = () => ({ unresolvedStart }); const flush = () => { for (const run of pending) if (visitor.run(run)) break; return finish(); }; return { flush, /** * Hand the visitor the runs no later window can extend, and announce the bound. Returns the * walk's result when the visitor stops it, else `undefined`. */ settle() { if (bound === void 0) return; const settleBefore = bound; for (const run of pending) { if (run.end >= settleBefore) break; if (visitor.run(run)) return finish(); } pending = pending.filter((run) => run.end >= settleBefore); return visitor.day(settleBefore, pending) ? flush() : void 0; }, /** Take a date's windows; `start` is the first instant of the date. */ add(resolved, start) { for (const window of resolved.unresolved) if (window.end > fromNs && (unresolvedStart === void 0 || window.start < unresolvedStart)) unresolvedStart = window.start; pending = coalesceIntervalNanoseconds([...pending, ...resolved.records]); bound = start; } }; } /** * Walk the schedule's local dates from a little before `fromNs`, handing the visitor each open * run as soon as no later window can extend it. * * - Dates come from the day buckets `floorToZone` and `bucketRange` use, so a deleted date * (`Pacific/Apia`, 2011-12-30) is never visited, and a date the clock re-enters after a * fall-back (`America/Goose_Bay`, 2010-11-06) is visited once. * - A window can start as early as the previous date's first instant (see `resolveDateWindows`), * so the walk settles one date behind: once a date is resolved, the first instant of the date * resolved before it is the `bound` no later window starts before. * - A walk from the start of Temporal's range also resolves the date before its first, whose * wrapping windows are still open when the range begins (`resolveWindowsBefore`). * - When the visitor stops the walk from `day`, or the last representable day is reached, the * runs still pending are handed over too. * - Returns `null` when a day bucket could not be found, or the walk would resolve a date * `MAX_SCHEDULE_DAYS + 2` after the one `fromNs` falls on. Neither use reaches that: a range * is refused up front past `MAX_SCHEDULE_DAYS` dates, and a search's horizon ends with the * `MAX_SCHEDULE_DAYS`th date, so its walk stops by the horizon one date later. */ function walkSchedule(schedule, fromNs, disambiguation, visitor, timeline) { return walkNearsLimits(fromNs) ? walkScheduleByBuckets(schedule, fromNs, disambiguation, visitor) : walkScheduleByDays(schedule, fromNs, disambiguation, visitor, timeline); } /** * `walkSchedule` date by date through Temporal, for a walk that could come near Temporal's limits, * where every date, bucket and wall time keeps the range rules (`resolveEdge`). */ function walkScheduleByBuckets(schedule, fromNs, disambiguation, visitor) { const walk = startWalk(fromNs, visitor); const limitDate = dateLimit(schedule, fromNs, 10002); const first = firstWalkBucket(schedule, fromNs); let current = first.bucket; let carryIn = first.atRangeStart; let lastDate = null; for (let bucket = 0; bucket < MAX_SCHEDULE_BUCKETS; bucket++) { if (current === null) return null; const stopped = walk.settle(); if (stopped !== void 0) return stopped; const date = current.toPlainDate(); if (lastDate === null || Temporal.PlainDate.compare(date, lastDate) > 0) { if (isFromDate(date, limitDate)) return null; const resolved = resolveVisit(schedule, date, carryIn, disambiguation); carryIn = false; lastDate = date; walk.add(resolved, frameEpochNanoseconds(current, schedule.frame)); } const following = nextDayBucket(current); if (following === void 0) return walk.flush(); current = following; } return null; } /** True when `date` is `limit` or later; never when there is no limit. */ function isFromDate(date, limit) { return limit !== null && Temporal.PlainDate.compare(date, limit) >= 0; } /** The bucket of the next day, or `undefined` when it starts past Temporal's last instant. */ function nextDayBucket(current) { try { return nextZonedBucketStart(current, "day"); } catch (error) { if (error instanceof RangeError) return; throw error; } } /** * Walk dates as epoch days read from the offset timeline, without a `ZonedDateTime` per date. * Same contract as `walkScheduleByBuckets`, for a walk that stays clear of Temporal's limits. * `shared` is a timeline a caller already built that reaches at least nine days before `fromNs`, * so pieces of one range read the zone's offset changes once. * Dates ascend and are visited once: a date whose first instant is the next date's (a deleted * date, `Pacific/Apia` 2011-12-30) is skipped, and a re-entered date (`America/Goose_Bay` * 2010-11-06) is one date. */ function walkScheduleByDays(schedule, fromNs, disambiguation, visitor, shared) { const walk = startWalk(fromNs, visitor); const startNs = fromNs - LOOKBACK_NANOSECONDS; const timeline = shared ?? offsetTimeline(schedule.frame, startNs - 6n * 86400000000000n, startNs + 14n * 86400000000000n); const limitDay = localDayOf(timeline, fromNs) + MAX_SCHEDULE_DAYS + 2; let day = localDayOf(timeline, startNs); for (let count = 0; count < MAX_SCHEDULE_BUCKETS; count++, day++) { ensureTimeline(timeline, BigInt(day + 6) * NANOSECONDS_PER_DAY); const start = startOfDayNs(timeline, day); if (start === startOfDayNs(timeline, day + 1)) continue; const stopped = walk.settle(); if (stopped !== void 0) return stopped; if (day >= limitDay) return null; walk.add(resolveDayWindows(windowsOnDay(schedule, day), timeline, day, disambiguation), start); } return null; } /** The local epoch day `ns` falls on in the timeline's frame. */ function localDayOf(timeline, ns) { return Number(floorDivide(ns + offsetOf(timeline, ns), NANOSECONDS_PER_DAY)); } /** `resolveDateWindows` for a date well inside Temporal's range, read from the timeline. */ function resolveDayWindows(windows, timeline, day, disambiguation) { const records = []; const unresolved = []; const midnight = BigInt(day) * NANOSECONDS_PER_DAY; for (const window of windows) { const startWall = midnight + window.fromNs; const endWall = (window.wraps ? midnight + NANOSECONDS_PER_DAY : midnight) + window.toNs; const start = resolveWallNs(timeline, startWall, disambiguation); const end = resolveWallNs(timeline, endWall, disambiguation); if (start === void 0 || end === void 0) unresolved.push({ start: resolveWallNs(timeline, startWall, "earlier") ?? BEFORE_FIRST_INSTANT, end: resolveWallNs(timeline, endWall, "later") ?? PAST_LAST_INSTANT }); else if (start < end) records.push({ start, end, startText: "", endText: "" }); } return { records, unresolved }; } /** * Days either side of Temporal's range that the closed forms and the date walk keep clear of; * a walk that could come nearer takes the date-by-date path, which reads the limits itself. */ const LIMIT_MARGIN = 30n * NANOSECONDS_PER_DAY; /** True when a walk from `fromNs` could reach within `LIMIT_MARGIN` of Temporal's range. */ function walkNearsLimits(fromNs) { return fromNs - LOOKBACK_NANOSECONDS - LIMIT_MARGIN < -8640000000000000000000n || fromNs + BigInt(10003) * 86400000000000n + LIMIT_MARGIN > 8640000000000000000000n; } /** * True when a range spans more than MAX_SCHEDULE_DAYS local dates: it is refused before walking. * Walking thousands of dates only to return the sentinel took seconds. */ function spansTooManyDates(schedule, range) { const lastInstant = range.end > range.start ? range.end - 1n : range.end; const limit = dateLimit(schedule, range.start, MAX_SCHEDULE_DAYS); return limit !== null && Temporal.PlainDate.compare(localDateOf(schedule, lastInstant), limit) >= 0; } /** * The text of a run's edges: the range's own where the run was clipped to it, else the instant's * UTC string, written here and only for the runs a caller returns. */ function runText(run) { return { start: run.startText === "" ? instantText(run.start) : run.startText, end: run.endText === "" ? instantText(run.end) : run.endText }; } /** * The schedule's open runs clipped to `[startNs, endNs)`, in the range's own strings where a run * is clipped. `null` when the walk fails (see `walkSchedule`), or when a window rejected under * `"reject"` reaches into the range. */ function scheduleRunsWithin(schedule, range, disambiguation) { return spansTooManyDates(schedule, range) ? null : walkedRunsWithin(schedule, range, disambiguation); } /** * `scheduleRunsWithin` for a range already known to span few enough local dates, as every piece * of such a range does: a piece starts no earlier and ends no later, so it cannot reach the limit. */ function walkedRunsWithin(schedule, range, disambiguation, timeline) { const runs = []; const walked = walkSchedule(schedule, range.start, disambiguation, { run: (run) => { if (run.start >= range.end) return true; if (run.end > range.start) { const clipStart = run.start < range.start; const clipEnd = run.end > range.end; runs.push({ start: clipStart ? range.start : run.start, end: clipEnd ? range.end : run.end, startText: clipStart ? range.startText : run.startText, endText: clipEnd ? range.endText : run.endText }); } return false; }, day: (bound) => bound >= range.end }, timeline); if (walked === null || walked.unresolvedStart !== void 0 && walked.unresolvedStart < range.end) return null; return runs.filter((run) => run.start < run.end); } /** The later of two instants. */ function later(a, b) { return a > b ? a : b; } /** * Walk from `fromNs` until a caller can answer. `atRun` sees each finished run that ends after * `fromNs`, and `atDay` the first pending run that does when no later window can start before * `bound`; each returns the answer, or `undefined` to keep walking. Past `horizonNs` the walk * stops unanswered. `null` when the walk fails. */ function firstAnswer(schedule, fromNs, horizonNs, disambiguation, atRun, atDay) { let answer; const walked = walkSchedule(schedule, fromNs, disambiguation, { run: (run) => { if (answer === void 0 && run.end > fromNs) answer = atRun(run); return answer !== void 0; }, day: (bound, pending) => { if (bound > horizonNs) return true; answer = atDay(bound, pending.find((run) => run.end > fromNs)); return answer !== void 0; } }); return walked === null ? null : { answer, unresolvedStart: walked.unresolvedStart }; } /** * The first instant at or after `fromNs` that the schedule is open: `fromNs` itself inside an * open run, else the start of the next run. Answered as soon as no later window can open * sooner, without waiting for the run to close. `undefined` when none falls at or before * `horizonNs`, `null` when the walk fails or a window rejected under `"reject"` could open * sooner. */ function firstOpenInstant(schedule, fromNs, horizonNs, disambiguation) { const walked = firstAnswer(schedule, fromNs, horizonNs, disambiguation, (run) => later(run.start, fromNs), (bound, next) => next !== void 0 && later(next.start, fromNs) <= bound ? later(next.start, fromNs) : void 0); if (walked === null) return null; const { answer, unresolvedStart } = walked; if (answer === void 0 || answer > horizonNs) return; return answer > fromNs && unresolvedStart !== void 0 && unresolvedStart < answer ? null : answer; } /** * The first instant at or after `fromNs` that the schedule is closed: `fromNs` itself outside * every open run, else the end of the run holding it. A closed `fromNs` is answered as soon as * no later window can cover it. `undefined` when the answer falls after `horizonNs`, `null` when * the walk fails or a window rejected under `"reject"` could keep the schedule open past it. */ function firstClosedInstant(schedule, fromNs, horizonNs, disambiguation) { const walked = firstAnswer(schedule, fromNs, horizonNs, disambiguation, (run) => run.start <= fromNs ? run.end : fromNs, (bound, next) => fromNs < bound && (next === void 0 || next.start > fromNs) ? fromNs : void 0); if (walked === null) return null; const closed = walked.answer ?? fromNs; if (closed > horizonNs) return; const { unresolvedStart } = walked; return unresolvedStart !== void 0 && unresolvedStart <= closed ? null : closed; } /** * The instant at which `amountNs` of open time has elapsed since `fromNs`: the earliest `X` * with exactly that much open time in `[fromNs, X)`. `fromNs` itself when `amountNs` is zero. * Answered as soon as no later window can add open time before `X`. `undefined` when `X` falls * after `horizonNs`, `null` when the walk fails or a window rejected under `"reject"` could add * open time before `X`. */ function instantAfterOpenTime(schedule, fromNs, amountNs, horizonNs, disambiguation) { if (amountNs === 0n) return fromNs; const jump = jumpOverWholeDays(schedule, fromNs, amountNs, horizonNs, disambiguation); return walkForOpenTime(schedule, jump?.startNs ?? fromNs, jump?.remaining ?? amountNs, horizonNs, disambiguation); } /** `instantAfterOpenTime` by walking dates from `fromNs`. */ function walkForOpenTime(schedule, fromNs, amountNs, horizonNs, disambiguation) { let remaining = amountNs; const walked = firstAnswer(schedule, fromNs, horizonNs, disambiguation, (run) => { const start = later(run.start, fromNs); if (run.end - start >= remaining) return start + remaining; remaining -= run.end - start; }, (bound, next) => { if (next === void 0) return; const deadline = later(next.start, fromNs) + remaining; return deadline <= next.end && deadline <= bound ? deadline : void 0; }); if (walked === null) return null; const { answer, unresolvedStart } = walked; if (answer === void 0 || answer > horizonNs) return; return unresolvedStart !== void 0 && unresolvedStart < answer ? null : answer; } const NANOSECONDS_PER_UNIT = { hours: 3600000000000n, minutes: 60000000000n, seconds: 1000000000n, milliseconds: 1000000n, microseconds: 1000n, nanoseconds: 1n }; /** * An amount of open time in nanoseconds, from an ISO duration of hours and smaller units. * `null` for a negative duration, for any calendar unit (years, months, weeks or days — `P1D` * could mean 24 open hours or one working day, so it is not guessed), or invalid input. */ function parseOpenTimeAmount(value) { if (typeof value !== "string") return null; try { const duration = durationFrom(value); if (duration.sign < 0 || duration.years !== 0 || duration.months !== 0 || duration.weeks !== 0 || duration.days !== 0) return null; return Object.keys(NANOSECONDS_PER_UNIT).reduce((total, unit) => total + BigInt(duration[unit]) * NANOSECONDS_PER_UNIT[unit], 0n); } catch { return null; } } /** Epoch nanoseconds as a UTC instant string ending in `Z`. */ function instantText(nanoseconds) { return Temporal.Instant.fromEpochNanoseconds(nanoseconds).toString(); } /** `a / b` rounded toward negative infinity, for a positive `b`. */ function floorDivide(a, b) { const quotient = a / b; return a % b < 0n ? quotient - 1n : quotient; } /** `a / b` rounded toward positive infinity, for a positive `b`. */ function ceilDivide(a, b) { return -floorDivide(-a, b); } /** Length of the union of `spans`, each `[start, end)`. */ function unionLength(spans) { spans.sort((x, y) => x[0] < y[0] ? -1 : x[0] > y[0] ? 1 : 0); let total = 0n; let reached = 0n; for (const [start, end] of spans) { const from = start > reached ? start : reached; if (end > from) { total += end - from; reached = end; } } return total; } /** * Open time inside the day bucket of a quiet date: the tails of the previous date's wrapping * windows (`[0, to)`) and the date's own windows (`[from, to)`, or `[from, 1 day)` when wrapping), * as a union. */ function dayCoverage(previous, current) { const spans = []; for (const window of previous) if (window.wraps) spans.push([0n, window.toNs]); for (const window of current) spans.push([window.fromNs, window.wraps ? NANOSECONDS_PER_DAY : window.toNs]); return unionLength(spans); } /** * The maximal blocks of quiet dates lying wholly inside `range`, ascending. A date `d` is quiet * when one stretch, offset `o`, holds every instant from `(d - 2)` days `- o` to `(d + 3)` days * `- o`: every wall time on `d - 1`, `d` and `d + 1` then has the one candidate `wall - o` under * every policy, and `d`'s bucket is exactly one day. A refused edge has a change within a day of * it, so none reaches a quiet date. */ function quietBlocks(timeline, range) { const { changes, offsets } = timeline; const day = NANOSECONDS_PER_DAY; const blocks = []; for (let index = 0; index < offsets.length; index++) { const offset = offsets[index]; const low = index === 0 ? timeline.fromNs : changes[index - 1]; const high = index === changes.length ? timeline.toNs : changes[index]; const first = ceilDivide(low + offset, day) + 2n; const last = floorDivide(high - 1n + offset, day) - 3n; const insideFirst = ceilDivide(range.start + offset, day); const insideLast = floorDivide(range.end + offset, day) - 1n; const firstDay = first > insideFirst ? first : insideFirst; const lastDay = last < insideLast ? last : insideLast; if (firstDay <= lastDay) blocks.push({ firstDay: Number(firstDay), lastDay: Number(lastDay), startNs: firstDay * day - offset, endNs: (lastDay + 1n) * day - offset }); } return blocks; } /** The open time each date adds to the plain week, indexed by ISO weekday (index 0 is unused). */ function plainWeekCoverage(schedule) { const plain = [0n]; for (let weekday = 1; weekday <= 7; weekday++) plain.push(dayCoverage(schedule.weekly[weekday === 1 ? 7 : weekday - 1], schedule.weekly[weekday])); return plain; } /** * What the holiday and override dates of a quiet block change from the plain week. A date differs * from it when it or the date before it is a holiday or override. */ function specialDayCorrection(schedule, block, plain) { const { specials } = schedule; let total = 0n; let handled = block.firstDay - 1; for (let index = lowerBound(specials, block.firstDay - 1); index < specials.length && specials[index] <= block.lastDay; index++) for (const day of [specials[index], specials[index] + 1]) if (day > handled && day >= block.firstDay && day <= block.lastDay) { total += dayCoverage(windowsOnDay(schedule, day - 1), windowsOnDay(schedule, day)) - plain[isoWeekdayOf(day)]; handled = day; } return total; } /** Open time over the dates of a quiet block, from the weekly pattern and the special days. */ function quietBlockOpenTime(schedule, block) { const plain = plainWeekCoverage(schedule); const dates = block.lastDay - block.firstDay + 1; const leftover = dates % 7; let week = 0n; for (let weekday = 1; weekday <= 7; weekday++) week += plain[weekday]; let total = week * BigInt(Math.floor(dates / 7)); for (let offset = 0; offset < leftover; offset++) total += plain[isoWeekdayOf(block.firstDay + offset)]; return total + specialDayCorrection(schedule, block, plain); } /** * Open time in `[start, end)` by walking its dates, or `null` as `scheduleRunsWithin` is. * `timeline` is one a caller already built for the pieces of a range. */ function walkedOpenTime(schedule, start, end, disambiguation, timeline) { const runs = walkedRunsWithin(schedule, { start, end, startText: "", endText: "" }, disambiguation, timeline); return runs === null ? null : runs.reduce((total, run) => total + (run.end - run.start), 0n); } /** * Open time in `[range.start, range.end)`, in nanoseconds; `null` exactly when * `scheduleRunsWithin` would be. * * Open time is additive over disjoint half-open pieces. Whole day buckets inside a stretch of * constant offset are summed in closed form (whole weeks of the weekly pattern, plus a correction * for each holiday and override); the pieces between them, the range's ends and the dates near an * offset change, are walked. */ function openTimeWithin(schedule, range, disambiguation) { if (spansTooManyDates(schedule, range)) return null; let total = 0n; let cursor = range.start; let timeline; if (!walkNearsLimits(range.start)) { timeline = offsetTimeline(schedule.frame, range.start - 12n * NANOSECONDS_PER_DAY, range.end + 6n * NANOSECONDS_PER_DAY); for (const block of quietBlocks(timeline, range)) { const piece = walkedOpenTime(schedule, cursor, block.startNs, disambiguation, timeline); if (piece === null) return null; total += piece + quietBlockOpenTime(schedule, block); cursor = block.endNs; } } const last = walkedOpenTime(schedule, cursor, range.end, disambiguation, timeline); return last === null ? null : total + last; } /** * Where `instantAfterOpenTime` can start walking: the first instant of the latest local date * whose start still has less than `amountNs` of open time before it, and the open time left after * that. A date that starts at or before `fromNs` (a fall-back repeats local midnight) starts at * `fromNs` instead, with nothing counted before it: counting from an earlier start would add open * time that precedes `fromNs`. Equal by additivity: the answer `X` has * `open[fromNs, X) = amountNs`, so it lies after that start. `undefined` when no date qualifies, when the walk could near Temporal's limits, or * when `openTimeWithin` is `null` (a window refused under `"reject"`): the walk then starts at * `fromNs`. */ function jumpOverWholeDays(schedule, fromNs, amountNs, horizonNs, disambiguation) { if (walkNearsLimits(fromNs)) return; const timeline = offsetTimeline(schedule.frame, fromNs - 6n * NANOSECONDS_PER_DAY, fromNs + 14n * NANOSECONDS_PER_DAY); const firstDay = localDayOf(timeline, fromNs); const lastDay = localDayOf(timeline, horizonNs); const before = (day) => { ensureTimeline(timeline, BigInt(day + 6) * NANOSECONDS_PER_DAY); const dayStart = startOfDayNs(timeline, day); const startNs = dayStart > fromNs ? dayStart : fromNs; const open = openTimeWithin(schedule, { start: fromNs, end: startNs, startText: "", endText: "" }, disambiguation); return open === null ? void 0 : [startNs, open]; }; const found = latestDayBelow(firstDay, lastDay, amountNs, before); if (found === void 0) return; return { startNs: found.startNs, remaining: amountNs - found.open }; } /** * The latest date in `(firstDay, lastDay]` whose probe reads less than `amountNs`: gallop away * from `firstDay`, then bisect between the last date below and the first not. `undefined` when * there is none or a probe is `undefined`. */ function latestDayBelow(firstDay, lastDay, amountNs, before) { let good; let bad = lastDay + 1; for (let step = 1; firstDay + step <= lastDay; step *= 2) { const probed = before(firstDay + step); if (probed === void 0) return; if (probed[1] >= amountNs) { bad = firstDay + step; break; } good = { day: firstDay + step, startNs: probed[0], open: probed[1] }; } while (good !== void 0 && bad - good.day > 1) { const middle = good.day + Math.floor((bad - good.day) / 2); const probed = before(middle); if (probed === void 0) return; if (probed[1] >= amountNs) bad = middle; else good = { day: middle, startNs: probed[0], open: probed[1] }; } return good; } export { firstClosedInstant, firstOpenInstant, instantAfterOpenTime, instantText, openTimeWithin, parseOpenTimeAmount, parseOperatingSchedule, parseScheduleDisambiguation, parseSearchHorizon, parseWeeklyPattern, runText, scheduleRunsWithin };