@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.
83 lines (81 loc) • 5.93 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { durationTotal } from "../../internal/zonedWallClockDifference.js";
import { resolveDurationRelativeTo } from "../../internal/resolveDurationRelativeTo.js";
import { isValidDateTimeDurationUnit } from "../../plain/validate/isValidDateTimeDurationUnit.js";
import { resolveDurationUnit } from "../../internal/resolveDurationUnit.js";
import { isValidDuration } from "../validate/isValidDuration.js";
// src/duration/calculate/totalDuration.ts
/**
* Express an ISO 8601 duration as a single fractional total in one unit.
*
* - Uses Temporal.Duration.from and .total — the whole duration is converted, so
* totalDuration("P1DT2H30M", "hours") is 26.5, not the 2 that `getDurationUnit` reads.
* - Which function to use: `totalDuration` takes an ISO 8601 duration string. `totalUnits` takes
* a unit object of exact units, such as `{ minutes: 15 }`. `durationFromUnits` turns a unit
* object into the duration string this function takes. To total a calendar amount written as a
* unit object, such as `{ months: 1 }`, pass the result of `durationFromUnits` here with
* `relativeTo`.
* - For a length counted from now, use `totalUnitsFromNow`. The other forms are `getUnixIn` (an
* epoch), `getUtcIn` (UTC text), `formatHttpDateFromNow` (an HTTP date) and `ttlFromNow` (all of
* them from one clock read).
* - The result is fractional, not rounded: totalDuration("P1DT2H30M", "days") is
* 1.1041666666666667. Round it yourself, or reach for `normalizeDuration` instead. It is a
* JavaScript number, so a total past 2^53 is the nearest double.
* - The calendar-unit rule of `relativeTo` is the same one `normalizeDuration` (A3) carries;
* `addDuration`/`subtractDuration` (A2) have it worse still, since Temporal gives them no
* `relativeTo` option at all.
* - The requested-unit half of that rule bites even on day/time-only input: "P1DT2H30M" has
* no calendar component, yet totalDuration("P1DT2H30M", "weeks") is still null — a week is a
* calendar quantity to Temporal regardless of what it is being measured from.
* - A zoned `relativeTo` string resolves its wall time with disambiguation "compatible" and
* offset "reject", as Temporal does: an ambiguous wall time takes the earlier instant, a
* nonexistent one the later instant, and an offset that does not match the zone returns null.
* Pass an explicit offset to pick the other reading of an ambiguous wall time.
* - A `relativeTo` string is ISO 8601 extended format before its first `[`:
* a date, a date-time, or with a time zone annotation a zoned date-time (`<date>T<time>`, then
* nothing, `Z` or `±HH:MM`). Basic format (`"20240101"`), a space or lower-case `t` separator, a
* lower-case `z`, a UTC offset without a zone and a zoned date without a time are invalid.
* - A `relativeTo` string takes a calendar in RFC 9557 form, as `convertDateToCalendar` and
* `convertZonedToCalendar` write it: ISO digits and a `[u-ca=<id>]` annotation, after the zone
* for a zoned string (`"2024-02-24[u-ca=hebrew]"`, `"2024-02-24T00:00:00+02:00[Asia/Jerusalem][u-ca=hebrew]"`).
* The annotations are read as Temporal reads them: the critical flag and the id's letter case
* are accepted, elective unknown annotations are ignored, the first `u-ca` names the calendar,
* and a string with a time zone annotation is zoned. An unknown critical annotation, a critical
* duplicate calendar, a calendar before the zone, an unsupported calendar and a leap second in
* any spelling return null.
* Compatibility: before 1.16.0 the digits were read as the calendar's own year, month and day.
* - `unit` accepts the plural or singular name (`"hours"` or `"hour"`), as Temporal does.
* - Returns null on a non-duration `value`, an invalid `unit`, or an invalid `relativeTo`.
*
* @param value ISO 8601 duration string
* @param unit DateTimeDurationUnit to total into ("years" | "months" | "weeks" | "days" | "hours" | "minutes" | "seconds" | "milliseconds" | "microseconds" | "nanoseconds", or the singular name)
* @param options The anchor calendar units are measured from
* @returns the fractional total in `unit`, or null on invalid input
*
* @example totalDuration("P1DT2H30M", "hours") // 26.5
* @example totalDuration("P1DT2H30M", "minutes") // 1590
* @example totalDuration("PT36H", "days") // 1.5
* @example totalDuration("-PT90M", "hours") // -1.5
* @example totalDuration("P1M", "days") // null (calendar unit needs relativeTo)
* @example totalDuration("P1M", "days", { relativeTo: "2024-02-01" }) // 29
* @example totalDuration("P1M", "days", { relativeTo: "20240201" }) // null (basic format)
* @example totalDuration("P1D", "hours", { relativeTo: "2024-03-31T00:00:00+00:00[Europe/London]" }) // 23 (spring-forward)
* @example totalDuration("P1D", "hours", { relativeTo: "2024-04-07T02:30[Australia/Sydney]" }) // 25
* @example totalDuration("P1DT2H30M", "minute") // 1590 (singular unit name)
* @example totalDuration("not a duration", "hours") // null
* @example totalDuration("P1Y", "days", { relativeTo: "2024-02-24[u-ca=hebrew]" }) // 385 (Hebrew leap year 5784)
* @example totalDuration("P1M", "days", { relativeTo: "2024-02-10[!u-ca=hebrew]" }) // 30 (1 Adar I 5784; the critical flag is accepted)
* @example totalDuration("P1M", "days", { relativeTo: "2024-02-10T00:00:00[u-ca=hebrew]" }) // 30 (a date-time without a zone reads as its date, as Temporal reads relativeTo)
*/
function totalDuration(value, unit, options) {
if (!isOptionsArgument(options)) return null;
const resolvedUnit = resolveDurationUnit(unit);
if (!isValidDuration(value) || !isValidDateTimeDurationUnit(resolvedUnit)) return null;
try {
return durationTotal(Temporal.Duration.from(value), resolvedUnit, resolveDurationRelativeTo(options?.relativeTo));
} catch {
return null;
}
}
export { totalDuration };