@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.
61 lines (59 loc) • 3.93 kB
JavaScript
import { calendarDateOf } from "../../internal/calendarDateString.js";
import { parseCalendarDatePairForArithmetic } from "../../internal/calendarDatePairPolicy.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { durationUntilString } from "../../internal/durationUntilString.js";
import { isDateDurationUnitName } from "../../internal/unitNames.js";
import { resolveDurationUnit } from "../../internal/resolveDurationUnit.js";
// src/plain/calculate/diffDateAsDuration.ts
/**
* Return the difference between two PlainDate values as an ISO 8601 duration string,
* bridging to the `duration` namespace (see `parseDuration`, `normalizeDuration`).
*
* - Returns `""` for invalid inputs (negative diffs are valid and render with a leading `-`).
* - Uses Temporal's `until` semantics with `largestUnit` set to `unit` (TC39 CalendarDateUntil; for
* non-ISO calendars the Intl era/monthCode proposal's NonISODateUntil), then `.toString()`.
* - Accepts RFC 9557 calendar-annotated PlainDate strings. Same calendar rule as `diffDate` (see
* its JSDoc): measured in the shared calendar when `date1` and `date2` name the same calendar,
* `""` when they name different calendars (TC39 `CalendarEquals`; before 1.16.0 measured in ISO).
* - Unlike `diffDate`, `unit` is a single unit (not an array) — an ISO duration string already
* expresses a full multi-unit breakdown via `largestUnit` alone, so there's no array-of-units
* overload here.
*
* `smallestUnit`, `roundingIncrement`, and `roundingMode` control optional rounding of the
* underlying difference before it's rendered, per Temporal's DifferenceOptions — same as `diffDate`.
* `toStringSmallestUnit`, `fractionalSecondDigits`, and `toStringRoundingMode` control the
* precision of the rendered string itself, per Temporal's ToStringPrecisionOptions (mirroring
* `parseDuration`'s options) — kept separate from the `.until()` rounding options above because
* both option sets have colliding `smallestUnit`/`roundingMode` keys with different Temporal types.
*
* - Compatibility: since 1.16.0 calendar strings are RFC 9557 (ISO digits, `[u-ca=<id>]`, canonical
* calendar ids); see `isValidCalendarDate`.
*
* @param date1 ISO PlainDate string for the start, optionally calendar-annotated
* @param date2 ISO PlainDate string for the end, optionally calendar-annotated
* @param unit date unit, singular or plural (`"day"` or `"days"`), to use as the duration's largestUnit
* @param options optional: how the difference is rounded and how the string is rendered; a non-object value (such as `null`) is invalid
* @returns ISO 8601 duration string, or "" on invalid input
*
* @example diffDateAsDuration("2024-03-10", "2024-04-05", "days") // "P26D"
* @example diffDateAsDuration("2024-01-01", "2023-01-01", "days") // "-P365D"
* @example diffDateAsDuration("2024-01-01", "2024-01-01", "days") // "PT0S"
* @example diffDateAsDuration("invalid", "2024-03-15", "days") // ""
* @example diffDateAsDuration("2024-01-01", "2024-01-16", "weeks", { smallestUnit: "weeks", roundingMode: "halfExpand" }) // "P2W"
* @example diffDateAsDuration("2024-02-24[u-ca=hebrew]", "2024-03-25[u-ca=hebrew]", "months") // "P1M" (measured in Hebrew, 15 Adar I -> 15 Adar II)
* @example diffDateAsDuration("2023-08-31[u-ca=buddhist]", "2023-09-30[u-ca=buddhist]", "months") // "P30D" (Aug 31 + 1 month would pass Sep 30)
*/
function diffDateAsDuration(date1, date2, unit, options) {
if (!isOptionsArgument(options)) return "";
const validDates = calendarDateOf(date1) !== null && calendarDateOf(date2) !== null;
const resolvedUnit = resolveDurationUnit(unit);
const validUnit = isDateDurationUnitName(resolvedUnit);
if (!validDates || !validUnit) return "";
try {
const { a: d1, b: d2 } = parseCalendarDatePairForArithmetic(date1, date2);
return durationUntilString(d1, d2, resolvedUnit, options);
} catch {
return "";
}
}
export { diffDateAsDuration };