Fix #3420 timezone issue (#3425)

pull/3428/head
Louis Lam 1 year ago committed by GitHub
parent 357466cc90
commit 21d556528f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -262,13 +262,43 @@ class UptimeKumaServer {
* @returns {Promise<string>} * @returns {Promise<string>}
*/ */
async getTimezone() { async getTimezone() {
// From process.env.TZ
try {
if (process.env.TZ) {
this.checkTimezone(process.env.TZ);
return process.env.TZ;
}
} catch (e) {
log.warn("timezone", e.message + " in process.env.TZ");
}
let timezone = await Settings.get("serverTimezone"); let timezone = await Settings.get("serverTimezone");
if (timezone) {
return timezone; // From Settings
} else if (process.env.TZ) { try {
return process.env.TZ; log.debug("timezone", "Using timezone from settings: " + timezone);
} else { if (timezone) {
return dayjs.tz.guess(); this.checkTimezone(timezone);
return timezone;
}
} catch (e) {
log.warn("timezone", e.message + " in settings");
}
// Guess
try {
let guess = dayjs.tz.guess();
log.debug("timezone", "Guessing timezone: " + guess);
if (guess) {
this.checkTimezone(guess);
return guess;
} else {
return "UTC";
}
} catch (e) {
// Guess failed, fall back to UTC
log.debug("timezone", "Guessed an invalid timezone. Use UTC as fallback");
return "UTC";
} }
} }
@ -280,11 +310,24 @@ class UptimeKumaServer {
return dayjs().format("Z"); return dayjs().format("Z");
} }
/**
* Throw an error if the timezone is invalid
* @param timezone
*/
checkTimezone(timezone) {
try {
dayjs.utc("2013-11-18 11:55").tz(timezone).format();
} catch (e) {
throw new Error("Invalid timezone:" + timezone);
}
}
/** /**
* Set the current server timezone and environment variables * Set the current server timezone and environment variables
* @param {string} timezone * @param {string} timezone
*/ */
async setTimezone(timezone) { async setTimezone(timezone) {
this.checkTimezone(timezone);
await Settings.set("serverTimezone", timezone, "general"); await Settings.set("serverTimezone", timezone, "general");
process.env.TZ = timezone; process.env.TZ = timezone;
dayjs.tz.setDefault(timezone); dayjs.tz.setDefault(timezone);

Loading…
Cancel
Save