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
3 changes: 2 additions & 1 deletion src/day.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {durationDay, durationMinute} from "./duration.js";
export const timeDay = timeInterval(
date => date.setHours(0, 0, 0, 0),
(date, step) => date.setDate(date.getDate() + step),
(start, end) => (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationDay,
// A boundary may be after midnight when daylight saving time skips it.
(start, end) => Math.round((end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationDay),
date => date.getDate() - 1
);

Expand Down
3 changes: 2 additions & 1 deletion src/week.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ function timeWeekday(i) {
}, (date, step) => {
date.setDate(date.getDate() + step * 7);
}, (start, end) => {
return (end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationWeek;
// A boundary may be after midnight when daylight saving time skips it.
return Math.round((end - start - (end.getTimezoneOffset() - start.getTimezoneOffset()) * durationMinute) / durationWeek);
});
}

Expand Down
23 changes: 23 additions & 0 deletions test/midnight-count-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import assert from "node:assert";
import {execFileSync} from "node:child_process";
import process from "node:process";
import {it} from "vitest";

function countInSaoPaulo(interval, start, end) {
return JSON.parse(execFileSync(process.execPath, ["--input-type=module", "--eval", `
import {${interval} as interval} from "./src/index.js";
const start = new Date(${JSON.stringify(start)}), end = new Date(${JSON.stringify(end)});
console.log(JSON.stringify([interval.count(start, end), interval.count(end, start)]));
`], {env: {...process.env, TZ: "America/Sao_Paulo"}, encoding: "utf8"}));
}

it("timeDay.count counts boundaries after a day whose midnight is skipped", () => {
// DST began at midnight on November 4: the day starts at 01:00 local time.
assert.deepStrictEqual(countInSaoPaulo("timeDay", "2018-11-04T03:00:00.000Z", "2018-11-05T02:00:00.000Z"), [1, -1]);
assert.deepStrictEqual(countInSaoPaulo("timeDay", "2018-11-03T03:00:00.000Z", "2018-11-04T03:00:00.000Z"), [1, -1]);
});

it("timeSunday.count counts boundaries after a week whose midnight is skipped", () => {
assert.deepStrictEqual(countInSaoPaulo("timeSunday", "2018-11-04T03:00:00.000Z", "2018-11-11T02:00:00.000Z"), [1, -1]);
assert.deepStrictEqual(countInSaoPaulo("timeSunday", "2018-10-28T03:00:00.000Z", "2018-11-04T03:00:00.000Z"), [1, -1]);
});