Sub topic 14
Daylight Saving Time and Ambiguous Wall-Clock Times
This chapter assumes you know how to create a Temporal.ZonedDateTime and how add treats calendar units differently from exact units. Here you will see what Temporal does when a local time is skipped or repeated, and how you can choose the result yourself.
Gaps and folds
A wall-clock time does not always map to exactly one instant. Daylight saving transitions break that mapping in two ways:
- Gap. In spring the clocks jump forward. In New York on 2024-03-10, the clock goes from 01:59:59 straight to 03:00, so 02:30 never happens.
- Fold. In autumn the clocks fall back. On 2024-11-03, the hour from 01:00 to 01:59 happens twice, first at offset -04:00 and then at -05:00.
Whenever Temporal turns a wall-clock value into an exact time, it reads the disambiguation option to decide what to do with a gap or a fold.
The four options
| Option | Gap (02:30 on 2024-03-10) | Fold (01:30 on 2024-11-03) |
|---|---|---|
| `'compatible'` (default) | 03:30-04:00 | 01:30-04:00 |
| `'earlier'` | 01:30-05:00 | 01:30-04:00 |
| `'later'` | 03:30-04:00 | 01:30-05:00 |
| `'reject'` | throws `RangeError` | throws `RangeError` |
The default, 'compatible', gives the same result as legacy Date. That keeps migrated code consistent, but it can quietly move a time that a user entered.
const fields = {
timeZone: 'America/New_York',
year: 2024, month: 3, day: 10, hour: 2, minute: 30,
};
console.log(Temporal.ZonedDateTime.from(fields).toString());
// '2024-03-10T03:30:00-04:00[America/New_York]'
console.log(Temporal.ZonedDateTime.from(fields, { disambiguation: 'earlier' }).toString());
// '2024-03-10T01:30:00-05:00[America/New_York]'
const fold = Temporal.PlainDateTime.from('2024-11-03T01:30');
console.log(fold.toZonedDateTime('America/New_York', { disambiguation: 'later' }).toString());
// '2024-11-03T01:30:00-05:00[America/New_York]'
try {
Temporal.ZonedDateTime.from(fields, { disambiguation: 'reject' });
} catch (e) {
console.log(e instanceof RangeError); // true
}The option is accepted by ZonedDateTime.from, with, PlainDateTime.prototype.toZonedDateTime, and the other methods that resolve a local time.
Common mistakes
- Assuming every day has 24 hours. On a transition day,
zdt.hoursInDayis 23 or 25. Compute day lengths withhoursInDayoruntilinstead of hard-coding 24. - Storing only wall-clock values for future events. If a rule change lands between saving and reading the value, the stored offset may conflict. When you parse a string that has both an offset and a zone, the
offsetoption ('use','ignore','prefer','reject') controls which one wins. Forfrom, the default is'reject'. - Forgetting that
withre-resolves. Changinghourwithwithcan land the value in a gap. Passdisambiguationthere too.
Exercise
A meeting is scheduled for 2024-11-03T01:30 in America/New_York, and it should take place during the second occurrence of 01:30. Which disambiguation value do you pass, and what offset appears in the result?
Show answer
Pass { disambiguation: 'later' }. The result is 2024-11-03T01:30:00-05:00[America/New_York], the occurrence after the clocks fall back to standard time.
Exercise
What does Temporal.ZonedDateTime.from('2024-03-10T02:30[America/New_York]') return when you pass no options, and why?
Show answer
It returns 2024-03-10T03:30:00-04:00[America/New_York]. 02:30 falls in the spring-forward gap, and the default 'compatible' resolves a gap by moving the time forward by the length of the gap.