@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.
40 lines • 3.04 kB
TypeScript
import { DurationRelativeTo } from "../types/duration-relative-to.js";
import "../types/index.js";
/**
* Resolve a `DurationRelativeTo` value for `Temporal.Duration`'s `relativeTo` option.
*
* - Every string is ISO 8601 extended format before its first `[`: with a
* time zone annotation, the strict zoned shape (`zonedDateTimeBody`: `<date>T<time>`, then
* nothing, `Z` or `±HH:MM[:SS[.fraction]]`); otherwise GMT's strict date (`plainDate`) or
* date-time (`plainDateTime`). Basic format (`"20241003"`), a space or lower-case `t` separator,
* a lower-case `z`, an hour-only time or offset, a UTC offset without a time zone annotation
* and a zoned date without a time throw, although Temporal's `ParseTemporalRelativeToString`
* reads them.
* - A string with a `u-ca` annotation is read as Temporal's `ParseTemporalRelativeToString` reads
* it: with a time zone annotation it is a ZonedDateTime (`"2024-02-24T00:00:00+02:00[Asia/Jerusalem][u-ca=hebrew]"`,
* `parseCalendarZonedValue`), otherwise a PlainDate (`"2024-02-24[u-ca=hebrew]"`, or a date-time
* whose time Temporal drops, `parseCalendarDateValue`). Annotations follow Temporal's
* grammar: elective unknown ones are ignored, the first `u-ca` wins, and an unknown critical
* annotation, a critical duplicate calendar, a calendar before the zone and `;era=` throw. The
* calendar must be a `CalendarSystem`, and `"ethiopic"`/`"coptic"` compute in `"ethioaa"`,
* which polyfill 0.5.1 can read. The digits are ISO, so this is the date Temporal itself would
* read.
* - A zoned string without a calendar annotation is read with `zonedDateTimeFrom`, so its offset
* is matched against the zone as TC39 `ToRelativeTemporalObject` matches it: an offset written
* to the minute may be the zone's sub-minute offset rounded, and one written with seconds must
* be the zone's offset exactly (`"1970-01-01T00:00:00-00:45:00[Africa/Monrovia]"` throws).
* - A leap second (second `60`) throws, in every spelling Temporal's grammar accepts — `T`, `t`
* or space separator, extended or basic digits, with or without a designator. Temporal's
* ParseISODateTime would otherwise clamp it to `:59`.
* - Every other `relativeTo` (a strict extended string without a calendar annotation, or a `PlainDate`/`PlainDateTime`/
* `ZonedDateTime` object or `-Like`) passes through unchanged.
*
* A PlainDate string resolves to a `Temporal.PlainDateTime` (midnight, same calendar) only because
* the polyfill's TypeScript declaration and GMT's `DurationRelativeTo` type list `PlainDateTime`,
* not `PlainDate`. Temporal's `GetTemporalRelativeToOption` converts a `PlainDateTime` with
* `CreateTemporalDate` and drops the time, so the result is identical.
*
* Throws on invalid input. Callers already wrap the Temporal call consuming this in
* `try { ... } catch { return sentinel }` per GMT's contract.
*/
export declare function resolveDurationRelativeTo(relativeTo: DurationRelativeTo | undefined): DurationRelativeTo | undefined;