@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.
59 lines (57 loc) • 4.27 kB
JavaScript
import { parseInstantNanoseconds } from "../../internal/instantNanoseconds.js";
import { instantAfterOpenTime, instantText, parseOpenTimeAmount, parseOperatingSchedule, parseScheduleDisambiguation, parseSearchHorizon } from "../../internal/operatingSchedule.js";
// src/calendar/hours/addOperatingTime.ts
/**
* Return the instant at which `duration` of open time has elapsed since `start`: the deadline
* of a service level measured in working hours.
*
* - The earliest instant `X` such that `operatingTimeBetween(start, X, schedule)` equals
* `duration`. The clock stops while the schedule is closed and resumes when it reopens, so an
* eight-hour SLA raised at Friday 16:00 on a 09:00–17:00 weekday desk falls due on Monday at
* 16:00. A deadline that lands exactly on a closing time is that closing instant, not the next
* opening.
* - `duration` is hours and smaller units (`"PT8H"`, `"PT90M"`). A duration with days, weeks,
* months or years returns `""`: `P1D` of open time could mean 24 open hours or one working
* day, and GMT does not guess. `"PT0S"` returns `start` itself, in UTC.
* - Returns `""` when the deadline would fall past the search horizon, rather than searching
* forever.
* - Holidays and overrides apply as in `operatingIntervals`.
* - Returns `""` on invalid input: an invalid instant, duration or `OperatingSchedule`, a
* negative `duration`, a `within` that is not a non-negative ISO duration, a `disambiguation`
* that is not one of the four values, and a search that would go more than 10,000 local dates past the input's date.
* - **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 last instant Temporal supports
* (`+275760-09-13T00:00:00Z`) for an offset east of UTC, or of the first
* (`-271821-04-20T00:00:00Z`) for one west, this returns `""`. An offset to the minute has no
* such limit.
*
* @param start ISO 8601 instant string where the clock starts
* @param duration ISO 8601 duration of open time, hours and smaller units (e.g. "PT8H")
* @param schedule `{ timeZone, weekly, holidays?, overrides? }` operating schedule
* @param optionsArg How far the search goes, and how a window edge on a clock change is resolved
* @returns UTC instant string ending in "Z", or "" when past the horizon or on invalid input
*
* @example addOperatingTime("2024-06-14T15:00:00Z", "PT2H", { timeZone: "Europe/London", weekly: { 1: [{ from: "09:00", to: "17:00" }], 5: [{ from: "09:00", to: "17:00" }] } }) // "2024-06-17T09:00:00Z" — Friday 16:00 to Monday 10:00 local
* @example addOperatingTime("2024-06-14T15:00:00Z", "PT1H", { timeZone: "Europe/London", weekly: { 5: [{ from: "09:00", to: "17:00" }] } }) // "2024-06-14T16:00:00Z" — due at closing, not at the next opening
* @example addOperatingTime("2024-06-14T15:00:00Z", "PT2H", { timeZone: "Europe/London", weekly: { 1: [{ from: "09:00", to: "17:00" }], 5: [{ from: "09:00", to: "17:00" }] } }, { within: "P1D" }) // "" — past the horizon
* @example addOperatingTime("2024-06-15T12:00:00Z", "PT0S", { timeZone: "UTC", weekly: {} }) // "2024-06-15T12:00:00Z"
* @example addOperatingTime("2024-06-14T20:00:00Z", "P1D", { timeZone: "UTC", weekly: { 5: [{ from: "09:00", to: "17:00" }] } }) // "" — days are not open time
* @example addOperatingTime("2024-06-14T20:00:00Z", "-PT1H", { timeZone: "UTC", weekly: { 5: [{ from: "09:00", to: "17:00" }] } }) // ""
*/
function addOperatingTime(start, duration, schedule, optionsArg) {
try {
const disambiguation = parseScheduleDisambiguation(optionsArg);
const resolved = parseOperatingSchedule(schedule);
const from = parseInstantNanoseconds(start);
const amount = parseOpenTimeAmount(duration);
if (disambiguation === null || resolved === null || from === null || amount === null) return "";
const horizon = parseSearchHorizon(from, optionsArg?.within, resolved.frame);
if (horizon === null) return "";
const answer = instantAfterOpenTime(resolved, from, amount, horizon, disambiguation);
return typeof answer === "bigint" ? instantText(answer) : "";
} catch {
return "";
}
}
export { addOperatingTime };