Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

round before coercing to date #281

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/time.js
Expand Up @@ -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) {
Expand Down Expand Up @@ -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(_) {
Expand Down
11 changes: 11 additions & 0 deletions test/utcTime-test.js
Expand Up @@ -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)]);
Expand Down