-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
332 lines (289 loc) · 10.3 KB
/
main.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import "./style.css";
import "./clock.css";
import "toastify-js/src/toastify.css"; // Import the CSS file
import Toastify from "toastify-js";
import moment from "moment-timezone";
import { clock } from "./clockComponent";
import { timeToDegreeValue } from "./time";
import { loadCities } from "./cities";
// Load cities from local storage
let cities = loadCities();
// Function to save cities to local storage
function saveCities() {
localStorage.setItem("cities", JSON.stringify(cities));
}
saveCities();
// Function to save settings to local storage
const saveSettingsToLocalStorage = (
startTime,
endTime,
timezoneValue,
timezoneLocalString
) => {
localStorage.setItem("startTime", startTime);
localStorage.setItem("endTime", endTime);
localStorage.setItem("timezoneValue", timezoneValue);
localStorage.setItem("timezoneLocalString", timezoneLocalString);
};
// Function to fetch settings from local storage with default values
const fetchSettingsFromLocalStorage = () => ({
startTime: localStorage.getItem("startTime") || "09:00",
endTime: localStorage.getItem("endTime") || "17:00",
timezoneValue: localStorage.getItem("timezoneValue") || "5.5",
timezoneLocalString:
localStorage.getItem("timezoneLocalString") || "Asia/Kolkata",
});
// Function to initialize time inputs and timezone from local storage
const initializeSettings = () => {
const { startTime, endTime, timezoneValue, timezoneLocalString } =
fetchSettingsFromLocalStorage();
startTimeInput.value = startTime;
endTimeInput.value = endTime;
selectedTimezone.value = timezoneValue;
// Set the correct option for timezone based on timezoneLocalString
Array.from(selectedTimezone.options).forEach((option) => {
if (
option.getAttribute("data-timezoneLocalString") === timezoneLocalString
) {
option.selected = true;
}
});
};
// Function to handle time input event
const handleTimeInput = () => {
const startTime = startTimeInput.value;
const endTime = endTimeInput.value;
const selectedOption =
selectedTimezone.options[selectedTimezone.selectedIndex];
const timezoneValue = selectedOption.value;
const timezoneLocalString = getSelectedTimeZone(
"timezone",
"data-timezoneLocalString"
); // Use getSelectedTimeZone to get timezoneLocalString
saveSettingsToLocalStorage(
startTime,
endTime,
timezoneValue,
timezoneLocalString
);
convertTime(); // Call convertTime to update converted time
};
// Function to get the selected timezone's data-timezoneLocalString attribute
function getSelectedTimeZone(id, dataId) {
const timezoneSelect = document.getElementById(id);
const selectedOption = timezoneSelect.options[timezoneSelect.selectedIndex];
const timeZoneLocalString = selectedOption.getAttribute(dataId);
return timeZoneLocalString;
}
function convertTime() {
const { startTime, endTime } = fetchSettingsFromLocalStorage();
const myTimezone = getSelectedTimeZone(
"timezone",
"data-timezoneLocalString"
);
let cityName =
document.getElementById("timezone").options[
document.getElementById("timezone").selectedIndex
].text;
let result = ``;
let resultDiv = `
<div class="resultText">
To Trade or Work in the Cities or Markets listed below, here are your Trading or Working hours in ${cityName}, based on Your or Your Company's chosen Trading or Working hours: (${startTime} to ${endTime}).
These times are DST-adjusted where applicable.
<div class="github">
<a
href="https://github.com/s54a/GlobalTimeConvertor"
target="_blank"
rel="noopener noreferrer"
>GitHub</a
>
</div>
</div>
`;
let resultStartTime, resultEndTime, startDeg, endDeg;
cities.forEach((city) => {
let cityStartTime, cityEndTime;
if (city.isMarket) {
cityStartTime = city.openTime;
cityEndTime = city.closeTime;
} else {
cityStartTime = startTime;
cityEndTime = endTime;
}
resultStartTime = convertTimeToCity(
myTimezone,
cityStartTime,
city.timeZoneString
);
resultEndTime = convertTimeToCity(
myTimezone,
cityEndTime,
city.timeZoneString
);
startDeg = timeToDegreeValue(resultStartTime);
endDeg = timeToDegreeValue(resultEndTime);
let gradient;
if (startDeg > 243.75) {
gradient = `background: conic-gradient(red 0deg ${endDeg}deg,#ddd ${endDeg}deg ${startDeg}deg,red ${startDeg}deg 0deg);`;
} else {
gradient = `background: conic-gradient(#ddd 0deg ${startDeg}deg,red ${startDeg}deg ${endDeg}deg,#ddd ${startDeg}deg ${endDeg}deg);`;
}
const cityNameWithoutSpaces = city.name.replace(/\s+/g, "-");
result += `
<div class="resultTime">
${clock(gradient)}
<div class="city">${city.name}</div>
<div class="cityTime">${resultStartTime} - ${resultEndTime}</div>
<div class="currentCityTime city" id="current-time-${city.name.replace(
/\s+/g,
"-"
)}">${getCurrentTime(city.timeZoneString)}</div>
<div class="city timeZoneString">${city.timeZoneString} UTC ${
city.offset
}</div>
<button class="removeBtn" data-city-name="${cityNameWithoutSpaces}">Remove</button>
</div>
`;
// <button class="removeBtn" id="current-city-${cityNameWithoutSpaces}" onclick="removeCity('current-city-${cityNameWithoutSpaces}')">Remove</button>
});
let resultHTML = `
${resultDiv}
<div class="clockDiv">${result}</div>
`;
document.getElementById("result").innerHTML = resultHTML;
// Attach event listeners for remove buttons
document.querySelectorAll(".removeBtn").forEach((button) => {
button.addEventListener("click", function () {
const cityNameWithoutSpaces = this.getAttribute("data-city-name");
removeCity(cityNameWithoutSpaces);
});
});
function removeCity(cityNameWithoutSpaces) {
cities = cities.filter(
(city) => city.name.replace(/\s+/g, "-") !== cityNameWithoutSpaces
);
saveCities();
convertTime();
Toastify({
text: `City removed successfully!`,
backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
duration: 3000,
}).showToast();
}
}
function convertTimeToCity(myTimezone, time, targetTimeZoneString) {
let localTime = moment.tz(time, "HH:mm", myTimezone);
let convertedTime = localTime.clone().tz(targetTimeZoneString);
return convertedTime.format("HH:mm");
}
// Function to add a new city
function addCity() {
const cityName = document
.getElementById("city-name")
.value.trim()
.toLowerCase(); // Convert to lowercase
const capitalizedCityName =
cityName.charAt(0).toUpperCase() + cityName.slice(1); // Capitalize the first letter
const cityOffset = parseFloat(document.getElementById("city-offset").value);
// Check if city name and offset are provided
if (capitalizedCityName && !isNaN(cityOffset)) {
// Check if the city already exists
if (
cities.some(
(city) => city.name.toLowerCase() === capitalizedCityName.toLowerCase()
)
) {
// Show toast message for existing city
Toastify({
text: `City "${capitalizedCityName}" already exists!`,
backgroundColor: "linear-gradient(to right, #ff416c, #ff4b2b)",
duration: 3000,
}).showToast();
} else {
const myTimezone = getSelectedTimeZone(
"city-offset",
"data-timezoneLocalStringForAddCity"
);
// Add the new city if it doesn't already exist
cities.unshift({
name: capitalizedCityName,
offset: cityOffset,
timeZoneString: myTimezone,
}); // Add to the beginning of the array
saveCities(); // Save updated cities to local storage
// Show toast message for successful addition
Toastify({
text: `City "${capitalizedCityName}" added successfully!`,
backgroundColor: "linear-gradient(to right, #00b09b, #96c93d)",
duration: 3000,
}).showToast();
}
} else {
// Show toast message for invalid input
Toastify({
text: "Please provide a valid city name and UTC offset.",
backgroundColor: "linear-gradient(to right, #ff416c, #ff4b2b)",
duration: 3000,
}).showToast();
}
// Clear input fields
document.getElementById("city-name").value = "";
handleTimeInput();
}
function getCurrentTime(timezone) {
let now = new Date();
return now.toLocaleString("en-US", {
timeZone: timezone,
hour12: false,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
});
}
// Update the current time of each city every second
setInterval(() => {
cities.forEach((city) => {
const currentTimeElement = document.getElementById(
`current-time-${city.name.replace(/\s+/g, "-")}`
);
if (currentTimeElement) {
currentTimeElement.textContent = getCurrentTime(city.timeZoneString);
}
});
}, 1000);
convertTime();
// References to HTML elements
const addCityBtn = document.querySelector("#addCity");
const startTimeInput = document.getElementById("start-time");
const endTimeInput = document.getElementById("end-time");
const selectedTimezone = document.getElementById("timezone");
const cityNameInput = document.getElementById("city-name");
// Add event listeners for time inputs and timezone change
startTimeInput.addEventListener("input", handleTimeInput);
endTimeInput.addEventListener("input", handleTimeInput);
addCityBtn.addEventListener("click", addCity);
selectedTimezone.addEventListener("change", handleTimeInput);
// Add event listener for Enter key press on city name input
cityNameInput.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
addCity(); // Call addCity function when Enter is pressed
}
});
// Initialize settings on page load
initializeSettings();
document.addEventListener("DOMContentLoaded", initializeSettings);
/* -- Glow effect -- */
// const blob = document.querySelector("#blob");
// window.onpointermove = (event) => {
// const { clientX, clientY } = event;
// blob.animate(
// {
// left: `${clientX}px`,
// top: `${clientY}px`,
// },
// { duration: 3000, fill: "forwards" }
// );
// };
// Extra resources
// https://www.worldtimebuddy.com/?pl=1&lid=0,5391959,5128581,2643743,292223,30,1880252,1850147,2147714,2193733&h=0&hf=1
// https://www.worldtimebuddy.com/?pl=1&lid=5391959,5128581,0,2643743,292223,30,1880252,1850147,2147714,2193733&h=0&hf=1