-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
192 lines (152 loc) · 6.29 KB
/
script.js
File metadata and controls
192 lines (152 loc) · 6.29 KB
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
/* variables */
let darkMode = localStorage.getItem("darkMode");
let themeButton = document.getElementById("themeButton");
let removeMarkersButton = document.getElementById("removeMarkersButton");
let liveClockUTC = document.getElementById("clockDisplay");
let ESTDisplay = document.getElementById("ESTDisplay");
let EDTDisplay = document.getElementById("EDTDisplay");
let PSTDisplay = document.getElementById("PSTDisplay");
let PDTDisplay = document.getElementById("PDTDisplay");
let GMTDisplay = document.getElementById("GMTDisplay");
let MSTDisplay = document.getElementById("MSTDisplay");
let MDTDisplay = document.getElementById("MDTDisplay");
let BRTDisplay = document.getElementById("BRTDisplay");
let CETDisplay = document.getElementById("CETDisplay");
let CESTDisplay = document.getElementById("CESTDisplay");
let AESTDisplay = document.getElementById("AESTDisplay");
let AEDTDisplay = document.getElementById("AEDTDisplay");
let BSTDisplay = document.getElementById("BSTDisplay");
let ISTDisplay = document.getElementById("ISTDisplay");
let JSTDisplay = document.getElementById("JSTDisplay");
let CSTDisplay = document.getElementById("CSTDisplay");
/* logic */
/* map */
const map = L.map('iMap').setView([0, 0], 2);
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
maxZoom: 18,
}).addTo(map);
const markers = [];
function updateMarkerTime(marker) {
if (!marker.timeZone) {
marker.setPopupContent("Time zone not set.");
return;
}
const now = new Date();
const timeString = new Intl.DateTimeFormat('en-US', {
timeZone: marker.timeZone,
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
hour12: false
}).format(now);
marker.setPopupContent(`
<strong>Time Zone:</strong> ${marker.timeZone}<br>
<strong>Local Time:</strong> ${timeString}
`);
}
// Map click: Add marker and store timeZone
map.on('click', function(e) {
const { lat, lng } = e.latlng;
const marker = L.marker([lat, lng]).addTo(map);
marker.bindPopup("Loading...").openPopup();
// Get time zone (this must work!)
try {
const timeZone = tzlookup(lat, lng);
marker.timeZone = timeZone;
} catch (err) {
marker.setPopupContent("Couldn't get time zone.");
return;
}
updateMarkerTime(marker); // initial display
markers.push(marker);
});
// Update every second
setInterval(() => {
markers.forEach(updateMarkerTime);
}, 1000);
removeMarkersButton.addEventListener("click", () => {
markers.forEach(i => map.removeLayer(i));
markers.length = 0;
});
/* functions */
function add0To1Digit(number) {
if (number.toString().length < 2) {
return `0${number}`;
}
else {
return number;
}
}
function keepClockPosition(hour) {
hour = parseInt(hour);
if (hour >= 24) {return hour - 24;}
else if (hour < 0) {return hour + 24;}
else {return hour;}
}
function keepClockPositionMinutes(minute) {
minute = parseInt(minute);
if (minute >= 60) {return minute - 60;}
else if (minute < 0) {return minute + 60;}
else {return minute;}
}
liveClockUTC.textContent = `${add0To1Digit(new Date().getUTCHours())}:${add0To1Digit(new Date().getUTCMinutes())}:${add0To1Digit(new Date().getUTCSeconds())}`;
/* main function */
function updateClock() {
/* variables */
const timeZone = new Date();
const UTChours = add0To1Digit(timeZone.getUTCHours());
const ESThours = keepClockPosition(timeZone.getUTCHours() - 5);
const EDThours = keepClockPosition(timeZone.getUTCHours() - 4);
const PSThours = keepClockPosition(timeZone.getUTCHours() - 8);
const PDThours = keepClockPosition(timeZone.getUTCHours() - 7);
const MDThours = keepClockPosition(timeZone.getUTCHours() - 6);
const BRThours = keepClockPosition(timeZone.getUTCHours() - 3);
const CEThours = keepClockPosition(timeZone.getUTCHours() + 1);
const CESThours = keepClockPosition(timeZone.getUTCHours() + 2);
const AESThours = keepClockPosition(timeZone.getUTCHours() + 10);
const AEDThours = keepClockPosition(timeZone.getUTCHours() + 11);
const ISThours = keepClockPosition(timeZone.getUTCHours() + 5)
const JSThours = keepClockPosition(timeZone.getUTCHours() + 9)
const CSThours = keepClockPosition(timeZone.getUTCHours() + 8)
const ISTMinutes = keepClockPositionMinutes(timeZone.getUTCMinutes() + 30);
const minutes = add0To1Digit(timeZone.getUTCMinutes());
const seconds = add0To1Digit(timeZone.getUTCSeconds());
/* updating text content */
liveClockUTC.textContent = `${UTChours}:${minutes}:${seconds}`;
ESTDisplay.textContent = `${add0To1Digit(ESThours)}:${minutes}`;
EDTDisplay.textContent = `${add0To1Digit(EDThours)}:${minutes}`;
PSTDisplay.textContent = `${add0To1Digit(PSThours)}:${minutes}`;
PDTDisplay.textContent = `${add0To1Digit(PDThours)}:${minutes}`;
GMTDisplay.textContent = `${UTChours}:${minutes}`;
MSTDisplay.textContent = `${add0To1Digit(PDThours)}:${minutes}`;
MDTDisplay.textContent = `${add0To1Digit(MDThours)}:${minutes}`;
BRTDisplay.textContent = `${add0To1Digit(BRThours)}:${minutes}`;
CETDisplay.textContent = `${add0To1Digit(CEThours)}:${minutes}`;
CESTDisplay.textContent = `${add0To1Digit(CESThours)}:${minutes}`;
AESTDisplay.textContent = `${add0To1Digit(AESThours)}:${minutes}`;
AEDTDisplay.textContent = `${add0To1Digit(AEDThours)}:${minutes}`;
BSTDisplay.textContent = `${add0To1Digit(CEThours)}:${minutes}`;
ISTDisplay.textContent = `${add0To1Digit(ISThours)}:${add0To1Digit(ISTMinutes)}`;
JSTDisplay.textContent = `${add0To1Digit(JSThours)}:${minutes}`;
CSTDisplay.textContent = `${add0To1Digit(CSThours)}:${minutes}`;
}
function enableDarkmode() {
document.body.classList.add("darkMode");
localStorage.setItem("darkMode", "active");
}
function disableDarkmode() {
document.body.classList.remove("darkMode");
localStorage.setItem("darkMode", null);
}
themeButton.addEventListener("click", () => {
darkMode = localStorage.getItem("darkMode");
darkMode !== "active" ? enableDarkmode() : disableDarkmode()
});
if (darkMode === "active") {enableDarkmode();}
setInterval(() => {
setInterval(updateClock(), 1000);
updateClock();
});
setInterval(() => {
markers.forEach(marker => updateMarkerTime(marker));
}, 1000);