@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.
49 lines (47 loc) • 3.18 kB
JavaScript
import { parseInstantNanoseconds } from "../../internal/instantNanoseconds.js";
// src/span/validate/isValidSpan.ts
/**
* Validate whether two ISO 8601 instant strings form a measurable span.
*
* True exactly when `spanMs` and `spanNs` will return a value rather than `null` — the
* endpoints both parse, so there is an elapsed time between them.
*
* - Accepts what `toNanoseconds` does on both sides: an offset designator is required,
* optionally followed by a bracketed IANA zone; no leap seconds. Calendar and elective
* annotations are ignored, as `Temporal.Instant.from` ignores them. The two endpoints need not
* share a zone.
* - **The offset fixes the instant; a bracketed zone only resolves a rounded one.** An offset
* written to the minute that is the bracketed zone's sub-minute offset rounded
* (`-00:45[Africa/Monrovia]`, for −00:44:30) names the instant the zone gives, as
* `Temporal.ZonedDateTime.from` reads it, so a zoned string Temporal wrote is read back as
* the instant it was written for, except a wall time repeated inside a sub-minute offset
* change, which reads as its first pass (see `isValidInstant`). Otherwise the bracket is not
* checked: a zone that does not exist, or that disagrees with the offset, changes nothing. Validity never depends
* on the bracket.
* - Order does not matter. A span is signed, not invalid, when `start` is after `end`, so
* this is symmetric.
* - **`spanMs` can still return `null` on a pair this accepts** — a span wider than
* `Number.MAX_SAFE_INTEGER` milliseconds has no `number` to return. That is a range
* limit on the result, not a defect in the inputs, and `spanNs` returns the exact
* `bigint` for the same pair.
* - Does **not** validate `spanWallClock`, which needs a bracketed IANA zone on both
* endpoints rather than a bare offset — that is `isValidZonedDateTime`.
* - Returns `false` for non-strings and empty strings.
*
* @param start candidate ISO 8601 instant string the span would be measured from
* @param end candidate ISO 8601 instant string the span would be measured to
* @returns boolean indicating whether spanMs/spanNs will measure this pair
*
* @example isValidSpan("2024-03-10T12:00:00Z", "2024-03-10T12:00:01Z") // true
* @example isValidSpan("2024-03-10T12:00:00Z", "2024-03-10T12:00:00Z") // true — a zero span is a span
* @example isValidSpan("2024-03-10T12:00:01Z", "2024-03-10T12:00:00Z") // true — spans are signed
* @example isValidSpan("2024-03-10T07:00:00-05:00[America/Bogota]", "2024-03-10T12:00:00Z") // true
* @example isValidSpan("2024-03-10T12:00:00+05:00[Asia/Tokyo]", "2024-03-10T12:00:00Z[Not/AZone]") // true — zone annotations are not checked
* @example isValidSpan("-271821-04-20T00:00:00Z", "+275760-09-13T00:00:00Z") // true — but spanMs returns null, use spanNs
* @example isValidSpan("2024-03-10T12:00:00Z", "invalid") // false
* @example isValidSpan("2024-03-10T12:00:00[Europe/London]", "2024-03-11T12:00:00[Europe/London]") // false — no offset designator
*/
function isValidSpan(start, end) {
return parseInstantNanoseconds(start) !== null && parseInstantNanoseconds(end) !== null;
}
export { isValidSpan };