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
2 changes: 1 addition & 1 deletion src/interval.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export function timeInterval(floori, offseti, count, field) {
interval.count = (start, end) => {
t0.setTime(+start), t1.setTime(+end);
floori(t0), floori(t1);
return Math.floor(count(t0, t1));
return Math.round(count(t0, t1));
};

interval.every = (step) => {
Expand Down
17 changes: 17 additions & 0 deletions test/day-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from "node:assert";
import process from "node:process";
import {it} from "vitest";
import {timeDay, timeDays, timeYear} from "d3-time";
import {local, utc} from "./date.js";
Expand Down Expand Up @@ -188,6 +189,22 @@ it("timeDay.count(start, end) observes daylight saving", () => {
assert.strictEqual(timeDay.count(local(2011, 0, 1), local(2011, 10, 6, 2)), 309);
});

it("timeDay.count(start, end) counts boundaries when the start day's midnight is skipped by DST", () => {
const TZ = process.env.TZ;
process.env.TZ = "America/Santiago";
try {
// In America/Santiago DST starts at midnight on 2025-09-07, so
// timeDay.floor of that day is 01:00 rather than 00:00.
const start = timeDay.floor(new Date(2025, 8, 7, 12));
assert.strictEqual(start.getHours(), 1);
assert.strictEqual(timeDay.count(start, timeDay.floor(new Date(2025, 8, 8, 12))), 1);
assert.strictEqual(timeDay.count(start, new Date(2025, 8, 17)), 10);
assert.strictEqual(timeDay.count(timeDay.floor(new Date(2025, 8, 8, 12)), new Date(2025, 8, 17)), 9);
} finally {
process.env.TZ = TZ;
}
});

it("timeDay.count(start, stop) does not exhibit floating-point rounding error", () => {
const date = new Date(2011, 4, 9);
assert.strictEqual(timeDay.count(timeYear(date), date), 128);
Expand Down
14 changes: 14 additions & 0 deletions test/sunday-test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from "node:assert";
import process from "node:process";
import {it} from "vitest";
import {timeSunday, timeSundays} from "d3-time";
import {local} from "./date.js";
Expand Down Expand Up @@ -154,3 +155,16 @@ it("timeSunday.count(start, end) observes daylight saving", () => {
assert.strictEqual(timeSunday.count(local(2011, 0, 1), local(2011, 10, 6, 1)), 45);
assert.strictEqual(timeSunday.count(local(2011, 0, 1), local(2011, 10, 6, 2)), 45);
});

it("timeSunday.count(start, end) counts boundaries when the start day's midnight is skipped by DST", () => {
const TZ = process.env.TZ;
process.env.TZ = "America/Santiago";
try {
const start = timeSunday.floor(new Date(2025, 8, 7, 12));
assert.strictEqual(start.getHours(), 1);
assert.strictEqual(timeSunday.count(start, new Date(2025, 8, 17)), 1);
assert.strictEqual(timeSunday.count(start, new Date(2025, 8, 21)), 2);
} finally {
process.env.TZ = TZ;
}
});