Add configurable server timezone

This commit is contained in:
Louis Lam
2022-10-09 20:59:58 +08:00
parent b007681e67
commit a36f24d827
7 changed files with 66 additions and 13 deletions

View File

@@ -9,6 +9,7 @@ const Database = require("./database");
const util = require("util");
const { CacheableDnsHttpAgent } = require("./cacheable-dns-http-agent");
const { Settings } = require("./settings");
const dayjs = require("dayjs");
/**
* `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue.
@@ -84,6 +85,13 @@ class UptimeKumaServer {
this.io = new Server(this.httpServer);
}
async initAfterDatabaseReady() {
process.env.TZ = await this.getTimezone();
dayjs.tz.setDefault(process.env.TZ);
log.debug("DEBUG", "Timezone: " + process.env.TZ);
log.debug("DEBUG", "Current Time: " + dayjs.tz().format());
}
async sendMonitorList(socket) {
let list = await this.getMonitorJSONList(socket.userID);
this.io.to(socket.userID).emit("monitorList", list);
@@ -184,6 +192,23 @@ class UptimeKumaServer {
return clientIP.replace(/^.*:/, "");
}
}
async getTimezone() {
let timezone = await Settings.get("serverTimezone");
if (timezone) {
return timezone;
} else if (process.env.TZ) {
return process.env.TZ;
} else {
return dayjs.tz.guess();
}
}
async setTimezone(timezone) {
await Settings.set("serverTimezone", timezone, "general");
process.env.TZ = timezone;
dayjs.tz.setDefault(timezone);
}
}
module.exports = {