1
0
Files
jecnarozvrh/scheduleRules.js
2025-12-20 20:52:08 +01:00

52 lines
1.6 KiB
JavaScript

/*
* 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 const scheduleRules = [
{ 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) {
const [h, m] = timeStr.split(":").map(Number);
return h * 60 + (m || 0);
}
export function getCurrentInterval(date = new Date()) {
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;
}