@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.
44 lines • 2.46 kB
TypeScript
/**
* Return true if `value1` and `value2` form a valid zoned range — both parseable as
* ISO ZonedDateTime strings and the instant at `value1` is <= the instant at `value2`.
*
* - Both inputs must be valid ISO 8601 zoned datetime strings in extended format, as
* `isValidZonedDateTime` requires: basic format, a space or lower-case `t` separator, a
* lower-case `z` and a date without a time return `false`.
* - Comparison is done by instant, so intervals spanning DST transitions are compared
* by absolute time.
* - Invalid input, malformed strings, or leap-second strings return `false`.
* - Each endpoint is read as `isValidZonedDateTime` reads it: a calendar or elective annotation
* returns `false`.
*
* @param props the two endpoints of the range and its optional settings
* @returns boolean indicating whether the zoned range is valid
*
* @example isValidZonedRange({ value1: "2024-01-01T10:00:00+00:00[UTC]", value2: "2024-12-31T23:59:59+00:00[UTC]" }) // true
* @example isValidZonedRange({ value1: "2024-12-31T23:59:59+00:00[UTC]", value2: "2024-01-01T10:00:00+00:00[UTC]" }) // false
* @example isValidZonedRange({ value1: "2024-06-15T12:00:00+09:00[Asia/Tokyo]", value2: "2024-06-15T12:00:00+09:00[Asia/Tokyo]", options: { allowEqual: true } }) // true
* @example isValidZonedRange({ value1: "2024-01-01T00:00:00+00:00[UTC][u-ca=hebrew]", value2: "2024-02-01T00:00:00+00:00[UTC]" }) // false (non-ISO calendar)
* @example isValidZonedRange({ value1: "2024-01-01T00:00:00+00:00[UTC][u-ca=iso8601]", value2: "2024-02-01T00:00:00+00:00[UTC]" }) // false (a calendar annotation)
* @example isValidZonedRange({ value1: "2024-01-01 10:00:00+00:00[UTC]", value2: "2024-12-31T23:59:59+00:00[UTC]" }) // false (space separator)
*/
export declare function isValidZonedRange(props: {
/** The start of the range, as an ISO 8601 zoned datetime string. */
value1: string;
/** The end of the range, as an ISO 8601 zoned datetime string. */
value2: string;
/**
* The settings for the comparison. It must be an object or omitted; any other value, `null`
* included, returns `false`.
*
* @defaultValue None. Two values at the same instant are not a valid range.
*/
options?: {
/**
* Whether `value1` at the same instant as `value2` is a valid range. `false` requires `value1`
* to be before `value2`.
*
* @defaultValue `false`
*/
allowEqual?: boolean;
};
}): boolean;