From db1cac1c71c28f98d7087d28bacc75b2639b56c4 Mon Sep 17 00:00:00 2001 From: magiccwarss Date: Sat, 7 Nov 2026 17:09:28 +0400 Subject: [PATCH] Count day and week boundaries across skipped midnights --- src/day.js | 3 ++- src/week.js | 3 ++- test/midnight-count-test.js | 23 +++++++++++++++++++++++ 3 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/midnight-count-test.js diff --git a/src/day.js b/src/day.js index 2b5d4b6..98d2a55 100644 --- a/src/day.js +++ b/src/day.js @@ -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 ); diff --git a/src/week.js b/src/week.js index f75ff52..0066c36 100644 --- a/src/week.js +++ b/src/week.js @@ -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); }); } diff --git a/test/midnight-count-test.js b/test/midnight-count-test.js new file mode 100644 index 0000000..98b4eee --- /dev/null +++ b/test/midnight-count-test.js @@ -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]); +});