From 74f3390413d47853939df514982fd48faf44cd2c Mon Sep 17 00:00:00 2001 From: "sebastien.arod@gmail.com" Date: Tue, 4 Jun 2024 21:26:08 +0200 Subject: [PATCH] fix: generateConsecutivePeriods to end 1 millisecond before next period start --- src/period/generateConsecutivePeriods.test.ts | 4 ++-- src/period/generateConsecutivePeriods.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/period/generateConsecutivePeriods.test.ts b/src/period/generateConsecutivePeriods.test.ts index 66869d1..fa16080 100644 --- a/src/period/generateConsecutivePeriods.test.ts +++ b/src/period/generateConsecutivePeriods.test.ts @@ -14,11 +14,11 @@ describe("generateConsecutivePeriods", () => { expect(results[0]).toEqual({ start: new Date(Date.UTC(2020, 0, 1)), - end: new Date(Date.UTC(2021, 0, 1)), + end: new Date(Date.UTC(2020, 11, 31, 23, 59, 59, 999)), }); expect(results[4]).toEqual({ start: new Date(Date.UTC(2024, 0, 1)), - end: new Date(Date.UTC(2025, 0, 1)), + end: new Date(Date.UTC(2024, 11, 31, 23, 59, 59, 999)), }); }); }); diff --git a/src/period/generateConsecutivePeriods.ts b/src/period/generateConsecutivePeriods.ts index 010749b..1bc9367 100644 --- a/src/period/generateConsecutivePeriods.ts +++ b/src/period/generateConsecutivePeriods.ts @@ -1,3 +1,4 @@ +import { addMilliseconds } from "date-fns"; import { Period } from "./Period"; export default function generateConsecutivePeriods({ @@ -14,7 +15,8 @@ export default function generateConsecutivePeriods({ let nextPeriodStart = start; do { const periodStart = nextPeriodStart; - const periodEnd = incrementStart(nextPeriodStart); + nextPeriodStart = incrementStart(periodStart); + const periodEnd = addMilliseconds(nextPeriodStart, -1); if (periodEnd.getTime() < periodStart.getTime()) { throw new Error("incrementStart call decremented date!!!"); } @@ -22,8 +24,6 @@ export default function generateConsecutivePeriods({ start: periodStart, end: periodEnd, }); - - nextPeriodStart = periodEnd; } while (nextPeriodStart.getTime() < end.getTime()); } return periods;