Added support for timezone.
This commit is contained in:
@@ -36,6 +36,7 @@
|
|||||||
"disableSearchBar": false,
|
"disableSearchBar": false,
|
||||||
"disable24Hour": false,
|
"disable24Hour": false,
|
||||||
"disableWeather": true,
|
"disableWeather": true,
|
||||||
|
"timeZone": "America/Los_Angeles",
|
||||||
"weatherConf": {
|
"weatherConf": {
|
||||||
"location": "Pune India",
|
"location": "Pune India",
|
||||||
"unit": "cel"
|
"unit": "cel"
|
||||||
|
|||||||
57
js/main.js
57
js/main.js
@@ -10,6 +10,7 @@ dateId = "date-text"
|
|||||||
weatherId = "weather-text"
|
weatherId = "weather-text"
|
||||||
lineId = "line"
|
lineId = "line"
|
||||||
messageId = "message-text"
|
messageId = "message-text"
|
||||||
|
timeZ = undefined
|
||||||
otherContentId = "other-content"
|
otherContentId = "other-content"
|
||||||
userName = ""
|
userName = ""
|
||||||
disable24Hour = false;
|
disable24Hour = false;
|
||||||
@@ -113,7 +114,15 @@ function updateTime() {
|
|||||||
* Get the current time and date and return it.
|
* Get the current time and date and return it.
|
||||||
*/
|
*/
|
||||||
currentDate = new Date()
|
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
|
document.getElementById(dateId).textContent = finalDate
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -124,16 +133,6 @@ function updateTimeHook() {
|
|||||||
}, 30 * 1000)
|
}, 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) {
|
function updateWeather(weatherConfig) {
|
||||||
/**
|
/**
|
||||||
* Get the weather using the location passed by the user using
|
* 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) {
|
function readJSON(fileName) {
|
||||||
// Load the data of the passed file.
|
// Load the data of the passed file.
|
||||||
fetch(fileName)
|
fetch(fileName)
|
||||||
@@ -191,6 +186,8 @@ function parseAndCreate(jsonData) {
|
|||||||
document.getElementById(messageId).textContent = builtMsg
|
document.getElementById(messageId).textContent = builtMsg
|
||||||
// Check if 24 hour is disabled
|
// Check if 24 hour is disabled
|
||||||
disable24Hour = jsonData["disable24Hour"]
|
disable24Hour = jsonData["disable24Hour"]
|
||||||
|
timeZ = jsonData["timeZone"]
|
||||||
|
timeZ = isValidTimeZone(timeZ) ? timeZ : undefined
|
||||||
// Check if welcome message is supposed to be disabled
|
// Check if welcome message is supposed to be disabled
|
||||||
if (jsonData["disableMessage"])
|
if (jsonData["disableMessage"])
|
||||||
document.getElementById(messageDivId).style.display = "none"
|
document.getElementById(messageDivId).style.display = "none"
|
||||||
@@ -258,4 +255,34 @@ function createSqr(sqrData, index) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
return div
|
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)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user