date-vir
Version:
Easy and explicit dates and times.
35 lines (34 loc) • 1.18 kB
JavaScript
import { AssertionError } from '@augment-vir/assert';
import { Info } from 'luxon';
/**
* Asserts that the given input is a valid timezone name.
*
* Note that this may actually allow names beyond what is currently in the exact {@link Timezone}
* type, because this function checks to see if the given timezone can be used to construct a date,
* regardless of whether it's in our typed list or not.
*
* @category Assertion
*/
export function assertValidTimezone(potentialTimezone, userMessage) {
if (!Info.isValidIANAZone(potentialTimezone)) {
throw new AssertionError(`'${potentialTimezone}' is not a valid time zone`, userMessage);
}
}
/**
* Checks that the given timezone is valid.
*
* Note that this may actually allow names beyond what is currently in the exact {@link Timezone}
* type, because this function checks to see if the given timezone can be used to construct a date,
* regardless of whether it's in our typed list or not.
*
* @category Assertion
*/
export function isValidTimezone(potentialTimezone) {
try {
assertValidTimezone(potentialTimezone);
return true;
}
catch {
return false;
}
}