Skip to content

intervalSplitAtTime

intervalSplitAtTime(start: string, end: string, points: string[]): { start: string; end: string; }[]
import { intervalSplitAtTime } from "@northguild/gmt/plain/interval";

Split a time interval at arbitrary points, producing consecutive sub-intervals.

  • points need not be sorted — they are sorted internally before splitting.
  • Points outside [start, end] are dropped; they cannot introduce a boundary that isn’t inside the interval.
  • Points exactly on start or end are dropped too — they would only produce a zero-length sub-interval at the edge, which divideEqually’s zero-length case is the deliberate way to express, not splitAt’s.
  • Duplicate points collapse to a single boundary.
  • Returns [{ start, end }] (the whole interval, unsplit) when no valid in-range point remains.
  • Returns [] when points is not an array, when any element is not a valid ISO PlainTime string, or on invalid input (unparseable start/end, start > end).
Parameter Type Description
start string ISO PlainTime string for the interval start
end string ISO PlainTime string for the interval end
points string[] array of ISO PlainTime strings to split at

array of { start, end } records, or [] on invalid input

intervalSplitAtTime("09:00:00", "17:00:00", ["12:00:00"]) // [{ start: "09:00:00", end: "12:00:00" }, { start: "12:00:00", end: "17:00:00" }]
intervalSplitAtTime("09:00:00", "17:00:00", ["15:00:00", "11:00:00"]) // [{ start: "09:00:00", end: "11:00:00" }, { start: "11:00:00", end: "15:00:00" }, { start: "15:00:00", end: "17:00:00" }]
intervalSplitAtTime("09:00:00", "17:00:00", ["09:00:00", "17:00:00", "20:00:00"]) // [{ start: "09:00:00", end: "17:00:00" }]
intervalSplitAtTime("09:00:00", "17:00:00", []) // [{ start: "09:00:00", end: "17:00:00" }]
intervalSplitAtTime("invalid", "17:00:00", ["12:00:00"]) // []
intervalSplitAtTime("09:00:00", "17:00:00", ["not-a-time"]) // []

plain/interval/intervalSplitAtTime.ts