feat: Custom schedule rules
All checks were successful
Remote Deploy / deploy (push) Successful in 4s
All checks were successful
Remote Deploy / deploy (push) Successful in 4s
This commit is contained in:
43
scheduleRules.js
Normal file
43
scheduleRules.js
Normal file
@@ -0,0 +1,43 @@
|
||||
// Rules: start and end in 24h format, interval in minutes
|
||||
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 }
|
||||
];
|
||||
|
||||
function toMinutes(timeStr) {
|
||||
const [h, m] = timeStr.split(":").map(Number);
|
||||
return h * 60 + (m || 0);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
scheduleRules,
|
||||
getCurrentInterval,
|
||||
toMinutes
|
||||
};
|
||||
Reference in New Issue
Block a user