@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.
67 lines (65 loc) • 3.92 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { parseCalendarDateValue } from "../../internal/calendarDateString.js";
import { calendarSystemOfDateValue } from "../../internal/calendarValueOfDate.js";
import { formatDateInCalendar } from "../../internal/formatDateInCalendar.js";
import { plainDateAdd } from "../../internal/plainDateAdd.js";
import { resolveOverflow } from "../../internal/resolveOverflow.js";
import { isValidCalendarDate } from "../validate/isValidCalendarDate.js";
import { isValidDuration } from "../../duration/validate/isValidDuration.js";
// src/plain/interval/intervalFromDurationDate.ts
/**
* Construct a date 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.PlainDate.prototype.add`/`.subtract` semantics, so calendar units
* (years/months/weeks) resolve against `value` itself — no separate `relativeTo` is needed,
* unlike `addDuration`. Time units count as whole 24-hour days, as Temporal's do.
* - A negative `duration` (e.g. `"-P1D"`) can invert the computed span; returns null when that
* happens, mirroring `intervalIntersectionDate`'s `start > end` rejection.
* - Accepts a RFC 9557 calendar-annotated PlainDate string — E5 (issue #78). The computed endpoint
* is resolved in `value`'s own calendar (no `relativeTo`/pair-matching question, since there
* is only one calendar-tagged input) and both endpoints are re-formatted in that calendar,
* re-derived from the actual result rather than copied from `value`'s tag (a leap-month or
* era boundary can fall between them).
* - Returns null on invalid input (unparseable `value`, invalid `duration`, or an `anchor` other
* than `"start"`/`"end"`).
*
* @param value ISO PlainDate string, optionally calendar-annotated
* @param duration ISO 8601 duration string
* @param anchor "start" | "end" — which endpoint `value` represents
* @param options How an out-of-range endpoint is handled
* @returns `{ start, end }` with the constructed span, or null on invalid input
*
* @example intervalFromDurationDate("2024-01-01", "P1M", "start") // { start: "2024-01-01", end: "2024-02-01" }
* @example intervalFromDurationDate("2024-02-01", "P1M", "end") // { start: "2024-01-01", end: "2024-02-01" }
* @example intervalFromDurationDate("2024-01-31", "P1M", "start", { overflow: "reject" }) // null
* @example intervalFromDurationDate("2024-01-05", "-P10D", "start") // null (inverted span)
* @example intervalFromDurationDate("invalid", "P1M", "start") // null
* @example intervalFromDurationDate("2024-02-24[u-ca=hebrew]", "P1M", "start") // { start: "2024-02-24[u-ca=hebrew]", end: "2024-03-25[u-ca=hebrew]" } (Adar I -> Adar)
*/
function intervalFromDurationDate(value, duration, anchor, options) {
if (!isOptionsArgument(options)) return null;
if (typeof value !== "string" || !isValidCalendarDate(value)) return null;
if (!isValidDuration(duration)) return null;
if (anchor !== "start" && anchor !== "end") return null;
try {
const calendar = calendarSystemOfDateValue(value);
if (!calendar) return null;
const point = parseCalendarDateValue(value);
const dur = Temporal.Duration.from(duration);
const overflow = resolveOverflow(options?.overflow);
const other = plainDateAdd(point, anchor === "start" ? dur : dur.negated(), overflow);
const start = anchor === "start" ? point : other;
const end = anchor === "start" ? other : point;
if (Temporal.PlainDate.compare(start, end) > 0) return null;
return {
start: formatDateInCalendar(start, calendar),
end: formatDateInCalendar(end, calendar)
};
} catch {
return null;
}
}
export { intervalFromDurationDate };