UNPKG

@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.

49 lines • 3.65 kB
import { Temporal } from "./temporal.js"; import { DurationRelativeTo } from "../types/duration-relative-to.js"; import "../types/index.js"; /** * What `resolveDurationRelativeTo` hands to Temporal: the string itself, or the object GMT read * from a calendar-annotated or zoned string. */ export type ResolvedRelativeTo = string | Temporal.PlainDate | Temporal.PlainDateTime | Temporal.ZonedDateTime; /** * Resolve a `DurationRelativeTo` value for `Temporal.Duration`'s `relativeTo` option. * * - Every string is ISO 8601 extended format and nothing else: with a time zone annotation, the * strict zoned shape (`<date>T<time>`, then nothing, `Z` or `±HH:MM[:SS[.fraction]]`, then the * zone); otherwise GMT's strict date (`plainDate`) or date-time (`plainDateTime`). Either may * carry one calendar annotation; any other annotation (an elective `[foo=bar]`, a second `u-ca`) * throws. 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 * rules: at most one `u-ca` after the body (and the zone, for the zoned form). An elective * unknown annotation, a second `u-ca`, an unknown critical annotation, a calendar before the * zone and `;era=` are rejected. 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 string (a strict extended string without a time zone or calendar annotation) passes * through unchanged. `undefined` is no anchor. Any other value (a `Temporal` object, a property * bag, a number, `null`) throws `TypeError`: `relativeTo` is a string, and Temporal would clamp an * invalid property bag (`{ year: 2024, month: 2, day: 30 }` is 29 February). * * A PlainDate string resolves to a `Temporal.PlainDateTime` (midnight, same calendar) only because * the polyfill's TypeScript declaration lists `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): ResolvedRelativeTo | undefined;