mirror of
https://github.com/cpvalente/ontime.git
synced 2026-08-14 03:43:50 +00:00
fix: roll loads events that cross midnight
This commit is contained in:
committed by
Carlos Valente
parent
358ad79ae4
commit
155cf48a17
@@ -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);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user