Skip to content

Commit 791f5dc

Browse files
authoredMar 21, 2025
Replace runAtSpecificTimeOfDay with UTC logic (#44)
* Replace runAtSpecificTimeOfDay with UTC logic * Update restrictedHours to use UTC times * Directly increment next Date to avoid looping
1 parent 7157747 commit 791f5dc

File tree

3 files changed

+43
-39
lines changed

3 files changed

+43
-39
lines changed
 

‎src/config/config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const pauseCloudSceneChange = false;
1717
const announceChatSceneChange = false;
1818
//UTC TIME
1919
const notifyHours = {start:14,end:23};
20-
const restrictedHours = {start:9,end:18};
20+
const restrictedHours = {start:14,end:23};
2121
const globalMusicSource = "Music Playlist Global";
2222

2323
const userRanks = {

‎src/modules/legacy.js

+2-38
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
const UtilsModule = require("../utils/utilsModule");
21
const config = require("../config/config");
32
const helper = require("../utils/helper");
3+
const schedule = require("../utils/schedule");
44
const Logger = require("../utils/logger");
55

66
const logger = new Logger("modules/legacy");
@@ -41,7 +41,7 @@ const main = async controller => {
4141
setPTZRoamMode(controller, currentScene);
4242
}
4343

44-
runAtSpecificTimeOfDay(config.restrictedHours.start - 1, 55, () => {
44+
schedule(config.restrictedHours.start - 1, 55, () => {
4545
try {
4646
let now = new Date();
4747
let minutes = now.getUTCMinutes();
@@ -2780,40 +2780,4 @@ async function setPTZRoamMode(controller, scene) {
27802780
roamTimeout = setTimeout(setPTZRoamMode, length * 1000, controller, scene);
27812781
}
27822782

2783-
// function runAtSpecificTimeOfDay(hour, minutes, func) {
2784-
// const twentyFourHours = 86400000;
2785-
// const now = new Date();
2786-
// let eta_ms = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), hour, minutes, 0, 0).getTime() - now;
2787-
// if (eta_ms < 0) {
2788-
// eta_ms += twentyFourHours;
2789-
// }
2790-
// let nowMin = now.getUTCMinutes();
2791-
// let nowHour = now.getUTCHours();
2792-
// logger.log("check time: ",now,nowHour,nowMin,config);
2793-
// logger.log("setup !livecam Timer for: ",eta_ms);
2794-
// setTimeout(function () {
2795-
// //run once
2796-
// func();
2797-
// // run every 24 hours from now on
2798-
// setInterval(func, twentyFourHours);
2799-
// }, eta_ms);
2800-
// }
2801-
2802-
function runAtSpecificTimeOfDay(hour, minutes, func) {
2803-
const twentyFourHours = 86400000;
2804-
const now = new Date();
2805-
let next = new Date(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate(), hour, minutes, 0, 0).getTime();
2806-
if (next < Date.now()) {
2807-
next += twentyFourHours;
2808-
}
2809-
2810-
setInterval(function () {
2811-
if (next < Date.now()) {
2812-
func();
2813-
next += twentyFourHours;
2814-
}
2815-
}, 60_000);
2816-
}
2817-
28182783
module.exports = Object.assign(main, { onTwitchMessage });
2819-

‎src/utils/schedule.js

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Run a function at a specific Date
3+
*
4+
* @param {Date} date Date to run at
5+
* @param {Function} callback Function to run
6+
*/
7+
const runAtDate = (date, callback) => {
8+
setTimeout(callback, date - new Date());
9+
};
10+
11+
12+
/**
13+
* Run a function at a specific UTC time
14+
*
15+
* @param {number} hour UTC hour
16+
* @param {number} minute UTC minute
17+
* @param {Function} callback Function to run
18+
* @param {boolean} [loop] Whether to run the function every day at the specified time
19+
*/
20+
const runAtTime = (hour, minute, callback, loop = true) => {
21+
// Determine our initial next run time
22+
const next = new Date();
23+
next.setUTCHours(hour);
24+
next.setUTCMinutes(minute);
25+
next.setUTCSeconds(0);
26+
if (next < new Date()) next.setDate(next.getDate() + 1);
27+
28+
// Invoke the callback and increment the next run time each day
29+
const callbackLoop = () => {
30+
callback();
31+
32+
if (loop) {
33+
next.setDate(next.getDate() + 1);
34+
runAtDate(next, callbackLoop);
35+
}
36+
};
37+
runAtDate(next, callbackLoop);
38+
};
39+
40+
module.exports = runAtTime;

0 commit comments

Comments
 (0)
Please sign in to comment.