diff --git a/src/time.js b/src/time.js index d1ea678..403c91a 100644 --- a/src/time.js +++ b/src/time.js @@ -9,7 +9,7 @@ function date(t) { } function number(t) { - return t instanceof Date ? +t : +new Date(+t); + return t instanceof Date ? +t : Math.round(t); } export function calendar(ticks, tickInterval, year, month, week, day, hour, minute, second, format) { @@ -37,7 +37,7 @@ export function calendar(ticks, tickInterval, year, month, week, day, hour, minu } scale.invert = function(y) { - return new Date(invert(y)); + return new Date(Math.round(invert(y))); }; scale.domain = function(_) { diff --git a/test/utcTime-test.js b/test/utcTime-test.js index 729feaa..821df2e 100644 --- a/test/utcTime-test.js +++ b/test/utcTime-test.js @@ -4,6 +4,17 @@ import {utcDay, utcMinute, utcMonth, utcWeek, utcYear} from "d3-time"; import {scaleUtc} from "../src/index.js"; import {utc} from "./date.js"; +it("scaleUtc.invert() rounds to the nearest millisecond", () => { + const x = scaleUtc().domain([new Date("2022-08-31T22:00Z"), new Date("2022-09-30T21:59:59.999Z")]).range([0, 946]); + const date = new Date("2022-09-04T22:00Z"); + assert.deepStrictEqual(x.invert(x(date)), date); +}); + +it("scaleUtc.domain() rounds to the nearest millisecond", () => { + const x = scaleUtc().domain([1661983200000.1, 1664575199999.9]).range([0, 946]); + assert.deepStrictEqual(x.domain(), [new Date(1661983200000), new Date(1664575200000)]); +}); + it("scaleUtc.nice() is an alias for scaleUtc.nice(10)", () => { const x = scaleUtc().domain([utc(2009, 0, 1, 0, 17), utc(2009, 0, 1, 23, 42)]); assert.deepStrictEqual(x.nice().domain(), [utc(2009, 0, 1), utc(2009, 0, 2)]);