From 6b37baafd08015e607aca65c49aae18a05c26691 Mon Sep 17 00:00:00 2001 From: developjik Date: Mon, 23 Nov 2026 05:11:42 +0900 Subject: [PATCH] fix non-divisor fixed-duration intervals such as 11 hours MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 #2452 --- CHANGELOG.md | 4 ++++ src/time.js | 26 +++++++++++++++++++++++--- test/marks/time-test.js | 40 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fb76f0497b..f193437664 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ Year: **Current** · [2025](./CHANGELOG-2025.md) · [2024](./CHANGELOG-2024.md) · [2023](./CHANGELOG-2023.md) · [2022](./CHANGELOG-2022.md) · [2021](./CHANGELOG-2021.md) +## Unreleased + +- Fixes the interpretation of fixed-duration time intervals whose period doesn’t divide the parent unit, such as **“11 hours”** or **“65 minutes”**. These now anchor to the Unix epoch and recur exactly every *n* units, as [d3’s millisecond.every](https://d3js.org/d3-time#interval_every) does, instead of restarting at (UTC) midnight each day — so, for example, binning hourly events into 11-hour bins now produces equal-width bins. Multiples that divide the parent unit (say, “3 hours”) are unaffected in whole-hour time zones; calendar units (days, weeks, months, years) keep their current behavior. [#2452](https://github.com/observablehq/plot/issues/2452) + ## 0.6.18 [Released November 3, 2026.](https://github.com/observablehq/plot/releases/tag/v0.6.18) diff --git a/src/time.js b/src/time.js index 118c99daad..0c90a729d2 100644 --- a/src/time.js +++ b/src/time.js @@ -1,4 +1,4 @@ -import {bisector, max, pairs, timeFormat, utcFormat} from "d3"; +import {bisector, max, pairs, timeFormat, utcFormat, timeInterval as d3TimeInterval} from "d3"; import {utcSecond, utcMinute, utcHour, unixDay, utcWeek, utcMonth, utcYear} from "d3"; import {utcMonday, utcTuesday, utcWednesday, utcThursday, utcFriday, utcSaturday, utcSunday} from "d3"; import {timeSecond, timeMinute, timeHour, timeDay, timeWeek, timeMonth, timeYear} from "d3"; @@ -191,11 +191,31 @@ export function utcInterval(input) { return asInterval(parseTimeInterval(input), "utc"); } +// Units whose duration is an exact number of milliseconds. Unlike calendar +// units such as days and months, a multiple of one of these units (say, “11 +// hours") means a fixed span of time and is anchored to the Unix epoch below. +const durationUnits = new Set(["second", "minute", "hour"]); + +// d3’s interval.every returns a filtered interval that only keeps the +// boundaries whose clock field (say, getHours) is a multiple of the period, +// so a multiple that doesn’t divide the parent unit (say, “11 hours” within a +// 24-hour day) restarts at midnight each day rather than recurring every 11 +// hours. For units with an exact duration, anchor the boundaries to the Unix +// epoch instead, the same way d3 implements millisecond.every. +function durationInterval(duration) { + return d3TimeInterval( + (date) => date.setTime(Math.floor(+date / duration) * duration), + (date, step) => date.setTime(+date + step * duration), + (start, end) => (end - start) / duration + ); +} + function asInterval([name, period], type) { let interval = (type === "time" ? timeIntervals : utcIntervals).get(name); if (period > 1) { - interval = interval.every(period); - interval[intervalDuration] = durations.get(name) * period; + const duration = durations.get(name); + interval = durationUnits.has(name) ? durationInterval(duration * period) : interval.every(period); + interval[intervalDuration] = duration * period; interval[intervalType] = type; } return interval; diff --git a/test/marks/time-test.js b/test/marks/time-test.js index ea09e0613c..203212f791 100644 --- a/test/marks/time-test.js +++ b/test/marks/time-test.js @@ -75,7 +75,26 @@ it("timeInterval('n seconds') returns the expected time interval", () => { const end = new Date("2012-01-01T12:14:08"); assert.deepStrictEqual(timeInterval("5 seconds").range(start, end), d3.timeSecond.every(5).range(start, end)); assert.deepStrictEqual(timeInterval("15 seconds").range(start, end), d3.timeSecond.every(15).range(start, end)); - assert.deepStrictEqual(timeInterval("45 seconds").range(start, end), d3.timeSecond.every(45).range(start, end)); + // 45 does not divide a minute, so unlike d3’s timeSecond.every(45), which + // restarts at the top of each minute, the interval recurs every 45 seconds. + assert.deepStrictEqual(timeInterval("45 seconds").range(start, end), durationRange(45e3, start, end)); +}); + +it("timeInterval('n hours') recurs every n hours when n does not divide the day", () => { + const start = new Date("2026-07-31T19:30:00"); + const end = new Date("2026-08-02T12:00:00"); + const range = timeInterval("11 hours").range(start, end); + assert.deepStrictEqual(range, durationRange(11 * 36e5, start, end)); +}); + +it("timeInterval('n minutes') floors and ceils exactly n minutes apart", () => { + const date = new Date("2026-08-01T12:33:00"); + const interval = timeInterval("65 minutes"); + assert.deepStrictEqual( + interval.range(date, new Date("2026-08-01T17:00:00")), + durationRange(65 * 6e4, date, new Date("2026-08-01T17:00:00")) + ); + assert.strictEqual((interval.ceil(date) - interval.floor(date)) / 6e4, 65); }); it("utcInterval('period') returns the expected UTC interval", () => { @@ -151,5 +170,22 @@ it("utcInterval('n seconds') returns the expected UTC interval", () => { const end = new Date("2012-01-01T12:14:08"); assert.deepStrictEqual(utcInterval("5 seconds").range(start, end), d3.utcSecond.every(5).range(start, end)); assert.deepStrictEqual(utcInterval("15 seconds").range(start, end), d3.utcSecond.every(15).range(start, end)); - assert.deepStrictEqual(utcInterval("45 seconds").range(start, end), d3.utcSecond.every(45).range(start, end)); + assert.deepStrictEqual(utcInterval("45 seconds").range(start, end), durationRange(45e3, start, end)); }); + +it("utcInterval('n hours') recurs every n hours when n does not divide the day", () => { + const start = new Date("2026-07-31T19:30:00"); + const end = new Date("2026-08-02T12:00:00"); + const range = utcInterval("11 hours").range(start, end); + assert.deepStrictEqual(range, durationRange(11 * 36e5, start, end)); + // A fixed-duration interval is anchored to the Unix epoch, so the local and + // UTC flavors produce the same boundaries. + assert.deepStrictEqual(range, timeInterval("11 hours").range(start, end)); +}); + +// The boundaries of a fixed-duration interval: every step milliseconds since +// the Unix epoch, starting at the first boundary at or after start. +function durationRange(step, start, end) { + const t0 = Math.ceil(+start / step) * step; + return Array.from({length: Math.max(0, Math.ceil((+end - t0) / step))}, (_, i) => new Date(t0 + i * step)); +}