Skip to content

dutyTotals

dutyTotals(log: DutyEntry[], window: Interval, statuses: string[]): string

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.
ParameterTypeDescription
logDutyEntry[]the duty log, ordered and non-overlapping
windowInterval{ start, end } record of ISO 8601 instant strings to total inside
statusesstring[]the statuses whose time is totalled

ISO 8601 duration string, or "" on invalid input

import { dutyTotals } from "@northguild/gmt/duty/calculate";
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"
log
statuses
output 

duty/calculate/dutyTotals.ts