@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.
68 lines (66 loc) • 4.85 kB
JavaScript
import { calendarZonedOf } from "../../internal/calendarZonedString.js";
import { parseCalendarZonedPairForArithmetic } from "../../internal/calendarZonedPairPolicy.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { isCalendarDifferenceAcrossZones } from "../../internal/zonedWallClockDifference.js";
import { durationUntilString } from "../../internal/durationUntilString.js";
import { isDateTimeDurationUnitName } from "../../internal/unitNames.js";
import { resolveDurationUnit } from "../../internal/resolveDurationUnit.js";
// src/zoned/calculate/diffZonedAsDuration.ts
/**
* Return the difference between two zoned datetimes as an ISO 8601 duration string,
* bridging to the `duration` namespace (see `parseDuration`, `normalizeDuration`).
*
* - Uses Temporal.ZonedDateTime.until with `largestUnit` set to `unit`, then `.toString()`.
* - Measures like `diffZoned` (Temporal's DifferenceZonedDateTime): calendar units on the zone's
* own wall clock, time units in exact elapsed time. Values in different time zones with a
* calendar `unit` (days or larger) return "".
* - Compatibility: before 1.16.0 both values were converted to UTC first. To get that value:
* `diffUtcAsDuration(convertZonedToUtc(value1), convertZonedToUtc(value2), unit)`.
* - Accepts RFC 9557 calendar-annotated zoned strings, with the same calendar rule as `diffZoned`:
* measured in the shared calendar, `""` when the two name different calendars (TC39
* `CalendarEquals`; before 1.16.0 measured in ISO).
* - Unlike `diffZoned`, `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.
* - Returns `""` for invalid input (negative diffs are valid and render with a leading `-`).
*
* `smallestUnit`, `roundingIncrement`, and `roundingMode` control optional rounding of the
* underlying difference before it's rendered, per Temporal's DifferenceOptions — same as
* `diffZoned`. `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, the `[u-ca=<id>]`
* annotation after the zone, canonical calendar ids); see `isValidCalendarZonedDateTime`.
*
* @param value1 zoned ISO 8601 datetime string (start), optionally calendar-annotated
* @param value2 zoned ISO 8601 datetime string (end), optionally calendar-annotated
* @param unit date-time unit to use as the duration's largestUnit, singular or plural (`"day"` or `"days"`)
* @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 diffZonedAsDuration("2024-10-05T12:00:00+10:00[Australia/Sydney]", "2024-10-07T12:00:00+11:00[Australia/Sydney]", "days") // "P2D" (wall-clock days; 47 elapsed hours)
* @example diffZonedAsDuration("2028-01-01T00:00:00+00:00[UTC]", "2028-01-01T00:00:00+00:00[UTC]", "hours") // "PT0S"
* @example diffZonedAsDuration("2028-01-01T00:00:00+00:00[UTC]", "2028-01-02T13:00:00+13:00[Pacific/Apia]", "days") // "" (calendar unit across two time zones)
* @example diffUtcAsDuration(convertZonedToUtc("2024-10-05T12:00:00+10:00[Australia/Sydney]"), convertZonedToUtc("2024-10-07T12:00:00+11:00[Australia/Sydney]"), "days") // "P1DT23H" (pre-1.16.0 UTC-clock result)
* @example diffZonedAsDuration("invalid", "2028-01-01T00:00:00+00:00[UTC]", "days") // ""
* @example diffZonedAsDuration("2023-09-16T00:00:00+02:00[Africa/Johannesburg][u-ca=hebrew]", "2024-10-03T00:00:00+02:00[Africa/Johannesburg][u-ca=hebrew]", "months") // "P13M" (Hebrew leap year 5784)
* @example diffZonedAsDuration("2024-03-10T14:30:00+09:00[u-ca=hebrew][Asia/Tokyo]", "2024-03-11T14:30:00+09:00[Asia/Tokyo]", "days") // "" (calendar before zone is not RFC 9557)
*/
function diffZonedAsDuration(value1, value2, unit, options) {
if (!isOptionsArgument(options)) return "";
const validZonedDateTimes = calendarZonedOf(value1) !== null && calendarZonedOf(value2) !== null;
const resolvedUnit = resolveDurationUnit(unit);
const validUnit = isDateTimeDurationUnitName(resolvedUnit);
if (!validZonedDateTimes || !validUnit) return "";
try {
const { a, b } = parseCalendarZonedPairForArithmetic(value1, value2);
if (isCalendarDifferenceAcrossZones(a, b, resolvedUnit)) return "";
return durationUntilString(a, b, resolvedUnit, options);
} catch {
return "";
}
}
export { diffZonedAsDuration };