@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.
92 lines • 5.92 kB
TypeScript
import { Temporal } from "./temporal.js";
import { DateTimeUnit } from "../types/date-time-unit.js";
import "../types/index.js";
/**
* ISO day-of-week a week starts on: 1 = Monday .. 7 = Sunday, as `Temporal`'s `dayOfWeek` reads.
*/
export type WeekStartDay = 1 | 2 | 3 | 4 | 5 | 6 | 7;
/**
* Units the walker floors to: every `DateTimeUnit`, plus the calendar `quarter` (three local
* months starting January, April, July or October). Internal only — the public unit types stay
* unchanged.
*/
export type WalkerUnit = DateTimeUnit | "quarter";
/**
* Return the instant the local `unit` containing `zoned` began — the bucket `zoned` falls in.
*
* A bucket is the run of instants sharing `zoned`'s local label *without interruption*, so
* this walks back from `zoned` to where that run started. Re-resolving a truncated wall clock
* cannot do the job, because truncation can land on a local time the zone never had and
* Temporal's "compatible" disambiguation then relocates it:
*
* - **A gap pushes the boundary past the instant being floored.** `Pacific/Chatham` springs
* forward 02:45 → 03:45, so local 03:00 never happens; truncating 03:45 to the hour and
* resolving it lands fifteen minutes *after* the instant it was asked to floor.
* `Antarctica/Casey`'s three-hour 2020 jump lands three hours after.
* - **A sub-hour fall-back answers with the wrong pass.** Chatham falls back 03:45 → 02:45,
* so local 02:00 and local 03:00 each name two instants an hour and three quarters apart,
* and "compatible" always picks the earlier — silently skipping a boundary.
*
* So the boundary is taken in `zoned`'s own offset, then extended back through any transition
* the bucket runs straight through. See `startsNewBucketAt` for which those are.
*
* - Handles every `DateTimeUnit` plus `quarter`. Sub-hour units need the same own-offset treatment, because
* historic offsets carry seconds (`Africa/Monrovia` ran at −00:44:30 until 1972).
* - Weeks start on `weekStartsOn` (ISO day number, Monday by default).
* - Keeps `zoned`'s calendar, so a non-ISO month or year floors in that calendar.
* - Returns null if the walk-back is still inside transitions after `MAX_TRANSITION_WALKBACK`
* of them, which no IANA zone comes close to. The last `start` reached at that point is
* only a partial answer, and a bounded walk that runs out answers with the sentinel.
*/
export declare function zonedUnitStart(zoned: Temporal.ZonedDateTime, unit: WalkerUnit, weekStartsOn?: WeekStartDay): Temporal.ZonedDateTime | null;
/**
* Return the start of the bucket following the one containing `current` (which need not be that
* bucket's start).
*
* Stepping a whole unit and re-flooring is wrong, because a bucket can be shorter than its
* unit and would be stepped straight over: `Pacific/Chatham`'s 02:45 → 03:45 spring-forward
* leaves a local 03:00 hour only fifteen minutes long. So this takes whichever comes first —
* the next boundary in the current offset, or a transition that opens a bucket of its own —
* and runs through any transition that does not.
*
* - Handles every `DateTimeUnit` plus `quarter`, weeks starting on `weekStartsOn` (Monday by default).
* - Returns null if neither is reachable within `MAX_TRANSITION_WALKBACK` transitions, which
* no IANA zone comes close to.
* - Throws a RangeError when the next bucket starts after the last representable instant.
*/
export declare function nextZonedBucketStart(current: Temporal.ZonedDateTime, unit: WalkerUnit, weekStartsOn?: WeekStartDay): Temporal.ZonedDateTime | null;
/**
* Return the last instant of the local `unit` containing `zoned`: one nanosecond before the
* next bucket starts.
*
* - Never earlier than `zoned`, because it is measured from the real bucket `zoned` is in —
* London's second 01:00 hour on a fall-back morning ends at 01:59:59.999999999 +00:00, not
* at the first pass's.
* - Walks forward from `zoned` itself, never from its bucket's start. No transition between that
* start and `zoned` opens a bucket, so both walks reach the same next start — but the start may
* lie before the first representable instant (the month holding `-271821-04-20T00:00:00Z` began
* on 1 April), while the end does not.
* - Returns null if the next bucket start cannot be found.
*/
export declare function zonedUnitEnd(zoned: Temporal.ZonedDateTime, unit: WalkerUnit, weekStartsOn?: WeekStartDay): Temporal.ZonedDateTime | null;
/**
* Count the local `unit` buckets the half-open interval `[start, end)` touches, in `start`'s
* zone and calendar. `end` must already be expressed in both.
*
* The count is the bucket `start` is in, plus every bucket that starts after it and at or
* before `end` — less one when `end` sits exactly on a bucket start, since the interval does
* not reach into that bucket. A calendar-unit difference between truncated endpoints gets
* transitions wrong both ways: Chatham's 15-minute 03:00 hour is a bucket the difference
* never sees, and Samoa's deleted 2011-12-30 is a day the difference counts anyway.
*
* - Between transitions the wall clock runs continuously, so buckets there are counted by the
* whole-unit difference of their local labels.
* - A transition that does not open a bucket (see `startsNewBucketAt`) leaves the label
* running on, so it changes nothing and is stepped over.
* - A transition that does open one closes the run before it and counts the new bucket.
* - A `start` whose bucket began before the first representable instant still counts that
* bucket once.
* - Returns null if either endpoint's bucket cannot be found, or the span crosses more than
* `MAX_COUNTED_TRANSITIONS` transitions.
*/
export declare function countZonedBuckets(start: Temporal.ZonedDateTime, end: Temporal.ZonedDateTime, unit: DateTimeUnit, weekStartsOn?: WeekStartDay): number | null;