forked from Nandika-A/EyeProtection
-
Notifications
You must be signed in to change notification settings - Fork 6
/
schedule.js
41 lines (35 loc) · 1.15 KB
/
schedule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// function to save the schedule
function saveSchedule(startTime, endTime) {
return new Promise((resolve) => {
chrome.storage.local.set({ startTime, endTime }, () => {
resolve({ startTime, endTime });
});
});
}
// function to get the schedule
function getSchedule() {
return new Promise((resolve) => {
chrome.storage.local.get(['startTime', 'endTime'], (result) => {
resolve(result);
});
});
}
// function to check if the current time is within the schedule
function isTimeInSchedule(currentTime, schedule) {
const { startTime, endTime } = schedule;
if (!startTime || !endTime) return false;
const current = new Date(`2000-01-01T${currentTime}`);
const start = new Date(`2000-01-01T${startTime}`);
let end = new Date(`2000-01-01T${endTime}`);
// handle case where end time is on the next day
if (end < start) {
end = new Date(`2000-01-02T${endTime}`);
if (current < start) current.setDate(current.getDate() + 1);
}
return current >= start && current <= end;
}
module.exports = {
saveSchedule,
getSchedule,
isTimeInSchedule,
};