Skip to content

splitIntervalByUnitTime

splitIntervalByUnitTime(start: string, end: string, unit: string, amount: number, options?: { maxPieces?: number; }): { start: string; end: string; }[]

Split a time interval into sub-intervals of amount × unit.

  • Returns an array of { start, end } records that tile the interval, each record’s end equal to the next record’s start.
  • Every piece is half-open [start, end): a boundary belongs only to the piece that starts there, so the pieces share no value and together cover the interval exactly once (the rule CORE-6’s splitIntervalAt uses).
  • The final sub-interval is trimmed so its end never exceeds the original end.
  • Boundaries lie on the one day between start and end: PlainTime arithmetic wraps at midnight, but a step that reaches or passes midnight is past end, so that slice is trimmed to end ("12:00" to "23:00" by 12 hours is one slice).
  • Returns [{ start, end }] when start === end (zero-length interval).
  • Returns [] on invalid input (unparseable start/end, unsupported unit, non-positive amount, or a unit that has no effect on PlainTime, e.g. "days").
ParameterTypeDescription
startstringISO PlainTime string for the interval start
endstringISO PlainTime string for the interval end
unitstringduration unit string — "hours" | "minutes" | "seconds" | "milliseconds" | "microseconds" | "nanoseconds" (calendar units are ignored by PlainTime and return [])
amountnumberpositive number of units per step

options

The limit on the size of the result

OptionTypeDefaultDescription
maxPieces?number1_000_000The most records the result may hold. A result that would hold more, or more than the longest possible array (2^32 - 1 elements), returns [] and none is built. A value that is not a positive safe integer also returns [].

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

Members of each item

MemberTypeDescription
startstringThe clock time the interval begins at, as an ISO 8601 time string with no date, offset or time zone.
endstringThe first clock time after the interval, in the same format as start. It is exclusive: the interval holds everything from start up to but not including this value. It can equal start, which makes the interval empty.
import { splitIntervalByUnitTime } from "@northguild/gmt/plain/interval";
splitIntervalByUnitTime("12:00:00", "14:00:00", "hour", 1) // [{ start: "12:00:00", end: "13:00:00" }, { start: "13:00:00", end: "14:00:00" }]
output 

plain/interval/splitIntervalByUnitTime.ts