Skip to content

Taxonomy

Timezone and DST

A date is not an instant, and a day is not 24 hours.

What it looks like

  • A local date treated as absolute, or an absolute one formatted without a zone.
  • A day computed by adding 86,400,000 milliseconds.
  • 'Start of day' computed in the server's zone rather than the user's.

Why it survives review

It is correct for most of the year, in most zones, for anybody in UTC — which includes the CI machine. The two days a year it is wrong are not in anybody's test suite, and the reviewer reading it is almost certainly not thinking about Lord Howe Island.

How to see it

  1. For every date, ask whether it is an instant or a calendar date. They are different types and they are almost never interchangeable.
  2. Any arithmetic that adds a fixed number of milliseconds to get 'tomorrow' is wrong across a DST boundary.
  3. Ask whose timezone the answer is in — the user's, the server's, or the data's — and whether the code says so.

A minimal pair

Correct

return differenceInCalendarDays(end, start);

Defective

return Math.ceil((end - start) / 86_400_000);

A range spanning a clock change is 23 or 25 hours long, and the defective build rounds it to the wrong number of days.

Practise it

1 diff in the corpus carry this class. They are not listed, because knowing which diff contains what would make finding it a comprehension question about this page.

Go to the exercises