fix: roll loads events that cross midnight

This commit is contained in:
Carlos Valente
2026-03-03 00:44:03 +01:00
committed by Carlos Valente
parent 358ad79ae4
commit 155cf48a17
4 changed files with 82 additions and 14 deletions
@@ -24,6 +24,18 @@ describe('checkIsNow()', () => {
});
test('should return true accounting for events that roll over midnight', () => {
expect(checkIsNow(22 * MILLIS_PER_HOUR, 8 * MILLIS_PER_HOUR, 23 * MILLIS_PER_HOUR)).toBe(true);
const timeStart = 22 * MILLIS_PER_HOUR;
const timeEnd = 8 * MILLIS_PER_HOUR;
const now = 23 * MILLIS_PER_HOUR;
expect(checkIsNow(timeStart, timeEnd, now)).toBe(true);
});
test('should return true accounting for events that roll over midnight (2)', () => {
const timeStart = 22 * MILLIS_PER_HOUR;
const timeEnd = 8 * MILLIS_PER_HOUR;
const now = 1 * MILLIS_PER_HOUR;
expect(checkIsNow(timeStart, timeEnd, now)).toBe(true);
});
});
+5 -4
View File
@@ -1,9 +1,10 @@
import { dayInMs } from './conversionUtils.js';
/**
* Utility function checks whether a given event should be playing now
*/
export function checkIsNow(timeStart: number, timeEnd: number, clock: number): boolean {
const normalisedEnd = timeEnd < timeStart ? timeEnd + dayInMs : timeEnd;
return timeStart <= clock && clock <= normalisedEnd;
if (timeEnd < timeStart) {
// overnight event: clock is either after start OR before end
return clock >= timeStart || clock <= timeEnd;
}
return timeStart <= clock && clock <= timeEnd;
}