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.

26 lines • 2.05 kB
import { Interval } from "../../types/interval.js"; import { DutyEntry } from "../../types/duty.js"; import "../../types/index.js"; /** * Return the time a duty log spends in any of `statuses` inside a window, as an exact ISO 8601 * duration. * * - The interval algebra of CORE-6: the entries in `statuses` are merged, each run is cut to * `window` (half-open), and what is left is summed. An entry that crosses the window's edge * counts only its part inside. * - Hours are the largest unit, as `sumIntervals` writes it: `"PT49H30M"`, never days. * - `[]` as `statuses`, an empty log or an empty window gives `"PT0S"`. * - Returns `""` when the log fails `isValidDutyLog`, when `window` is not a valid `Interval`, and * when `statuses` is not an array of strings. * * @param log the duty log, ordered and non-overlapping * @param window `{ start, end }` record of ISO 8601 instant strings to total inside * @param statuses the statuses whose time is totalled * @returns ISO 8601 duration string, or "" on invalid input * * @example dutyTotals([{ status: "driving", start: "2024-01-01T10:00:00Z", end: "2024-01-01T15:00:00Z" }, { status: "onDuty", start: "2024-01-01T15:00:00Z", end: "2024-01-01T17:00:00Z" }, { status: "driving", start: "2024-01-01T17:00:00Z", end: "2024-01-01T20:00:00Z" }], { start: "2024-01-01T00:00:00Z", end: "2024-01-02T00:00:00Z" }, ["driving"]) // "PT8H" * @example dutyTotals([{ status: "driving", start: "2024-01-01T10:00:00Z", end: "2024-01-01T15:00:00Z" }], { start: "2024-01-01T12:00:00Z", end: "2024-01-01T18:00:00Z" }, ["driving"]) // "PT3H" — cut at the window * @example dutyTotals([], { start: "2024-01-01T00:00:00Z", end: "2024-01-02T00:00:00Z" }, ["driving"]) // "PT0S" * @example dutyTotals([{ status: "driving", start: "2024-01-01T15:00:00Z", end: "2024-01-01T10:00:00Z" }], { start: "2024-01-01T00:00:00Z", end: "2024-01-02T00:00:00Z" }, ["driving"]) // "" — inverted entry */ export declare function dutyTotals(log: DutyEntry[], window: Interval, statuses: string[]): string;