@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.
48 lines • 3.56 kB
TypeScript
/**
* Return true if `value` is a valid GMT ZonedDateTime string: a bare ISO zoned datetime
* ("2024-10-03T14:30:45+02:00[Africa/Johannesburg]") or an RFC 9557 calendar-annotated one
* ("2024-10-03T14:30:45+02:00[Africa/Johannesburg][u-ca=hebrew]"), as
* `Temporal.ZonedDateTime#toString()` and `convertZonedToCalendar` write it.
*
* - Parallel to `isValidZonedDateTime`, which accepts no calendar annotation. Only
* the calendar-aware `zoned/` functions gate on this validator, so a string is never certified
* by a validator whose own namespace refuses it.
* - The calendar annotation follows the time zone annotation (RFC 9557 §4.1); a calendar before
* the zone, which Temporal also rejects, returns false.
* - A `[u-ca=<id>]` annotation, optionally critical. Temporal's `CanonicalizeCalendar` validates
* the id (folding case and aliases), and the calendar must be one GMT supports
* (`CalendarSystem`).
* - At most one annotation beyond the zone, the `u-ca` one, after the zone. An elective or
* unknown critical annotation (`[foo=bar]`, `[!foo=bar]`) and a second `u-ca` annotation
* return false.
* - The part before the zone must be ISO 8601 extended format, as
* `isValidZonedDateTime` requires: basic format, a space or lower-case `t` separator, a
* lower-case `z`, an hour-only time or offset and a date without a time return false.
* - Rejects leap seconds, which `Temporal.ZonedDateTime.from` otherwise clamps to `:59`.
* - Delegates the date, time, offset, zone and annotation checks to `Temporal.ZonedDateTime.from`
* (via `parseCalendarZonedValue`).
* - Returns false for an empty string.
* - A value that is not a string, a `String` object included, returns `false`. A `true` result
* narrows an `unknown` value to `string`.
*
* Compatibility: since 1.16.0 the string is RFC 9557. Earlier releases accepted calendar-native
* digits, `;era=`, the calendar annotation before the zone, and the ids `gregorian`, `taiwan`,
* `islamic-tabular` and `ethiopic-amete-alem`.
*
* @param value any value; a string is checked as a zoned datetime, optionally calendar-annotated
* @returns boolean indicating validity
*
* @example isValidCalendarZonedDateTime("2024-10-03T14:30:45+02:00[Africa/Johannesburg]") // true
* @example isValidCalendarZonedDateTime("2024-02-24T14:30:00+02:00[Africa/Johannesburg][u-ca=hebrew]") // true
* @example isValidCalendarZonedDateTime("2024-02-24T14:30:00+02:00[Africa/Johannesburg][foo=bar][u-ca=hebrew]") // false (elective annotation)
* @example isValidCalendarZonedDateTime("2024-02-24T14:30:00+02:00[u-ca=hebrew][Africa/Johannesburg]") // false (calendar before zone)
* @example isValidCalendarZonedDateTime("2019-04-30T12:00:00+09:00[Asia/Tokyo][u-ca=japanese;era=heisei]") // false (not RFC 9557)
* @example isValidCalendarZonedDateTime("2024-02-24T14:30:00+03:00[Africa/Johannesburg][u-ca=hebrew]") // false (offset wrong for the zone)
* @example isValidCalendarZonedDateTime("2024-06-30T23:59:60+00:00[UTC]") // false (leap second)
* @example isValidCalendarZonedDateTime("2024-02-24 14:30:00+02:00[Africa/Johannesburg][u-ca=hebrew]") // false (space separator)
* @example isValidCalendarZonedDateTime("invalid") // false
* @example isValidCalendarZonedDateTime(null) // false
*/
export declare function isValidCalendarZonedDateTime(value: string): boolean;
export declare function isValidCalendarZonedDateTime(value: {} | null | undefined | void): boolean;
export declare function isValidCalendarZonedDateTime(value: unknown): value is string;