Skip to content
Closed
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
5 changes: 4 additions & 1 deletion src/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export function timeInterval(floori, offseti, count, field) {
};

interval.offset = (date, step) => {
return offseti(date = new Date(+date), step == null ? 1 : Math.floor(step)), date;
date = new Date(+date);
step = step == null ? 1 : Math.floor(step);
if (step !== 0) offseti(date, step);
return date;
};

interval.range = (start, stop, step) => {
Expand Down
14 changes: 13 additions & 1 deletion test/interval-test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import assert from "node:assert";
import {it} from "vitest";
import {timeDay, timeHour, timeInterval, timeMinute, timeSecond, timeYear} from "d3-time";
import {timeDay, timeHour, timeInterval, timeMinute, timeMonth, timeSecond, timeWeek, timeYear} from "d3-time";
import {local, utc} from "./date.js";

it("timeInterval() is equivalent to timeInterval.floor(new Date)", () => {
Expand Down Expand Up @@ -107,3 +107,15 @@ it("timeInterval.every(…).offset(date, step) returns the expected value when s
assert.deepStrictEqual(i.offset(local(2015, 0, 1, 12, 34), 1.2), local(2015, 0, 1, 12, 45));
assert.deepStrictEqual(i.offset(local(2015, 0, 1, 12, 34), -0.8), local(2015, 0, 1, 12, 30));
});

it("timeInterval.offset(date, 0) preserves the second occurrence of a repeated local time", () => {
const date = new Date("2024-11-03T09:30:00.000Z"); // 01:30 PST in America/Los_Angeles
for (const interval of [timeDay, timeWeek, timeMonth, timeYear, timeYear.every(10)]) {
for (const step of [0, -0, "0", 0.8]) {
const result = interval.offset(date, step);
assert.deepStrictEqual(result, new Date("2024-11-03T09:30:00.000Z"));
assert.notStrictEqual(result, date);
}
}
assert.deepStrictEqual(date, new Date("2024-11-03T09:30:00.000Z"));
});