Added support for timezone.

This commit is contained in:
deepjyoti30
2020-05-20 16:35:41 +05:30
parent 929d3aab9b
commit ffe3e6da5c
2 changed files with 43 additions and 15 deletions

View File

@@ -36,6 +36,7 @@
"disableSearchBar": false,
"disable24Hour": false,
"disableWeather": true,
"timeZone": "America/Los_Angeles",
"weatherConf": {
"location": "Pune India",
"unit": "cel"

View File

@@ -10,6 +10,7 @@ dateId = "date-text"
weatherId = "weather-text"
lineId = "line"
messageId = "message-text"
timeZ = undefined
otherContentId = "other-content"
userName = ""
disable24Hour = false;
@@ -113,7 +114,15 @@ function updateTime() {
* Get the current time and date and return it.
*/
currentDate = new Date()
finalDate = currentDate.toLocaleString(undefined, {day:'numeric', month:'short', hour:'numeric', minute:'numeric',hour12:disable24Hour})
options = {
day: 'numeric',
month: 'short',
hour: 'numeric',
minute: 'numeric',
hour12: disable24Hour,
timeZone: timeZ
}
finalDate = currentDate.toLocaleString(undefined, options)
document.getElementById(dateId).textContent = finalDate
}
@@ -124,16 +133,6 @@ function updateTimeHook() {
}, 30 * 1000)
}
function getFahrenheit(inCelcius) {
return Math.floor((inCelcius * 9 / 5) + 32)
}
function indexUppercase(unformatted) {
return unformatted.split(" ").map(w => {
return w[0].toUpperCase() + w.substring(1)
}).join(" ")
}
function updateWeather(weatherConfig) {
/**
* Get the weather using the location passed by the user using
@@ -160,10 +159,6 @@ function updateWeather(weatherConfig) {
})
}
function inRange(number, min, max) {
return (number >= min && number <= max)
}
function readJSON(fileName) {
// Load the data of the passed file.
fetch(fileName)
@@ -191,6 +186,8 @@ function parseAndCreate(jsonData) {
document.getElementById(messageId).textContent = builtMsg
// Check if 24 hour is disabled
disable24Hour = jsonData["disable24Hour"]
timeZ = jsonData["timeZone"]
timeZ = isValidTimeZone(timeZ) ? timeZ : undefined
// Check if welcome message is supposed to be disabled
if (jsonData["disableMessage"])
document.getElementById(messageDivId).style.display = "none"
@@ -258,4 +255,34 @@ function createSqr(sqrData, index) {
})
return div
}
// Utility functions
function isValidTimeZone(tz) {
if (!Intl || !Intl.DateTimeFormat().resolvedOptions().timeZone) {
throw 'Time zones are not available in this environment';
}
try {
Intl.DateTimeFormat(undefined, {timeZone: tz});
return true;
}
catch (ex) {
return false;
}
}
function getFahrenheit(inCelcius) {
return Math.floor((inCelcius * 9 / 5) + 32)
}
function indexUppercase(unformatted) {
return unformatted.split(" ").map(w => {
return w[0].toUpperCase() + w.substring(1)
}).join(" ")
}
function inRange(number, min, max) {
return (number >= min && number <= max)
}