@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.
63 lines (61 loc) • 3.87 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { timeZoneEquals } from "../../internal/temporalCompat/timeZoneEquals.js";
import { zonedDateTimeOf } from "../../internal/zonedWallClock.js";
import { exceedsPieceLimit, resolveMaxPieces } from "../../internal/maxPieces.js";
// src/zoned/map/mapZonedDatesInRange.ts
/**
* Return an array of plain ISO date strings covering the inclusive date range between two zoned datetimes.
*
* - Both datetimes must have the same time zone: identifiers of one zone count as the same
* (`UTC` and `Etc/UTC` on every host; an IANA link such as `Europe/Kiev` and `Europe/Kyiv` where
* the host resolves it, which Safari 27 without `Temporal` does not).
* - Returns [] for invalid inputs, mismatched timezones, start > end, or invalid step.
* - A range holding more dates than `options.maxPieces` returns `[]` without generating any.
* - An explicit `undefined` `stepDays` is the default step, so `(start, end, undefined, options)`
* reaches `options`. **Compatibility:** before 1.16.0 it returned `[]`.
* - **End-inclusive, where the `interval*` functions are half-open.** This enumerates the dates a
* range covers, so the end date is one of them; an interval bounds a span as `[start, end)`, so
* its `end` is not. The same pair therefore yields one more date here than `intervalCountZoned`
* counts.
*
* @param startZonedDateTime start zoned ISO 8601 datetime string
* @param endZonedDateTime end zoned ISO 8601 datetime string
* @param stepDays optional positive integer step in days
* @param options optional limit on the size of the output
* @returns array of ISO date strings or [] when invalid
*
* @example mapZonedDatesInRange("+275760-09-12T00:00:00+00:00[UTC]", "+275760-09-13T00:00:00+00:00[UTC]") // ["+275760-09-12", "+275760-09-13"] (a range ending on the maximum instant)
* @example mapZonedDatesInRange("2024-02-28T12:00:00+00:00[UTC]", "2024-03-02T12:00:00+00:00[UTC]") // ["2024-02-28", "2024-02-29", "2024-03-01", "2024-03-02"]
* @example mapZonedDatesInRange("2024-02-28T12:00:00+00:00[UTC]", "2024-03-02T12:00:00+00:00[UTC]", 2) // ["2024-02-28", "2024-03-01"]
* @example mapZonedDatesInRange("2024-02-28T12:00:00+00:00[UTC]", "2024-03-01T12:00:00+00:00[UTC]", undefined) // ["2024-02-28", "2024-02-29", "2024-03-01"] (undefined is the default step)
* @example mapZonedDatesInRange("invalid", "2024-03-02T12:00:00+00:00[UTC]") // []
* @example mapZonedDatesInRange("2024-02-28T12:00:00+00:00[UTC]", "2024-03-02T12:00:00+00:00[UTC]", 1, { maxPieces: 3 }) // [] (more dates than the limit)
* @example mapZonedDatesInRange("2024-02-28T12:00:00+00:00[UTC]", "2024-03-02T12:00:00+00:00[UTC]", 1, { maxPieces: 10 }) // ["2024-02-28", "2024-02-29", "2024-03-01", "2024-03-02"]
*/
function mapZonedDatesInRange(startZonedDateTime, endZonedDateTime, stepDays, options) {
try {
const resolvedStepDays = stepDays === void 0 ? 1 : stepDays;
if (typeof resolvedStepDays !== "number" || !Number.isInteger(resolvedStepDays) || resolvedStepDays <= 0) return [];
const maxPieces = resolveMaxPieces(options);
if (maxPieces === null) return [];
const start = zonedDateTimeOf(startZonedDateTime);
const end = zonedDateTimeOf(endZonedDateTime);
if (start === null || end === null) return [];
try {
if (!timeZoneEquals(start, end)) return [];
const startDate = start.toPlainDate();
const endDate = end.toPlainDate();
if (Temporal.PlainDate.compare(startDate, endDate) === 1) return [];
const count = Math.floor(startDate.until(endDate).days / resolvedStepDays) + 1;
if (exceedsPieceLimit(count, maxPieces)) return [];
const result = [];
for (let index = 0; index < count; index++) result.push(startDate.add({ days: index * resolvedStepDays }).toString());
return result;
} catch {
return [];
}
} catch {
return [];
}
}
export { mapZonedDatesInRange };