@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.
82 lines (80 loc) • 5.46 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isValidDateTimeDurationUnit } from "../../plain/validate/isValidDateTimeDurationUnit.js";
import { resolveDurationUnit } from "../../internal/resolveDurationUnit.js";
import { minSlicesForSpan, tileByUnit } from "../../internal/splitStep.js";
import { exceedsPieceLimit, resolveMaxPieces } from "../../internal/maxPieces.js";
import { isValidUtc } from "../validate/isValidUtc.js";
// src/utc/interval/splitIntervalByUnitUtc.ts
/**
* Split a UTC 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`.
* - Calendar-unit boundaries (years, months, weeks, days) are computed from `start`
* (`start + k × amount`), so month-end starts don't drift: a monthly split from January 31
* lands on February 29, March 31, April 30. Temporal's `add` constrains a day the month lacks
* to the month's last day, and stepping from that boundary would keep the shortened day
* (February 29, then March 29).
* - Each calendar-unit boundary is `start.add(amount × k)` with Temporal's
* `overflow: "constrain"` (TC39 Temporal `add`), never a step from the previous boundary.
* - Exact-unit boundaries (hours and smaller) step from the previous boundary. Exact units never
* clamp, and stepping keeps nanosecond precision where `k × amount` would pass
* `Number.MAX_SAFE_INTEGER`.
* - A step that resolves to the same instant as the previous boundary is skipped, so no empty
* slice is produced. A step that goes backwards returns `[]`.
* - Returns `[{ start, end }]` when `start === end` (zero-length interval).
* - Returns `[]` on invalid input (unparseable start/end, unsupported unit, non-positive amount,
* leap-second strings).
*
* @param start ISO UTC datetime string for the interval start
* @param end ISO UTC datetime string for the interval end
* @param unit duration unit string — any `DateTimeDurationUnit`
* @param amount positive number of units per step
* @param options A limit on the size of the result
* @returns array of `{ start, end }` records, or [] on invalid input
*
* @example splitIntervalByUnitUtc("2024-01-01T00:00:00Z", "2024-01-02T00:00:00Z", "hour", 6) // [{ start: "2024-01-01T00:00:00Z", end: "2024-01-01T06:00:00Z" }, { start: "2024-01-01T06:00:00Z", end: "2024-01-01T12:00:00Z" }, { start: "2024-01-01T12:00:00Z", end: "2024-01-01T18:00:00Z" }, { start: "2024-01-01T18:00:00Z", end: "2024-01-02T00:00:00Z" }]
* @example splitIntervalByUnitUtc("2024-01-01T00:00:00Z", "2024-01-01T01:30:00Z", "hour", 1) // [{ start: "2024-01-01T00:00:00Z", end: "2024-01-01T01:00:00Z" }, { start: "2024-01-01T01:00:00Z", end: "2024-01-01T01:30:00Z" }]
* @example splitIntervalByUnitUtc("2024-01-01T00:00:00Z", "2024-01-01T00:00:00Z", "hour", 1) // [{ start: "2024-01-01T00:00:00Z", end: "2024-01-01T00:00:00Z" }]
* @example splitIntervalByUnitUtc("2024-01-01T00:00:00Z", "2024-01-02T00:00:00Z", "hour", 0) // []
* @example splitIntervalByUnitUtc("invalid", "2024-01-02T00:00:00Z", "hour", 1) // []
* @example splitIntervalByUnitUtc("2024-01-01T00:00:00Z", "2024-01-01T01:30:00Z", "hour", 1, { maxPieces: 1 }) // [] (2 slices exceed the limit)
* @example splitIntervalByUnitUtc("2024-01-01T00:00:00Z", "2024-01-01T01:30:00Z", "hour", 1, { maxPieces: 2 }) // [{ start: "2024-01-01T00:00:00Z", end: "2024-01-01T01:00:00Z" }, { start: "2024-01-01T01:00:00Z", end: "2024-01-01T01:30:00Z" }]
* @example splitIntervalByUnitUtc("+275760-09-12T23:00:00Z", "+275760-09-13T00:00:00Z", "hour", 2) // [{ start: "+275760-09-12T23:00:00Z", end: "+275760-09-13T00:00:00Z" }] (the step past the last instant is trimmed to `end`)
*/
function splitIntervalByUnitUtc(start, end, unit, amount, options) {
try {
if (typeof start !== "string" || typeof end !== "string") return [];
if (!isValidUtc(start) || !isValidUtc(end)) return [];
if (typeof unit !== "string") return [];
const resolvedUnit = resolveDurationUnit(unit);
if (!isValidDateTimeDurationUnit(resolvedUnit)) return [];
if (!Number.isFinite(amount) || amount <= 0) return [];
const maxPieces = resolveMaxPieces(options);
if (maxPieces === null) return [];
try {
const startInstant = Temporal.Instant.from(start);
const endInstant = Temporal.Instant.from(end);
if (Temporal.Instant.compare(startInstant, endInstant) > 0) return [];
if (Temporal.Instant.compare(startInstant, endInstant) === 0) return [{
start: startInstant.toString(),
end: endInstant.toString()
}];
const spanNs = Number(endInstant.epochNanoseconds - startInstant.epochNanoseconds);
if (exceedsPieceLimit(minSlicesForSpan(spanNs, resolvedUnit, amount, false), maxPieces)) return [];
return (tileByUnit(startInstant.toZonedDateTimeISO("UTC"), endInstant.toZonedDateTimeISO("UTC"), Temporal.ZonedDateTime.compare, resolvedUnit, amount, maxPieces) ?? []).map(([sliceStart, sliceEnd]) => ({
start: sliceStart.toInstant().toString(),
end: sliceEnd.toInstant().toString()
}));
} catch {
return [];
}
} catch {
return [];
}
}
export { splitIntervalByUnitUtc };