UNPKG

@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.

60 lines • 4.87 kB
/** * Units `spanWallClock` measures a calendar distance in. * * | Unit | Meaning | * | --- | --- | * | `days` | Whole wall-clock days — a calendar day, whatever number of hours it happened to last. | * | `hours` | Whole wall-clock hours — the hours a clock face advanced, not the hours that elapsed. | * | `day`, `hour` | The singular names, accepted as Temporal accepts them. | */ export type WallClockSpanUnit = "days" | "hours" | "day" | "hour"; /** * Return the wall-clock distance between two zoned datetime strings — calendar time, not * elapsed time. * * The counterpart to `spanMs`. Both are correct answers to different questions, and * conflating them is the most common span bug there is: across a DST transition "one day * later" and "24 hours later" are not the same moment. * * - Reads each endpoint's own local wall clock and measures between them, so a calendar day * is 1 day and 24 hours in every zone, including one whose offset shifted in between. * `spanMs` over the same pair reports the 23, 24.5 or 25 hours that actually elapsed. * - Truncates toward zero, so the result is a whole count and `spanWallClock(b, a, unit)` is * `-spanWallClock(a, b, unit)`. Noon Friday to 18:00 Saturday is 1 day, or 30 hours — ask * for `hours` when the remainder matters. * - **Not a count of boundaries crossed.** 23:00 to 01:00 the next calendar day is `0` days, * not `1`; that question is about local midnights, and is `startOfZoned`'s to answer. * - **No disambiguation policy applies, because no local time is converted to an instant.** * The wall clock is read straight off the string with `Temporal.PlainDateTime.from`. Going * via `ZonedDateTime` would be a local → instant → local round trip, and that round trip is * lossy at exactly the DST edges this function exists to get right: an offset-less * `"2024-03-31T01:30:00[Europe/London]"` names a wall time that never occurred, and * Temporal's default `compatible` disambiguation would silently advance it to `02:30`, * turning a 24-hour answer into 23. A nonexistent or ambiguous local time is measured as * written instead — for a clock-face question, the digits in the string *are* the answer. * - Requires a bracketed IANA time zone on both endpoints, as `isValidZonedDateTime` does — * an instant string (`Z`) or a bare offset carries no zone whose wall clock could shift. * The offset is optional, but a present one must match the zone. Leap seconds and * `[u-ca=...]` calendar annotations are rejected; `days` and `hours` mean the same thing in * every calendar system, so the annotation would change nothing. * - The endpoints need not share a zone. When they differ, this is the distance between two * local wall clocks — a flight leaving São Paulo at 23:00 and landing in Berlin at 15:00 the * next local day is 16 wall-clock hours, and 12 elapsed hours by `spanMs`. * - Returns `null` on invalid input, not `0` — `0` is the span between a wall time and itself. * * @param start zoned ISO 8601 datetime string the span is measured from * @param end zoned ISO 8601 datetime string the span is measured to * @param unit "days" or "hours" (or the singular "day" or "hour") of wall-clock distance * @returns whole wall-clock units, negative when start is after end, or null on invalid input * * @example spanWallClock("2024-03-30T12:00:00+00:00[Europe/London]", "2024-03-31T12:00:00+01:00[Europe/London]", "hours") // 24 — spanMs reports 82800000 (23 hours) * @example spanWallClock("2024-04-06T12:00:00+11:00[Australia/Sydney]", "2024-04-07T12:00:00+10:00[Australia/Sydney]", "days") // 1 — spanMs reports 90000000 (25 hours) * @example spanWallClock("2024-03-01T12:00:00+05:30[Asia/Kolkata]", "2024-03-02T18:00:00+05:30[Asia/Kolkata]", "days") // 1 — truncated toward zero * @example spanWallClock("2024-03-02T18:00:00+05:30[Asia/Kolkata]", "2024-03-01T12:00:00+05:30[Asia/Kolkata]", "hours") // -30 * @example spanWallClock("2024-03-10T23:00:00-03:00[America/Sao_Paulo]", "2024-03-11T15:00:00+01:00[Europe/Berlin]", "hours") // 16 — each endpoint's own wall clock * @example spanWallClock("2024-03-31T01:30:00[Europe/London]", "2024-04-01T01:30:00[Europe/London]", "hours") // 24 — a wall time that never occurred is measured as written * @example spanWallClock("2024-03-01T12:00:00+02:00[Africa/Johannesburg]", "2024-03-02T12:00:00+02:00[Africa/Johannesburg]", "day") // 1 — singular unit name * @example spanWallClock("2024-03-10T12:00:00Z", "2024-03-11T12:00:00Z", "days") // null — no bracketed time zone * @example spanWallClock("2024-03-01T12:00:00+02:00[Africa/Johannesburg]", "2024-03-02T12:00:00+02:00[Africa/Johannesburg]", "minutes") // null */ export declare function spanWallClock(start: string, end: string, unit: WallClockSpanUnit): number | null;