@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.
88 lines (86 loc) • 6.31 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { stringOptionOrDefault } from "../../internal/typedOptions.js";
import { durationOf } from "../../internal/inputChecks.js";
import { calendarSystemOfZonedValue } from "../../internal/calendarValueOfZoned.js";
import { calendarZonedOf, formatZonedInCalendar } from "../../internal/calendarZonedString.js";
import { addToZonedDisambiguated } from "../../internal/zonedWallClockOperations.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { resolveOverflow } from "../../internal/resolveOverflow.js";
// src/zoned/interval/intervalFromDurationZoned.ts
/**
* Construct a zoned interval from a single point plus an ISO 8601 duration, anchored at either end.
*
* - `anchor: "start"` treats `value` as the interval start and adds `duration` to get the end.
* - `anchor: "end"` treats `value` as the interval end and subtracts `duration` to get the start.
* - Uses `Temporal.ZonedDateTime.prototype.add`/`.subtract`, so calendar units (years/months/weeks)
* resolve against `value` itself — no separate `relativeTo` is needed.
* - Follows Temporal's AddZonedDateTime: the date portion of the duration (years, months, weeks,
* days) moves the wall-clock date, then the time portion (hours and smaller) is added (subtracted
* for `anchor: "end"`) in exact time. `disambiguation` applies ONLY to the intermediate
* wall-clock date-time after the date portion, exactly as Temporal's GetEpochNanosecondsFor
* resolves it. It never re-resolves the exact-time result.
* - Compatibility: before 1.16.0 a date portion landing in a spring-forward gap was always moved
* forward, whatever `disambiguation` said. Pass "compatible" (or omit it) to keep that result.
* - Compatibility: before 1.16.0 a non-"compatible" `disambiguation` re-resolved the final wall
* clock (so `+ { minutes: 10 }` from `01:30-05:00` with "earlier" returned `01:40-04:00`, 50
* minutes earlier in exact time). To get that value, re-resolve the result's wall clock:
* `setZoned(result, { hour, minute, second }, { disambiguation, offset: "ignore" })`.
* - There is no `offset` option (removed in 1.16.0): the computed endpoint's wall clock is resolved
* from a plain date-time, which has no UTC offset for it to act on.
* - A negative `duration` (e.g. `"-P1D"`) can invert the computed span; returns null when that
* happens, mirroring `intervalIntersectionZoned`'s `start > end` rejection.
* - Accepts a RFC 9557 calendar-annotated zoned string (as produced by `convertZonedToCalendar`) as
* well as a bare ISO one — E7 (issue #152). Calendar units in `duration` resolve against that
* calendar, and both returned endpoints are re-derived in it via `formatZonedInCalendar`. There
* is only ONE calendar-tagged input here, so there is no second calendar to mismatch.
* - Returns null on invalid input (unparseable `value`, invalid `duration`, or an `anchor` other
* than `"start"`/`"end"`).
*
* @param value ISO 8601 zoned datetime string, optionally calendar-annotated
* @param duration ISO 8601 duration string
* @param anchor "start" | "end" — which endpoint `value` represents
* @param options optional settings for resolving a DST gap or overlap and an out-of-range date
* @returns `{ start, end }` with the constructed span, or null on invalid input
*
* @example intervalFromDurationZoned("2024-01-01T00:00:00+00:00[UTC]", "P1D", "start") // { start: "2024-01-01T00:00:00+00:00[UTC]", end: "2024-01-02T00:00:00+00:00[UTC]" }
* @example intervalFromDurationZoned("2024-01-02T00:00:00+00:00[UTC]", "P1D", "end") // { start: "2024-01-01T00:00:00+00:00[UTC]", end: "2024-01-02T00:00:00+00:00[UTC]" }
* @example intervalFromDurationZoned("2024-10-26T01:30:00+01:00[Europe/London]", "P1D", "start", { disambiguation: "later" }) // { start: "2024-10-26T01:30:00+01:00[Europe/London]", end: "2024-10-27T01:30:00+00:00[Europe/London]" } (fall-back overlap resolved; default "compatible" would return the +01:00 instant instead)
* @example intervalFromDurationZoned("2024-10-05T02:30:00+10:00[Australia/Sydney]", "P1D", "start", { disambiguation: "earlier" }) // { start: "2024-10-05T02:30:00+10:00[Australia/Sydney]", end: "2024-10-06T01:30:00+10:00[Australia/Sydney]" } (spring-forward gap; "earlier" moves the end back an hour)
* @example intervalFromDurationZoned("2024-10-05T02:30:00+10:00[Australia/Sydney]", "P1D", "start", { disambiguation: "reject" }) // null (spring-forward gap rejected)
* @example intervalFromDurationZoned("2024-10-27T00:30:00+01:00[Europe/London]", "PT1H", "start", { disambiguation: "later" }) // { start: "2024-10-27T00:30:00+01:00[Europe/London]", end: "2024-10-27T01:30:00+01:00[Europe/London]" } (exact time — disambiguation never re-resolves it)
* @example setZoned(intervalFromDurationZoned("2024-10-27T00:30:00+01:00[Europe/London]", "PT1H", "start")?.end ?? "", { hour: 1, minute: 30, second: 0 }, { disambiguation: "later", offset: "ignore" }) // "2024-10-27T01:30:00+00:00[Europe/London]" (pre-1.16.0 end)
* @example intervalFromDurationZoned("2024-01-31T12:00:00+05:30[Asia/Kolkata]", "P1M", "start", { overflow: "reject" }) // null
* @example intervalFromDurationZoned("invalid", "P1D", "start") // null
*/
function intervalFromDurationZoned(value, duration, anchor, options) {
try {
if (!isOptionsArgument(options)) return null;
const point = calendarZonedOf(value);
if (point === null) return null;
const dur = durationOf(duration);
if (dur === null) return null;
if (anchor !== "start" && anchor !== "end") return null;
const disambiguation = stringOptionOrDefault(options?.disambiguation, "compatible");
const overflow = resolveOverflow(options?.overflow);
try {
const calendar = calendarSystemOfZonedValue(value);
if (!calendar) return null;
const other = addToZonedDisambiguated(point, dur, anchor === "start" ? 1 : -1, {
overflow,
disambiguation
});
const start = anchor === "start" ? point : other;
const end = anchor === "start" ? other : point;
if (Temporal.ZonedDateTime.compare(start, end) > 0) return null;
return {
start: formatZonedInCalendar(start, calendar),
end: formatZonedInCalendar(end, calendar)
};
} catch {
return null;
}
} catch {
return null;
}
}
export { intervalFromDurationZoned };