summaryrefslogtreecommitdiff
path: root/tempo.js
blob: 7b471ae795a65ae2a70951def633b29ab6306823 (plain) (blame)
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
let timefmt = null;

const MONTHS = Array.from({ length: 12 }, (_, i) =>
	new Date(0, i).toLocaleString(undefined, { month: 'long' }));

function padTime(x) {
	return (x < 10) ? '0' + x : x;
}

function updateTime() {
	let date = new Date();

	let h = date.getHours();
	let m = date.getMinutes();
	let s = date.getSeconds();

	let dd = date.getDate();
	let dm = date.getMonth();
	let dy = date.getFullYear();

	if (timefmt == 12) // default to 24
		h = h % 12 || 12;

	document.getElementById('time').textContent =
		padTime(h) + '\u00A0\u00A0' +
		padTime(m) + '\u00A0\u00A0' +
		padTime(s);
	document.getElementById('date').textContent =
		dd + ' ' + MONTHS[dm] + ' ' + dy;
}

// recursively calling setTimeout instead of using setInterval allows us to
// correct for any drift caused by imprecise delays.
function updateTimeRecursive() {
	updateTime();
	// +1ms to avoid updating too early
	setTimeout(updateTimeRecursive, 1001 - new Date().getTime() % 1000);
}

function startTempo() {
	timefmt = localStorage['timefmt'];

	let val;
	if ((val = localStorage['fg']))
		document.body.style.color = val;
	if ((val = localStorage['fg_date']))
		document.getElementById('date').style.color = val;
	if ((val = localStorage['bg']))
		document.body.style.backgroundColor = val;

	updateTimeRecursive();
}

window.onload = startTempo;

document.addEventListener('contextmenu', e => e.preventDefault());