diff --git a/src/interval.js b/src/interval.js index 492ae90..ddabf13 100644 --- a/src/interval.js +++ b/src/interval.js @@ -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) => { diff --git a/test/day-test.js b/test/day-test.js index 210fef9..3490950 100644 --- a/test/day-test.js +++ b/test/day-test.js @@ -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"; @@ -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); diff --git a/test/sunday-test.js b/test/sunday-test.js index d67ac48..d0a4bb9 100644 --- a/test/sunday-test.js +++ b/test/sunday-test.js @@ -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"; @@ -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; + } +});