@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.
74 lines (72 loc) • 3.4 kB
JavaScript
import { zonedDateTimeFrom } from "../../internal/zonedWallClock.js";
import { zonedNextTransition, zonedStartOfDay } from "../../internal/zonedWallClockOperations.js";
import { zoneFrame } from "../../internal/zoneFrame.js";
// src/zoned/get/getDstTransitions.ts
const MAX_TRANSITIONS_PER_YEAR = 20;
/** 368 days: a 366-day year plus a day at each end for the zone's offset. */
const SEARCH_SPAN_NANOSECONDS = 368n * 86400000000000n;
/**
* List every DST transition instant for a time zone (an IANA name or a UTC offset) within a
* given year.
*
* - A "transition" is any UTC offset change: a spring-forward gap (nonexistent
* local time) or a fall-back overlap (ambiguous local time) — see
* `docs/dst-disambiguation.md` for the gap/overlap terminology. This is
* distinct from `hasDaylightSaving` (whether a zone observes DST at all)
* and from the `disambiguation`/`offset` options (what to do when
* constructing a single instant that lands in a gap/overlap).
* - Most zones have 0 or 2 transitions per year; some (e.g. `Africa/Casablanca`,
* which pauses DST for Ramadan) can have more.
* - A transition landing exactly on local January 1 00:00 belongs to that year
* (e.g. `Asia/Singapore` moving to +08:00 at the start of 1982).
* - Returns `[]` for an invalid timeZone, a non-integer year, or a valid zone
* with zero transitions in that year (not an error case), and when the scan
* exhausts its internal bound — never a partial list.
* - A UTC offset, with or without seconds (`"+05:30"`, `"-00:44:30"`), never changes, so it has no
* transitions and returns `[]` for every year.
*
* @param timeZone IANA name or UTC offset: a time zone identifier (`+05:30`, `+0530`, `-08`) or a
* stored offset (`±HH:MM[:SS]`, what `getTimeZoneOffset` returns)
* @param year calendar year to scan (must be an integer)
* @returns array of `{ instant, offsetBefore, offsetAfter }`, in chronological order
*
* @example getDstTransitions("Australia/Sydney", 2024)
* // [
* // { instant: "2024-04-06T16:00:00Z", offsetBefore: "+11:00", offsetAfter: "+10:00" },
* // { instant: "2024-10-05T16:00:00Z", offsetBefore: "+10:00", offsetAfter: "+11:00" },
* // ]
* @example getDstTransitions("Asia/Singapore", 1982)
* // [{ instant: "1981-12-31T16:00:00Z", offsetBefore: "+07:30", offsetAfter: "+08:00" }]
* @example getDstTransitions("Asia/Tokyo", 2024) // []
* @example getDstTransitions("+05:30", 2024) // [] (a fixed offset has no transitions)
* @example getDstTransitions("Invalid/Zone", 2024) // []
* @example getDstTransitions("-00:44:30", 1970) // [] (a fixed offset)
*/
function getDstTransitions(timeZone, year) {
const frame = zoneFrame(timeZone);
if (frame === null || frame.shiftNanoseconds !== 0n || !Number.isInteger(year)) return [];
try {
let cur = zonedStartOfDay(zonedDateTimeFrom({
year,
month: 1,
day: 1,
timeZone: frame.timeZone
})).subtract({ nanoseconds: 1 });
const searchEnd = cur.epochNanoseconds + SEARCH_SPAN_NANOSECONDS;
const transitions = [];
for (let i = 0; i <= MAX_TRANSITIONS_PER_YEAR; i++) {
const next = zonedNextTransition(cur, searchEnd);
if (!next || next.year > year) return transitions;
transitions.push({
instant: next.toInstant().toString(),
offsetBefore: cur.offset,
offsetAfter: next.offset
});
cur = next;
}
return [];
} catch {
return [];
}
}
export { getDstTransitions };