Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 23 additions & 3 deletions src/time.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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;
Expand Down
40 changes: 38 additions & 2 deletions test/marks/time-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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", () => {
Expand Down Expand Up @@ -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));
}