-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.html
246 lines (214 loc) · 8.09 KB
/
index.html
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta charset="UTF-8" />
<style>
body {
background-color: black;
color: lightgray;
font-family: 'Lucida Console', 'Courier', monospace;
margin: auto;
max-width: 600px;
min-width: 600px;
}
a {
color: blue;
text-decoration: none;
}
a:visited {
color: blue;
}
a:hover {
color: blue;
text-decoration: underline;
}
</style>
</head>
<body>
<h1 class="words">1,440 Minute Clock</h1>
<p id="datetime"></p>
<pre id="clock"></pre>
<div class="words">
<h2>About</h2>
<p>
This is a minimal clock that (by default) visualizes each of the
precious 1,440 minutes each of us has in a day.
</p>
<p>
This clock can be configured with query parameters. Here are a
few examples. Bookmark your own configuration!
</p>
<ul>
<a href="?">
Default
</a>
</ul>
<ul>
<a href="?sh=6">
Starting time at 6:00
</a>
</ul>
<ul>
<a href="?sh=6&sm=30">
Starting time at 6:30
</a>
</ul>
<ul>
<a href="?sh=7&dh=8">
An 8-hour workday starting at 7:00
</a>
</ul>
<ul>
<a href="?sh=6&dh=15">
The author's waking hours
</a>
</ul>
<p>
See the
<a href="https://github.com/robatron/1440-clock">
source code
</a>
for more details.
</p>
</div>
</body>
<script>
// Symbols to represent a minute, past, present, and future
const MINUTE_SYM = {
PAST: '. ', // '⬛ ',
FUTURE: '# ', // '⬜ ',
PRESENT: 'X ', // '🔲 ',
PRESENT_PROGRESS: ['˺', '˼', '˻', '˹'].map(c => c + ' '),
};
// Gather user settings from the query string.
// - `sh`: Starting hour
// - `sm`: Starting minute
// - `dh`: How many hours to display
const getQuerySetting = key =>
location.search.match(new RegExp(`\\b(${key})=(\\d+)\\b`), 'i');
const startHourQuery = getQuerySetting('sh');
const startMinuteQuery = getQuerySetting('sm');
const displayHoursQuery = getQuerySetting('dh');
const hideWordsQuery = getQuerySetting('hw');
// What time to start the clock display. Defaults to 0
const START_HOUR = startHourQuery ? parseInt(startHourQuery[2]) : 0;
const START_MINUTE = startMinuteQuery
? parseInt(startMinuteQuery[2])
: 0;
// How many columns will be shown. Can be used to show partial days
// (working hours, for example). Defaults to 24.
const HOURS_TO_DISPLAY = displayHoursQuery
? parseInt(displayHoursQuery[2])
: 24;
// How often to update the display in milliseconds
const DISPLAY_UPDATE_INTERVAL = 125;
// Just in case we ever switch to metric time ;-)
const HOURS_IN_DAY = 24;
const MINS_IN_HOUR = 60;
// Hide words immediately if necessary
const HIDE_WORDS = hideWordsQuery ? parseInt(hideWordsQuery[2]) : 0;
HIDE_WORDS &&
document.querySelectorAll('.words').forEach(n => {
n.style.display = 'none';
});
// Prefix targetNum with zeros up to the specified digits
const zeroPad = (targetNum, digits) =>
'0'.repeat(digits - String(targetNum).length) + targetNum;
// Return a formatted date-time string for display
const getFormattedDateTime = () => {
const d = new Date();
const year = d.getFullYear();
const month = zeroPad(d.getMonth() + 1, 2);
const day = zeroPad(d.getDate(), 2);
const hour = zeroPad(d.getHours(), 2);
const min = zeroPad(d.getMinutes(), 2);
const sec = zeroPad(d.getSeconds(), 2);
const ms = zeroPad(d.getMilliseconds(), 3);
const tzhour = Math.floor(d.getTimezoneOffset() / 60);
const tzmin = zeroPad(
Math.abs(tzhour * 60 - d.getTimezoneOffset()),
2,
);
return `${year}-${month}-${day} ${hour}:${min}:${sec}:${ms} (UTC${
tzhour < 0 ? '+' + -tzhour : '-' + tzhour
}:${tzmin})`;
};
// Display the clock matrix. Hours horizontally, minutes vertically.
const printClockMatrix = cm => {
// Hour label padding to make room for minute labels
let clockString = ' ';
// Hour labels
for (let i = 0; i < HOURS_TO_DISPLAY; ++i) {
const shiftedHours = (i + START_HOUR) % HOURS_IN_DAY;
clockString += zeroPad(shiftedHours, 2) + ' ';
}
clockString += '\n';
// Minute rows
for (let mins = 0; mins < MINS_IN_HOUR; ++mins) {
// Minute label
const shiftedMinutes = (mins + START_MINUTE) % MINS_IN_HOUR;
clockString += zeroPad(shiftedMinutes, 2) + ' ';
// Current minute
for (let hours = 0; hours < HOURS_TO_DISPLAY; ++hours) {
clockString += cm[hours][mins];
}
clockString += '\n';
}
return clockString;
};
// Generate a clock matrix for the current date
const generateClockMatrix = date => {
const hours =
(date.getHours() + HOURS_IN_DAY - START_HOUR) % HOURS_IN_DAY;
const mins =
(date.getMinutes() + MINS_IN_HOUR - START_MINUTE) %
MINS_IN_HOUR;
const secs = date.getSeconds();
const ms = date.getTime();
// Clock matrix. row:column -> hour:minute
const clockMatrix = new Array(HOURS_IN_DAY);
for (let h = 0; h < HOURS_IN_DAY; ++h) {
clockMatrix[h] = new Array(MINS_IN_HOUR);
for (let m = 0; m < MINS_IN_HOUR; ++m) {
// Past
if (h < hours || (h === hours && m < mins)) {
clockMatrix[h][m] = MINUTE_SYM.PAST;
}
// Present
else if (h === hours && m === mins) {
clockMatrix[h][m] =
MINUTE_SYM.PRESENT_PROGRESS[
Math.floor(ms / DISPLAY_UPDATE_INTERVAL) %
MINUTE_SYM.PRESENT_PROGRESS.length
];
}
// Future
else if (h > hours || (h === hours && m > mins)) {
clockMatrix[h][m] = MINUTE_SYM.FUTURE;
}
}
}
return clockMatrix;
};
// Update datetime
const updateDatetime = () => {
document.querySelector(
'#datetime',
).innerHTML = getFormattedDateTime();
};
updateDatetime();
// Update matrix clock
const updateClock = () => {
const clockMatrix = generateClockMatrix(new Date());
document.querySelector('#clock').innerHTML = printClockMatrix(
clockMatrix,
);
};
updateClock();
// Update display at a regular interval
setInterval(() => {
updateDatetime();
updateClock();
}, DISPLAY_UPDATE_INTERVAL);
</script>
</html>