@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.
47 lines (45 loc) • 2.88 kB
JavaScript
import { Temporal } from "../../internal/temporal.js";
import { instantFrom } from "../../internal/instantNanoseconds.js";
import { isValidInstant } from "../../precision/validate/isValidInstant.js";
// src/transport/compare/isPastCutoff.ts
/**
* Report whether a cut-off has passed.
*
* The window to meet a cut-off is half-open, `[…, cutoff)`: at the cut-off instant itself it
* has closed, so `now` equal to `cutoff` is past. This matches the half-open rule GMT's
* intervals follow, and `timeToCutoff`, which is `PT0S` there.
*
* - Both arguments are exact: an instant (`Z`/offset) or a zoned string, as `cutoffAt`
* returns one. They are compared as instants, so the zone each is written in does not
* matter, and the two passes of a repeated hour are told apart by their offsets.
* - **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 cut-off `cutoffAt` wrote is the instant it
* computed, 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.
* - `now` is the caller's: GMT does not read the clock here, so the answer is reproducible.
* - Returns `false` on invalid input, including `cutoffAt`'s `""` sentinel — which reads as
* "not past". Check the cut-off is not `""` before trusting a `false`.
*
* @param now ISO 8601 instant or zoned datetime string of the moment asked about
* @param cutoff ISO 8601 instant or zoned datetime string of the cut-off
* @returns true when now is at or after the cut-off, false otherwise or on invalid input
*
* @example isPastCutoff("2024-06-12T16:00:00Z", "2024-06-12T17:00:00+02:00[Europe/Amsterdam]") // true
* @example isPastCutoff("2024-06-12T14:00:00Z", "2024-06-12T17:00:00+02:00[Europe/Amsterdam]") // false
* @example isPastCutoff("2024-06-12T15:00:00Z", "2024-06-12T17:00:00+02:00[Europe/Amsterdam]") // true (at the cut-off, the window has closed)
* @example isPastCutoff("2024-04-07T02:30:00+10:00[Australia/Sydney]", "2024-04-07T02:30:00+11:00[Australia/Sydney]") // true (the second 02:30 is an hour after the first)
* @example isPastCutoff("2024-06-12T16:00:00", "2024-06-12T17:00:00+02:00[Europe/Amsterdam]") // false (a zoneless now is not a moment)
* @example isPastCutoff("2024-06-12T16:00:00Z", "") // false
*/
function isPastCutoff(now, cutoff) {
try {
if (!isValidInstant(now) || !isValidInstant(cutoff)) return false;
return Temporal.Instant.compare(instantFrom(now), instantFrom(cutoff)) >= 0;
} catch {
return false;
}
}
export { isPastCutoff };