Skip to content

Fix interval count when DST skips the start day's midnight - #75

Closed
draarivpatel-ui wants to merge 1 commit into
d3:mainfrom
draarivpatel-ui:fix-count-skipped-midnight
Closed

draarivpatel-ui wants to merge 1 commit into
d3:mainfrom
draarivpatel-ui:fix-count-skipped-midnight

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #74.

Problem

In time zones where DST starts at 00:00 local (America/Santiago, America/Havana, Asia/Beirut), floor of the DST-start day resolves to 01:00 because midnight does not exist. The elapsed-time quotient in interval.count is then N - 1/24 (or N - 1/168 for weeks), and Math.floor under-counts by one boundary:

// TZ=America/Santiago
const d = timeDay.floor(new Date(2025, 8, 7, 12)); // Sun Sep 07 01:00 GMT-0300
timeDay.count(d, timeDay.floor(new Date(2025, 8, 8, 12))); // 0, should be 1
timeDay.count(d, new Date(2025, 8, 17)); // 9, should be 10
timeWeek.count(d, new Date(2025, 8, 17)); // 0, should be 1

Fix

Both ends are already interval boundaries after floori(t0) / floori(t1), so the true count is always an integer. Round the quotient instead of flooring it: Math.floor to Math.round in interval.count. This also fixes the same class of off-by-one for timeWeek.count, month boundaries that land on a skipped midnight, and floating-point noise below the true value.

Test

Added cases to test/day-test.js and test/sunday-test.js that switch process.env.TZ to America/Santiago (restored in finally), verifying counts across the 2025-09-07 skipped midnight. They fail before the fix and pass after. Ran vitest run on the touched files plus the full suite under the repo's TZ=America/Los_Angeles: 467/467 pass.

In zones where DST starts at 00:00 local (America/Santiago,
America/Havana, Asia/Beirut), floor of the DST-start day lands at
01:00, so the elapsed-time quotient in interval.count is N - 1/24
and Math.floor under-counts by one boundary. Both ends are already
boundaries after flooring, so round instead of floor.

Fixes d3#74.
Copilot AI balanced review requested due to automatic review settings November 24, 2026 20:06

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

timeDay.count and timeWeek.count are one too small when start is on a day whose midnight is skipped by DST

2 participants