Skip to content

asTimeZone

function asTimeZone(value): TimeZone;

Defined in: timezone.ts:60

Brand a string as a TimeZone. The one tolerated cast: a brand constructor — the caller guarantees the string was validated first (every TimeZoneSchema runs isValidTimeZone before calling this).

It checks nothing at runtime, so validate first.

Parameter Type
value string

TimeZone

Validate, then brand

import { asTimeZone, isValidTimeZone, type TimeZone } from "@xtandard/lib/valibot";
function timeZoneFromHeader(value: string | null): TimeZone {
const candidate = value ?? "UTC";
return isValidTimeZone({ timeZone: candidate }).valid
? asTimeZone(candidate)
: asTimeZone("UTC");
}
timeZoneFromHeader("Asia/Tokyo"); // "Asia/Tokyo" (TimeZone)
timeZoneFromHeader("Mars/Olympus"); // "UTC"