Skip to content

longestRun

longestRun(log: DutyEntry[], window: Interval, statuses: string[]): { interval: Interval; duration: string; } | null

Return the longest unbroken stretch a duty log spends in any of statuses inside a window.

  • A stretch is a run of touching entries whose statuses are all in statuses; another status or an unlogged gap ends it. Each run is cut to window (half-open) before it is measured.
  • On a tie the earlier stretch is returned.
  • interval is in UTC instants ending in “Z”; duration is an exact ISO 8601 duration with hours as the largest unit.
  • Returns null when no stretch falls inside the window, which is also the sentinel: 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 search inside
statusesstring[]the statuses a stretch is made of

{ interval, duration } of the longest stretch, or null when there is none or on invalid input

Members

MemberTypeDescription
intervalIntervalThe stretch, as UTC instants ending in “Z”, cut to the window.
durationstringIts length, an exact ISO 8601 duration with hours as the largest unit.
import { longestRun } from "@northguild/gmt/duty/calculate";
longestRun([{ status: "driving", start: "2024-01-01T00:00:00Z", end: "2024-01-01T03:00:00Z" }, { status: "onDuty", start: "2024-01-01T03:00:00Z", end: "2024-01-01T04:00:00Z" }, { status: "driving", start: "2024-01-01T04:00:00Z", end: "2024-01-01T08:00:00Z" }], { start: "2024-01-01T00:00:00Z", end: "2024-01-02T00:00:00Z" }, ["driving"]) // { interval: { start: "2024-01-01T04:00:00Z", end: "2024-01-01T08:00:00Z" }, duration: "PT4H" }
log
statuses
output 

duty/calculate/longestRun.ts