@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.
37 lines • 2.4 kB
TypeScript
/**
* Validate whether a string is a valid time zone identifier: an IANA name or a UTC offset.
*
* - Uses Temporal.ZonedDateTime.from to test timezone validity.
* - Uses the `timeZoneLike` regex to check the identifier's shape: Temporal's
* `TimeZoneIdentifier` grammar (§13.31).
* - Accepts every IANA Zone and Link name the runtime accepts, including single-component names
* (`UTC`, `Japan`, `Zulu`, `EST5EDT`, `GMT+0`), in any letter case (ECMA-402 matches
* identifiers ASCII-case-insensitively, so `utc` is as valid as `america/new_york`).
* - Accepts an offset identifier (`UTCOffset[~SubMinutePrecision]`): `"+05:00"`, `"+0530"`,
* `"-08"`, hour `00`–`23`, no seconds. Temporal and `Intl.DateTimeFormat` accept the same ones.
* - Returns false for invalid timezone formats.
* - Compatibility: earlier releases required a `/` in every name except `UTC` and `GMT`. To keep
* that narrower rule, also require `timeZone.includes("/")` or `"UTC"`/`"GMT"`.
* - A stored offset with seconds (`-00:44:30`) is false here and true for `isValidUtcOffset`. To
* check what a function with a zoneless result accepts, use
* `isValidTimeZone(x) || isValidUtcOffset(x)`.
* - A value that is not a string, a `String` object included, returns `false`. A `true` result
* narrows an `unknown` value to `string`.
*
* @param timeZone any value; a string is checked as an IANA time zone identifier or a UTC offset
* @returns boolean indicating validity
*
* @example isValidTimeZone("Asia/Kolkata") // true
* @example isValidTimeZone("Europe/London") // true
* @example isValidTimeZone("Invalid/Timezone") // false
* @example isValidTimeZone("Japan") // true (IANA link to Asia/Tokyo)
* @example isValidTimeZone("utc") // true (case-insensitive)
* @example isValidTimeZone("+05:00") // true (an offset identifier)
* @example isValidTimeZone("+05:00:00") // false (no seconds in an offset identifier)
* @example isValidTimeZone("-00:44:30") // false (a stored offset with seconds; `isValidUtcOffset` accepts it)
* @example "Japan".includes("/") // false (the earlier slash-only rule)
* @example isValidTimeZone(null) // false
*/
export declare function isValidTimeZone(timeZone: string): boolean;
export declare function isValidTimeZone(timeZone: {} | null | undefined | void): boolean;
export declare function isValidTimeZone(timeZone: unknown): timeZone is string;