@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.
55 lines (53 loc) • 3.03 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { hasZonedDateTimeShape } from "../../internal/isoStringBody.js";
import { hasSecond60 } from "../../plain/validate/hasSecond60.js";
import { isObject, isOptionsArgument } from "../../internal/isObject.js";
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
// src/zoned/validate/isValidZonedRange.ts
/**
* 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`.
* - Annotations are read as `isValidZonedDateTime` reads them: elective ones are ignored,
* `[u-ca=iso8601]` is accepted, and a non-ISO calendar on either endpoint 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]" }) // true
* @example isValidZonedRange({ value1: "2024-01-01 10:00:00+00:00[UTC]", value2: "2024-12-31T23:59:59+00:00[UTC]" }) // false (space separator)
*/
function isValidZonedRange(props) {
try {
if (!isObject(props)) return false;
const { value1, value2, options } = props;
if (!isOptionsArgument(options)) return false;
if (typeof value1 !== "string" || typeof value2 !== "string") return false;
if (hasSecond60(value1) || hasSecond60(value2) || !hasZonedDateTimeShape(value1) || !hasZonedDateTimeShape(value2)) return false;
try {
const zdt1 = zonedDateTimeFrom(value1);
const zdt2 = zonedDateTimeFrom(value2);
if (zdt1.calendarId !== "iso8601" || zdt2.calendarId !== "iso8601") return false;
const instant1 = zdt1.toInstant();
const instant2 = zdt2.toInstant();
const cmp = Temporal.Instant.compare(instant1, instant2);
if (options?.allowEqual) return cmp <= 0;
return cmp < 0;
} catch {
return false;
}
} catch {
return false;
}
}
export { isValidZonedRange };