UNPKG

@northguild/gmt

Version:

Date and time library for JavaScript and TypeScript, built on TC39 Temporal: time zones, DST, business days, ISO 8601 strings in and out. Never throws.

54 lines • 3.45 kB
/** * 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"`). * * @param start ISO PlainTime string for the interval start * @param end ISO PlainTime string for the interval end * @param unit duration unit string — `"hours" | "minutes" | "seconds" | "milliseconds" | "microseconds" | "nanoseconds"` (calendar units are ignored by PlainTime and return []) * @param amount positive number of units per step * @param options The limit on the size of the result * @returns array of `{ start, end }` records, or [] on invalid input * * @example 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" }] * @example splitIntervalByUnitTime("12:00:00", "14:30:00", "hour", 1) // [{ start: "12:00:00", end: "13:00:00" }, { start: "13:00:00", end: "14:00:00" }, { start: "14:00:00", end: "14:30:00" }] * @example splitIntervalByUnitTime("20:00:00", "23:00:00", "hour", 5) // [{ start: "20:00:00", end: "23:00:00" }] (20:00 + 5 hours is past midnight) * @example splitIntervalByUnitTime("12:00:00", "12:00:00", "hour", 1) // [{ start: "12:00:00", end: "12:00:00" }] * @example splitIntervalByUnitTime("12:00:00", "14:00:00", "hour", 0) // [] * @example splitIntervalByUnitTime("invalid", "14:00:00", "hour", 1) // [] * @example splitIntervalByUnitTime("12:00:00", "14:30:00", "hour", 1, { maxPieces: 2 }) // [] (3 slices exceed the limit) * @example splitIntervalByUnitTime("12:00:00", "14:30:00", "hour", 1, { maxPieces: 3 }) // [{ start: "12:00:00", end: "13:00:00" }, { start: "13:00:00", end: "14:00:00" }, { start: "14:00:00", end: "14:30:00" }] */ export declare function splitIntervalByUnitTime(start: string, end: string, unit: string, amount: number, options?: { /** * The 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 `[]`. * * @defaultValue `1_000_000` */ maxPieces?: number; }): Array<{ /** * The clock time the interval begins at, as an ISO 8601 time string with no date, offset or time * zone. */ start: string; /** * The 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. */ end: string; }>;