-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclock.js
160 lines (127 loc) · 4.86 KB
/
clock.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
const clock = document.querySelector('#clock');
const weekday = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
];
function updateClock() {
// const now = new Date(2024, 8, 2, 12, 45, 0);
const now = new Date();
if (now.getDay() === 0 || now.getDay() === 6) {
clock.style.display = 'none';
return;
} else {
clock.style.display = 'block';
}
const timeStr = `${now.getHours()}:${now
.getMinutes()
.toString()
.padStart(2, '0')}`;
let ampm = '';
if (!UserSettings.use24h) {
ampm = now.getHours() < 12 ? 'AM' : 'PM';
}
clock.querySelector('#clock-time').innerText = `${
weekday[now.getDay()]
}, ${convertTime(timeStr, UserSettings.use24h)}${ampm}`;
const daySched = Schedule[now.getDay() - 1].blocks;
const nowMinute = timeStringToMinute(timeStr);
const dayStart = timeStringToMinute(daySched[0].startTime);
const dayEnd = timeStringToMinute(daySched[daySched.length - 1].endTime);
if (nowMinute < dayStart || nowMinute > dayEnd) {
clock.style.display = 'none';
return;
} else {
clock.style.display = 'block';
}
const currentBlock = daySched.find((block) => {
const startTime = timeStringToMinute(block.startTime);
const endTime = timeStringToMinute(block.endTime);
return startTime <= nowMinute && endTime > nowMinute;
});
// Find next block
let nextBlock = undefined;
for (let i = 0; i < daySched.length; i++) {
const block = daySched[i];
const startTime = timeStringToMinute(block.startTime);
if (startTime > nowMinute) {
nextBlock = block;
break;
}
}
if (currentBlock) {
clock.querySelector('#clock-block').innerText =
formatBlockName(currentBlock);
clock.querySelector('#clock-block-time').innerText = `(${convertTime(
currentBlock.startTime,
UserSettings.use24h,
)}-${convertTime(currentBlock.endTime, UserSettings.use24h)})`;
const minDiff = timeStringToMinute(currentBlock.endTime) - nowMinute;
const minutesWord = minDiff === 1 ? 'minute' : 'minutes';
clock.querySelector(
'#clock-block-remaining',
).innerText = `${minDiff} ${minutesWord} remaining in block`;
} else {
clock.querySelector('#clock-block-time').innerText = '';
if (nextBlock) {
clock.querySelector('#clock-block').innerText = 'Passing Time';
const minDiff = timeStringToMinute(nextBlock.startTime) - nowMinute;
const minutesWord = minDiff === 1 ? 'minute' : 'minutes';
clock.querySelector(
'#clock-block-remaining',
).innerText = `${minDiff} ${minutesWord} until ${formatBlockName(
nextBlock,
)}`;
}
}
// Lunches
if (currentBlock === undefined || currentBlock.lunch.length === 0) {
clock.querySelector('#clock-lunch').style.display = 'none';
return;
} else {
clock.querySelector('#clock-lunch').style.display = 'block';
}
const activeLunchBlock = currentBlock.lunch.find((block) => {
const startTime = timeStringToMinute(block.startTime);
const endTime = timeStringToMinute(block.endTime);
return startTime <= nowMinute && endTime > nowMinute;
});
if (activeLunchBlock) {
clock.querySelector(
'#clock-lunch-name',
).innerText = `${activeLunchBlock.name} Lunch`;
clock.querySelector('#clock-lunch-time').innerText = `(${convertTime(
activeLunchBlock.startTime,
UserSettings.use24h,
)}-${convertTime(activeLunchBlock.endTime, UserSettings.use24h)})`;
const minDiff =
timeStringToMinute(activeLunchBlock.endTime) - nowMinute;
const minutesWord = minDiff === 1 ? 'minute' : 'minutes';
clock.querySelector(
'#clock-lunch-remaining',
).innerText = `${minDiff} ${minutesWord} remaining in lunch`;
} else {
const nextLunchBlock = currentBlock.lunch.find((block) => {
const startTime = timeStringToMinute(block.startTime);
return startTime > nowMinute;
});
clock.querySelector('#clock-lunch-time').innerText = '';
if (nextLunchBlock) {
clock.querySelector(
'#clock-lunch-name',
).innerText = `Between lunches`;
const minDiff =
timeStringToMinute(nextLunchBlock.startTime) - nowMinute;
const minutesWord = minDiff === 1 ? 'minute' : 'minutes';
clock.querySelector(
'#clock-lunch-remaining',
).innerText = `${minDiff} ${minutesWord} until ${nextLunchBlock.name} Lunch`;
}
}
}
updateClock();
setInterval(updateClock, 15 * 1000);