Skip to content

sumIntervals

sumIntervals(intervals: Interval[]): string

Return the time covered by a list of half-open intervals, as an exact ISO 8601 duration.

  • Covered time is the length of the union: overlapping time counts once, touching intervals add up, empty intervals add nothing.
  • Hours are the largest unit, exactly as Temporal.Instant.prototype.until with largestUnit: "hour" renders it — an instant has no calendar, so there are no days. PT49H30M, not P2DT1H30M.
  • Exact to the nanosecond at any size, including past 2^53 nanoseconds (about 104 days) and across the whole representable instant range (PT4800000000H).
  • Measures elapsed time, never wall-clock distance: a New York day across spring-forward is PT23H.
  • [] returns "PT0S" — no time covered, not invalid input.
  • Returns "" when intervals is not an array or any element is not a valid Interval.
ParameterTypeDescription
intervalsInterval[]array of { start, end } records of ISO 8601 instant strings

ISO 8601 duration string of the covered time, or "" on invalid input

import { sumIntervals } from "@northguild/gmt/interval/calculate";
sumIntervals([{ start: "2024-01-01T00:00:00Z", end: "2024-01-03T01:30:00Z" }]) // "PT49H30M"
intervals
output 

interval/calculate/sumIntervals.ts