intervalLengthZoned
Signature
Section titled “Signature”intervalLengthZoned(start: string, end: string, unit: string): numberimport { intervalLengthZoned } from "@northguild/gmt/zoned/interval";Return the exact length of a zoned interval in unit, as a real (possibly fractional) number.
- Distinct from
intervalCountZoned, which counts local calendarunitboundaries crossed rather than measuring exact duration — a local day that springs forward touches 1 day boundary viaintervalCountZonedbut is exactly23/24 ≈ 0.958days viaintervalLengthZoned. - Uses
Temporal.Duration.prototype.totalwithrelativeToset tostart, so the result is DST-aware: dividing a spring-forward day’s length in hours returns23, not24. - Returns
0for a zero-length interval (start === end). - Accepts GMT calendar-annotated zoned strings (as produced by
convertZonedToCalendar) as well as bare ISO ones — E7 (issue #152). When BOTH endpoints carry the same calendar tag the measurement is made in that calendar; when the tags mismatch, or either endpoint is bare ISO, it falls back to Gregorian/ISO rather than returning the sentinel (E7’s D5-zoned). The fallback is mandatory, not a convenience:ZonedDateTime.prototype.untilthrows across mismatched calendars for EVERYlargestUnit— verified, including"hour"and"nanosecond". - Returns
nullon invalid input (unparseable start/end,start > end, unsupported unit, leap-second strings).
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
start |
string |
ISO 8601 zoned datetime string for the interval start |
end |
string |
ISO 8601 zoned datetime string for the interval end |
unit |
string |
unit string — any DateTimeUnit |
Returns
Section titled “Returns”exact length of the interval expressed in unit, or null on invalid input
Examples
Section titled “Examples”intervalLengthZoned("2024-03-10T00:00:00-05:00[America/New_York]", "2024-03-11T00:00:00-04:00[America/New_York]", "hour") // 23 (spring forward)intervalLengthZoned("2024-03-10T00:00:00-05:00[America/New_York]", "2024-03-11T00:00:00-04:00[America/New_York]", "day") // 1intervalLengthZoned("2024-01-01T00:00:00+00:00[UTC]", "2024-01-01T00:00:00+00:00[UTC]", "day") // 0intervalLengthZoned("invalid", "2024-01-02T00:00:00+00:00[UTC]", "day") // null