fix: prevent issues with midnight and day recognition on dst kick in

This commit is contained in:
Carlos Valente
2026-03-03 21:35:04 +01:00
committed by Carlos Valente
parent ceb490c60a
commit 26d887c1a6
4 changed files with 101 additions and 8 deletions
@@ -557,6 +557,36 @@ describe('hasCrossedMidnight()', () => {
const time = (15 * MILLIS_PER_HOUR) as TimeOfDay; // 15:00
expect(hasCrossedMidnight(time, time)).toBe(false);
});
describe('DST transitions', () => {
it('returns false during DST fall back (~1h backward jump)', () => {
// During fall back, clock goes from 02:59 → 02:00 (1h backward)
const previous = (2 * MILLIS_PER_HOUR + 59 * MILLIS_PER_MINUTE) as TimeOfDay; // 02:59
const current = (2 * MILLIS_PER_HOUR) as TimeOfDay; // 02:00
expect(hasCrossedMidnight(previous, current)).toBe(false);
});
it('returns true at actual midnight (~23h backward jump)', () => {
// Actual midnight crossing: 23:59 → 00:01 (~23h58m backward)
const previous = (23 * MILLIS_PER_HOUR + 59 * MILLIS_PER_MINUTE) as TimeOfDay; // 23:59
const current = (1 * MILLIS_PER_MINUTE) as TimeOfDay; // 00:01
expect(hasCrossedMidnight(previous, current)).toBe(true);
});
it('returns false for small backward jumps near midnight boundary', () => {
// Edge case: 12h backward is NOT a midnight cross
const previous = (12 * MILLIS_PER_HOUR) as TimeOfDay; // 12:00
const current = (0 * MILLIS_PER_HOUR) as TimeOfDay; // 00:00
expect(hasCrossedMidnight(previous, current)).toBe(false);
});
it('returns true for backward jumps exceeding 12h', () => {
// 12h + 1ms backward IS a midnight cross
const previous = (12 * MILLIS_PER_HOUR + 1) as TimeOfDay; // 12:00:00.001
const current = (0 * MILLIS_PER_HOUR) as TimeOfDay; // 00:00
expect(hasCrossedMidnight(previous, current)).toBe(true);
});
});
});
describe('skippedOutOfEvent()', () => {
+4 -3
View File
@@ -10,11 +10,12 @@ export const normaliseEndTime = (start: number, end: number) => (end < start ? e
/**
* Checks whether the local wall clock wrapped into a new day
* This currently uses a simple wrap heuristic and is centralized
* so day-boundary behavior can be evolved in one place later.
* Uses a threshold to distinguish midnight wrap (~23h backward jump)
* from DST fall back (~1h backward jump)
*/
export function hasCrossedMidnight(previous: TimeOfDay, current: TimeOfDay): boolean {
return previous > current;
const backwardJump = previous - current;
return backwardJump > 12 * MILLIS_PER_HOUR;
}
/**