Skip to content

qualifyingRestPairs

qualifyingRestPairs(log: DutyEntry[], spec: { statuses: string[]; minimum: string; pair: PairSpec; }): RestPair[]

Return every paired rest in a duty log: two separate rests that together count as one, because they meet all three floors of spec.pair.

  • A pair is three constraints, not two integers: with { eachAtLeast: "PT2H", oneAtLeast: "PT7H", combinedAtLeast: "PT10H" }, 7.5 + 2.5 hours and 8 + 2 qualify, and 6 + 4 does not.
  • Rests are runs of touching entries in spec.statuses. Two rests in a row pair; either may be the longer.
  • A rest of spec.minimum or more is a full rest on its own: it pairs with nothing, and no pair spans it. A rest shorter than eachAtLeast is neither half of a pair nor a break between two.
  • Pairs chain: rests of 7, 3 and 7 hours give two pairs, (7, 3) and (3, 7).
  • Intervals are UTC instants ending in “Z”, in log order.
  • [] is both a legitimate result and the sentinel: returned when the log fails isValidDutyLog, when spec is not an object, when statuses is not an array of strings, and when minimum or a floor of pair is not a positive ISO 8601 duration of exact time.
ParameterTypeDescription
logDutyEntry[]the duty log, ordered and non-overlapping

spec

the statuses a rest is made of, the length of a full rest, and the pair’s floors

OptionTypeDefaultDescription
statusesstring[]RequiredThe statuses a rest is made of. Touching entries in any of them are one rest.
minimumstringRequiredThe length of a full rest, a positive ISO 8601 duration of exact time ("PT10H"). A rest this long or longer is not half of a pair.
pairPairSpecRequiredThe three floors the two rests must meet.

the pairs as { first, second } records, or [] on invalid input

Two rest periods that satisfy a PairSpec, in the order they were taken.

import type { RestPair } from "@northguild/gmt";
MemberTypeDescription
firstIntervalThe earlier period, as UTC instants.
secondIntervalThe later period, as UTC instants.
import { qualifyingRestPairs } from "@northguild/gmt/duty/calculate";
qualifyingRestPairs([{ status: "sleeper", start: "2024-01-01T00:00:00Z", end: "2024-01-01T07:30:00Z" }, { status: "work", start: "2024-01-01T07:30:00Z", end: "2024-01-01T11:30:00Z" }, { status: "off", start: "2024-01-01T11:30:00Z", end: "2024-01-01T14:00:00Z" }], { statuses: ["off", "sleeper"], minimum: "PT10H", pair: { eachAtLeast: "PT2H", oneAtLeast: "PT7H", combinedAtLeast: "PT10H" } }) // [{ first: { start: "2024-01-01T00:00:00Z", end: "2024-01-01T07:30:00Z" }, second: { start: "2024-01-01T11:30:00Z", end: "2024-01-01T14:00:00Z" } }]
log
output 

duty/calculate/qualifyingRestPairs.ts