-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
117 lines (95 loc) · 3.18 KB
/
script.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
let size = 86;
let columns = Array.from(document.getElementsByClassName('column'));
let d, c;
let classList = ['visible', 'close', 'far', 'far', 'distant', 'distant'];
let use24HourClock = true;
const toggleSeconds = document.getElementById("toggleSeconds");
const seconds = document.getElementById("seconds");
toggleSeconds.addEventListener("click", () => {
if (seconds.style.display === "none") {
seconds.style.display = "inline";
} else {
seconds.style.display = "none";
}
});
const rotateButton = document.getElementById("rotate");
const clock = document.getElementById("clock");
let rotation = 0;
rotateButton.addEventListener("click", () => {
rotation += 90; // Increment rotation by 90 degrees
clock.style.transform = `rotate(${rotation}deg)`;
});
const toggle12h24h = document.getElementById("toggle12h");
toggle12h24h.addEventListener("click", () => {
use24HourClock = !use24HourClock;
//change the 12h text of the button to bold if its 12h or change the 24h text of the button to bold if its 24h
toggle12h24h.innerHTML = use24HourClock ? "Toggle 24h" : "Toggle 12h";
})
function padClock(p, n) {
return p + ('0' + n).slice(-2);
}
async function getCurrentTimezoneByIp() {
const api = "https://worldtimeapi.org/api/ip";
let timezone;
try {
const response = await fetch(api);
if (!response.ok) {
throw new Error(`API request failed with status ${response.status}`);
}
const jsonData = await response.json();
timezone = jsonData.timezone;
} catch (error) {
console.error("Error fetching time:", error);
}
return timezone;
}
function changeTimezone(date, ianatz) {
// suppose the date is 12:00 UTC
var invdate = new Date(date.toLocaleString('en-US', {
timeZone: ianatz
}));
// then invdate will be 07:00 in Toronto
// and the diff is 5 hours
var diff = date.getTime() - invdate.getTime();
// so 12:00 in Toronto is 17:00 UTC
return new Date(date.getTime() - diff); // needs to substract
}
function getClock(timezone, use24HourClock) {
d = new Date();
d = changeTimezone(d, timezone)
return [
use24HourClock ? d.getHours() : d.getHours() % 12 || 12,
d.getMinutes(),
d.getSeconds()].
reduce(padClock, '');
}
function getClass(n, i2) {
return classList.find((class_, classIndex) => Math.abs(n - i2) === classIndex) || '';
}
getCurrentTimezoneByIp()
.then((timezone) => {
c = getClock(timezone, use24HourClock);
columns.forEach((ele, i) => {
let n = +c[i];
let offset = -n * size;
ele.style.transform = `translateY(calc(50vh + ${offset}px - ${size / 2}px))`;
Array.from(ele.children).forEach((ele2, i2) => {
ele2.className = 'num ' + getClass(n, i2);
});
});
});
let loop = setInterval(() => {
getCurrentTimezoneByIp()
.then((timezone) => {
c = getClock(timezone, use24HourClock);
columns.forEach((ele, i) => {
let n = +c[i];
let offset = -n * size;
ele.style.transform = `translateY(calc(50vh + ${offset}px - ${size / 2}px))`;
Array.from(ele.children).forEach((ele2, i2) => {
ele2.className = 'num ' + getClass(n, i2);
});
});
});
}, 2 * 1000);
// 200 + Math.E * 10 for microseconds