@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.
66 lines (64 loc) • 4.35 kB
JavaScript
import { roundingOptionsOf, stringOptionOrDefault } from "../../internal/typedOptions.js";
import { durationFrom } from "../../internal/durationFrom.js";
import { isOptionsArgument } from "../../internal/isObject.js";
import { durationRound } from "../../internal/zonedWallClockDifference.js";
import { resolveDurationRelativeTo } from "../../internal/resolveDurationRelativeTo.js";
// src/duration/normalize/normalizeDuration.ts
/**
* Roll an ISO 8601 duration string's small units into larger ones.
*
* - Uses Temporal.Duration.from and .round to rebalance, then .toString() to re-emit.
* - With no options, units are promoted only up to a unit already present: "PT90M" stays
* "PT90M", but "P1DT25H" becomes "P2DT1H" and "PT1H90M" becomes "PT2H30M". Pass an explicit
* largestUnit to promote further.
* - 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 "". Pass an
* explicit offset to pick the other reading of an ambiguous wall time.
* - A `relativeTo` string is ISO 8601 extended format over the whole string, and nothing else:
* a date, a date-time, or a zoned date-time (`<date>T<time>`, then nothing, `Z` or `±HH:MM`,
* then one time zone annotation). 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, and so is a time zone annotation on a plain value.
* - A `relativeTo` string takes a calendar in RFC 9557 form, as `convertDateToCalendar` and
* `convertZonedToCalendar` write it: ISO digits and one `[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 critical flag and the id's letter case are accepted. Any other annotation (an elective
* `[foo=bar]`, a second `u-ca`, an unknown critical one), a calendar before the zone, an
* unsupported calendar and a leap second in any spelling return `""`.
* Compatibility: before 1.16.0 the digits were read as the calendar's own year, month and day.
* - Returns "" for invalid input: non-string value, invalid duration string, or
* invalid relativeTo.
*
* @param value ISO 8601 duration string
* @param options The units to balance between, the rounding, and the anchor for calendar units, as Temporal's Duration.round takes them
* @returns rebalanced ISO 8601 duration string, or "" on invalid input
*
* @example normalizeDuration("PT90M", { largestUnit: "hour" }) // "PT1H30M"
* @example normalizeDuration("PT90M30S", { smallestUnit: "minute" }) // "PT91M"
* @example normalizeDuration("P45D", { largestUnit: "month" }) // "" (relativeTo required)
* @example normalizeDuration("P45D", { largestUnit: "month", relativeTo: "2024-01-01" }) // "P1M14D"
* @example normalizeDuration("P45D", { largestUnit: "month", relativeTo: "20240101" }) // "" (basic format)
* @example normalizeDuration("P1DT25H") // "P2DT1H"
* @example normalizeDuration("PT2H", { smallestUnit: "minute", roundingIncrement: 60 }) // ""
* @example normalizeDuration("PT25H", { largestUnit: "day", relativeTo: "2024-04-07T02:30[Australia/Sydney]" }) // "P1D"
* @example normalizeDuration("invalid") // ""
* @example normalizeDuration("P400D", { largestUnit: "year", relativeTo: "2024-02-24[u-ca=hebrew]" }) // "P1Y15D" (Hebrew leap year 5784)
* @example normalizeDuration("P45D", { largestUnit: "month", relativeTo: "2024-02-10[!u-ca=hebrew]" }) // "P1M15D" (1 Adar I 5784; the critical flag is accepted)
* @example normalizeDuration("P45D", { largestUnit: "month", relativeTo: "2024-02-10T00:00:00[u-ca=hebrew]" }) // "P1M15D" (a date-time without a zone reads as its date, as Temporal reads relativeTo)
*/
function normalizeDuration(value, options) {
if (!isOptionsArgument(options)) return "";
if (typeof value !== "string") return "";
try {
const duration = durationFrom(value);
return durationRound(duration, {
largestUnit: stringOptionOrDefault(options?.largestUnit, "auto"),
...roundingOptionsOf(options),
relativeTo: resolveDurationRelativeTo(options?.relativeTo)
}).toString();
} catch {
return "";
}
}
export { normalizeDuration };