fix: generateConsecutivePeriods to end 1 millisecond before next period start

wip-related-pages
sebastien.arod@gmail.com 2024-06-04 21:26:08 +02:00
parent 39eb9b08b4
commit 74f3390413
2 changed files with 5 additions and 5 deletions

View File

@ -14,11 +14,11 @@ describe("generateConsecutivePeriods", () => {
expect(results[0]).toEqual({ expect(results[0]).toEqual({
start: new Date(Date.UTC(2020, 0, 1)), 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({ expect(results[4]).toEqual({
start: new Date(Date.UTC(2024, 0, 1)), 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)),
}); });
}); });
}); });

View File

@ -1,3 +1,4 @@
import { addMilliseconds } from "date-fns";
import { Period } from "./Period"; import { Period } from "./Period";
export default function generateConsecutivePeriods({ export default function generateConsecutivePeriods({
@ -14,7 +15,8 @@ export default function generateConsecutivePeriods({
let nextPeriodStart = start; let nextPeriodStart = start;
do { do {
const periodStart = nextPeriodStart; const periodStart = nextPeriodStart;
const periodEnd = incrementStart(nextPeriodStart); nextPeriodStart = incrementStart(periodStart);
const periodEnd = addMilliseconds(nextPeriodStart, -1);
if (periodEnd.getTime() < periodStart.getTime()) { if (periodEnd.getTime() < periodStart.getTime()) {
throw new Error("incrementStart call decremented date!!!"); throw new Error("incrementStart call decremented date!!!");
} }
@ -22,8 +24,6 @@ export default function generateConsecutivePeriods({
start: periodStart, start: periodStart,
end: periodEnd, end: periodEnd,
}); });
nextPeriodStart = periodEnd;
} while (nextPeriodStart.getTime() < end.getTime()); } while (nextPeriodStart.getTime() < end.getTime());
} }
return periods; return periods;