@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.
55 lines (53 loc) • 3.56 kB
JavaScript
import { formatHourDuration } from "../../internal/hourDurationString.js";
import { parseIntervalNanoseconds } from "../../internal/intervalNanoseconds.js";
import { openTimeWithin, parseOperatingSchedule, parseScheduleDisambiguation } from "../../internal/operatingSchedule.js";
// src/calendar/hours/operatingTimeBetween.ts
/**
* Return how much open time `schedule` has between two instants, as an exact ISO duration.
*
* - The open time in the half-open range `[start, end)`: the length of its intersection with
* `operatingIntervals(schedule, { start, end })`, summed as `sumIntervals` sums. A support
* ticket raised Friday 16:00 and answered Monday 10:00 on a 09:00–17:00 weekday desk has
* waited `PT2H` of open time.
* - Hours are the largest unit (`PT40H`, not `P1DT16H`): an open-time total has no calendar.
* Exact to the nanosecond, and elapsed time, not wall-clock distance, so a 00:00–00:00 window
* on a London fall-back date adds 25 hours.
* - Holidays and overrides apply as in `operatingIntervals`.
* - `start === end` returns `"PT0S"`.
* - Returns `""` on invalid input: an invalid instant or `OperatingSchedule`, `start` after
* `end`, a `disambiguation` that is not one of the four values, and a range spanning more than
* 10,000 local dates (about 27 years) counted from the date `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 start ISO 8601 instant string where the clock starts (inclusive)
* @param end ISO 8601 instant string where the clock stops (exclusive)
* @param schedule `{ timeZone, weekly, holidays?, overrides? }` operating schedule
* @param optionsArg How a window edge on a clock change is resolved
* @returns ISO 8601 duration string of the open time, or "" on invalid input
*
* @example operatingTimeBetween("2024-06-14T10:30:00Z", "2024-06-17T04:30:00Z", { timeZone: "Asia/Kolkata", weekly: { 1: [{ from: "09:00", to: "17:00" }], 5: [{ from: "09:00", to: "17:00" }] } }) // "PT2H" — Friday 16:00 to Monday 10:00 local
* @example operatingTimeBetween("2024-06-15T00:00:00Z", "2024-06-16T00:00:00Z", { timeZone: "UTC", weekly: { 1: [{ from: "09:00", to: "17:00" }] } }) // "PT0S" — a closed Saturday
* @example operatingTimeBetween("2024-10-26T23:00:00Z", "2024-10-28T00:00:00Z", { timeZone: "Europe/London", weekly: { 7: [{ from: "00:00", to: "00:00" }] } }) // "PT25H" — the fall-back day
* @example operatingTimeBetween("2024-06-10T09:00:00Z", "2024-06-10T09:30:00.5Z", { timeZone: "UTC", weekly: { 1: [{ from: "09:00", to: "17:00" }] } }) // "PT30M0.5S"
* @example operatingTimeBetween("2024-06-17T00:00:00Z", "2024-06-10T00:00:00Z", { timeZone: "UTC", weekly: {} }) // "" — start after end
*/
function operatingTimeBetween(start, end, schedule, optionsArg) {
try {
const disambiguation = parseScheduleDisambiguation(optionsArg);
const resolved = parseOperatingSchedule(schedule);
const range = parseIntervalNanoseconds({
start,
end
});
if (disambiguation === null || resolved === null || range === null) return "";
const open = openTimeWithin(resolved, range, disambiguation);
return open === null ? "" : formatHourDuration(open);
} catch {
return "";
}
}
export { operatingTimeBetween };