Skip to content

anchor non-divisor fixed-duration intervals to the Unix epoch - #2484

Open
developjik wants to merge 1 commit into
observablehq:mainfrom
developjik:fix/non-divisor-time-intervals
Open

developjik wants to merge 1 commit into
observablehq:mainfrom
developjik:fix/non-divisor-time-intervals

Conversation

@developjik

Copy link
Copy Markdown

Fixes #2452.

What

Multiples of a fixed-duration unit whose period doesn't divide the parent unit — say "11 hours" within a 24-hour day, or "65 minutes" within an hour — are now anchored to the Unix epoch and recur exactly every n units, the same way d3 implements millisecond.every:

Plot.timeInterval("11 hours").range(new Date(2026, 7, 1), new Date(2026, 7, 3))
// before: 00:00, 11:00, 22:00, 00:00, 11:00, 22:00  (restarts at midnight each day)
// after:  04:00, 15:00, 02:00, 13:00               (every 11 hours)

Plot.timeInterval("65 minutes").floor(new Date(2026, 7, 1, 12, 33));
Plot.timeInterval("65 minutes").ceil(new Date(2026, 7, 1, 12, 33));
// before: 60 minutes apart
// after:  65 minutes apart

So binning hourly events with {interval: "11 hours"} now produces equal-width bins (the expectation in #2452) instead of a day split into 11h + 11h + 2h.

Why

asInterval implemented periods through d3's interval.every(period), which returns a filtered interval that keeps only the boundaries whose clock field (e.g. getHours()) is a multiple of the period:

d3.timeHour.every(11).range(new Date(2026, 7, 1), new Date(2026, 7, 3))
// 00:00, 11:00, 22:00, 00:00, …  — the phase restarts at midnight

That is a useful calendar alignment for calendar units (month.every(3) = quarters), but for a fixed duration the period restarting at (UTC) midnight each day makes some bins smaller than others and floor/ceil disagree with the interval's own duration. Anchoring to the epoch matches what millisecond.every already does in d3, and what Plot's numberInterval does for numeric intervals (Math.floor(d / n) * n).

Scope of the behavior change

  • Affected: periods > 1 of second, minute, hour whose period doesn't divide the parent unit (e.g. "11 hours", "65 minutes", "45 seconds", "90 seconds"), in both the time and utc flavors (a fixed duration has the same boundaries in either).
  • Unaffected: periods that divide the parent unit (e.g. "3 hours", "5 minutes") in whole-hour time zones, since the epoch grid and the clock-field grid coincide; and calendar units (day, week, monday, month, year), which keep using interval.every — a fixed epoch grid would be wrong across DST and calendar boundaries for those.
  • Since the boundaries are absolute instants, local-time DST transitions no longer shift a boundary: a bin is exactly n elapsed hours wide.

Tests

  • test/marks/time-test.js: the previous "45 seconds" assertions pinned the old minute-restart behavior (d3.timeSecond.every(45)); they now expect epoch-anchored boundaries via a durationRange helper, and new cases cover "11 hours" (time and utc), "65 minutes" floor/ceil, and that divisor periods ("5 seconds", "15 seconds") still match d3.
  • Full suite: 405 files, 1354 tests passed; eslint, prettier and tsc clean. No snapshot changes.

d3’s interval.every keeps only the boundaries whose clock field (say,
getHours) is a multiple of the period, so “11 hours” restarted at (UTC)
midnight each day and “65 minutes” produced floor and ceil 60 minutes
apart. Anchor fixed-duration units (seconds, minutes, hours) to the Unix
epoch instead, the way d3 implements millisecond.every.

Fixes observablehq#2452

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Binning temporal data using the built-in _hours_ interval forces interval boundaries to start at midnight

1 participant