Handle daylight saving time
Days of 23 and 25 hours, wall-clock times the clocks skip or repeat, and the rows of a day view, with @moltenship/calendar-temporal.
Twice a year, in a zone with daylight saving time, a date isn't 24 hours long. In Berlin the clocks go from 02:00 to 03:00 on March 29, 2026, so that date lasts 23 hours and 02:15 never happens. On October 25 they go from 03:00 back to 02:00, so the date lasts 25 hours and 02:15 happens twice. Code that adds 24 hours to midnight, or that turns "02:15" into an instant without looking, is wrong on those two dates.
The functions on this page take the time zone as an argument and never read the host's.
How long a date is
zonedDayRange(date, timeZone) returns the real start and end of a date in a zone, and its length:
import {
resolveZonedWallTime,
slotsInZonedDay,
snapZonedWallTime,
zonedDayRange,
} from "@moltenship/calendar-temporal";
/** How many hours a date lasts in a time zone: 23, 24 or 25 in most of them. */
export function hoursInDay(date: Temporal.PlainDate, timeZone: string): number {
return zonedDayRange(date, timeZone).duration.total("hours");
}In Europe/Berlin, hoursInDay returns 23 for March 29, 2026, 25 for October 25 and 24 for
October 8.
Which events are on a date
interval on the result is the range of instants to compare events with. Compare with it, and not
with midnight plus 24 hours:
/** Whether an event's instants overlap a date in a time zone. */
export function isOnDate(
event: { readonly start: Temporal.Instant; readonly end: Temporal.Instant },
date: Temporal.PlainDate,
timeZone: string,
): boolean {
const day = zonedDayRange(date, timeZone);
// A few zones have skipped a whole date. Such a date has no instants to compare with.
if (day.kind === "skipped") return false;
return (
Temporal.Instant.compare(event.start, day.interval.end) < 0 &&
Temporal.Instant.compare(event.end, day.interval.start) > 0
);
}An event from 22:30 to 23:00 UTC on October 8 is on October 9 in Berlin, where it starts at 00:30, and on October 8 in New York.
kind is "skipped" in the rare case where a zone skips a whole date, as Samoa did on December
30, 2011. Such a date has no instants, and interval is null.
For an event that crosses midnight, splitInstantRangeByZonedDay(range, timeZone) cuts its range
into one piece per date, and each piece says whether the event continues before or after that
date.
The rows of a day view
A day view shows the wall clock: 24 rows labelled 00:00 to 23:00, on every date. slotsInZonedDay
returns those rows with the instants behind each:
export interface HourRow {
/** The time on the wall clock, such as "02:00". */
readonly label: string;
/** "gap" for an hour the clocks skip, "fold" for one they repeat, else "unique". */
readonly kind: string;
/** When the row starts: no instant in a gap, two in a fold. */
readonly starts: readonly Temporal.Instant[];
}
/** The hour rows of a day view. Every day has the same 24 labels, whatever its length. */
export function hourRows(date: Temporal.PlainDate, timeZone: string): HourRow[] {
const { slots } = slotsInZonedDay(date, {
timeZone,
disambiguation: "compatible",
slotDuration: { hours: 1 },
});
return slots.map((slot) => ({
label: slot.wallRange.start.toPlainTime().toString({ smallestUnit: "minute" }),
kind: slot.kind,
starts: slot.segments.flatMap((segment) =>
segment.occurrences.map((occurrence) => occurrence.interval.start),
),
}));
}On March 29 in Berlin the 02:00 row is a gap and has no instant. On October 25 it is a fold and has two:
March 29, 2026 October 25, 2026
01:00 unique 2026-03-29T00:00:00Z 01:00 unique 2026-10-24T23:00:00Z
02:00 gap 02:00 fold 2026-10-25T00:00:00Z 2026-10-25T01:00:00Z
03:00 unique 2026-03-29T01:00:00Z 03:00 unique 2026-10-25T02:00:00ZA slot that a clock change cuts through is split at the change: its kind is "transition" and it
has one segment for each part. The function throws a RangeError when a day would have more than
10,000 slots, and limit raises that.
A time a person typed
A form gives a date and a time with no zone. resolveZonedWallTime turns them into an instant and
says what it found: kind is "unique", "gap" or "fold".
export type PickedTime =
| { readonly ok: true; readonly instant: Temporal.Instant }
| { readonly ok: false; readonly reason: string };
/** The instant of a date and time a person typed into a form. */
export function pickTime(wallTime: Temporal.PlainDateTime, timeZone: string): PickedTime {
const result = resolveZonedWallTime(wallTime, { timeZone, disambiguation: "compatible" });
if (result.status === "rejected") return { ok: false, reason: "That time can't be used." };
if (result.kind === "gap") {
// The clocks skipped this time. `adjustment` has the time "compatible" moved it to.
const moved = result.adjustment?.resolvedWallTime.toPlainTime().toString({
smallestUnit: "minute",
});
return { ok: false, reason: `The clocks skip that time. Did you mean ${moved}?` };
}
// In a fold the time happens twice, and `selected` is the first of `occurrences`.
return { ok: true, instant: result.selected.instant };
}With Berlin as the zone:
| The person typed | pickTime returns |
|---|---|
| October 8, 02:15 | The instant 2026-10-08T00:15:00Z |
| March 29, 02:15 | "The clocks skip that time. Did you mean 03:15?" |
| October 25, 02:15 | The instant 2026-10-25T00:15:00Z, the first 02:15 |
disambiguation takes Temporal's four values. "compatible" moves a skipped time later by the
length of the gap and picks the earlier of two repeated times. "reject" gives a result whose
status is "rejected" for both. In a fold, occurrences holds both instants, so a form can ask
which one the person meant.
A pointer on a time grid
A drag gives a wall-clock time that needs rounding to the grid. snapZonedWallTime rounds it and
resolves it in one step:
/** A pointer's position on a day view, rounded to the 15-minute grid. */
export function snapToGrid(wallTime: Temporal.PlainDateTime, timeZone: string) {
return snapZonedWallTime(wallTime, { timeZone, increment: 15, smallestUnit: "minute" });
}09:07 on October 8 snaps to 09:00. 02:10 on March 29 has no 02:00 or 02:15 to snap to, so it lands
on 03:00, the moment the clocks change. mode is "nearest" by default, or "floor" or "ceil",
and fold picks the earlier or the later of two repeated times.