1
0
Files
jecnarozvrh/scheduleRules.ts
jzitnik-dev ae17dc241a
All checks were successful
Remote Deploy / deploy (push) Successful in 14s
refactor: Rewrite to typescript
2026-02-11 08:20:56 +01:00

58 lines
1.8 KiB
TypeScript

/*
* Copyright (C) 2025 Jakub Žitník
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
// Rules: start and end in 24h format, interval in minutes
export interface ScheduleRule {
start: string;
end: string;
interval: number;
}
export const scheduleRules: ScheduleRule[] = [
{ start: "0:00", end: "3:00", interval: 180 },
{ start: "3:00", end: "4:00", interval: 60 },
{ start: "5:00", end: "6:00", interval: 30 },
{ start: "6:00", end: "7:30", interval: 15 },
{ start: "7:30", end: "16:00", interval: 5 },
{ start: "16:00", end: "19:00", interval: 60 },
{ start: "19:00", end: "0:00", interval: 180 }
];
export function toMinutes(timeStr: string): number {
const [h, m] = timeStr.split(":").map(Number);
return h * 60 + (m || 0);
}
export function getCurrentInterval(date: Date = new Date()): number | null {
const nowMinutes = date.getHours() * 60 + date.getMinutes();
for (const rule of scheduleRules) {
let startMin = toMinutes(rule.start);
let endMin = toMinutes(rule.end === "0:00" ? "24:00" : rule.end);
// Handle wrap-around midnight
if (endMin <= startMin) {
if (nowMinutes >= startMin || nowMinutes < endMin) {
return rule.interval;
}
} else {
if (nowMinutes >= startMin && nowMinutes < endMin) {
return rule.interval;
}
}
}
return null;
}