Fix uptime negative time error by saving it as UTC from the start.

This commit is contained in:
luukas 2021-04-18 00:20:23 +03:00
parent 9049e44803
commit 736924c839
4 changed files with 15 additions and 25 deletions

View File

@ -130,8 +130,8 @@ class Server:
self.process = pexpect.spawn(self.server_command, cwd=self.server_path, timeout=None, encoding=None) self.process = pexpect.spawn(self.server_command, cwd=self.server_path, timeout=None, encoding=None)
self.is_crashed = False self.is_crashed = False
ts = time.time() # see https://docs.python.org/3/library/datetime.html#datetime.datetime.now
self.start_time = str(datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')) self.start_time = str(datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S'))
if psutil.pid_exists(self.process.pid): if psutil.pid_exists(self.process.pid):
self.PID = self.process.pid self.PID = self.process.pid

View File

@ -86,11 +86,8 @@
let startedLocal; let startedLocal;
if (started != null) { if (started != null) {
console.log('88', '{{ data['server_stats']['started'] }}'); startedUTC = '{{ data['server_stats']['started'] }}';
{% if data['server_stats']['started'] != 'False' %} console.log('started utc:', startedUTC);
startedUTC = '{{ (datetime.datetime.strptime(data['server_stats']['started'], '%Y-%m-%d %H:%M:%S') - datetime.timedelta(seconds=-time.timezone)).strftime('%Y-%m-%d %H:%M:%S') }}';
{% end %}
console.log('utc', startedUTC);
startedUTC = moment.utc(startedUTC, 'YYYY-MM-DD HH:mm:ss'); startedUTC = moment.utc(startedUTC, 'YYYY-MM-DD HH:mm:ss');
let browserUTCOffset = moment().utcOffset(); // This is in minutes let browserUTCOffset = moment().utcOffset(); // This is in minutes
@ -98,32 +95,25 @@
startedLocal = startedUTC.utcOffset(browserUTCOffset); startedLocal = startedUTC.utcOffset(browserUTCOffset);
startedLocalFormatted = startedLocal.format('YYYY-MM-DD HH:mm:ss'); startedLocalFormatted = startedLocal.format('YYYY-MM-DD HH:mm:ss');
console.log('startedLocal', startedLocal); console.log('started local time:', startedLocalFormatted);
console.log('startedLocalFormatted', startedLocalFormatted);
started.textContent = startedLocalFormatted started.textContent = startedLocalFormatted
} }
let nowServerTime = '{{ data['time'] }}'; var calculateUptime = () => {
let startedServerTime = '{{ data['server_stats']['started'] }}';
if (uptime != null && started != null) {
var msdiff = moment(nowServerTime,"YYYY-MM-DD hh:mm:ss")
.diff(moment(startedServerTime,"YYYY-MM-DD hh:mm:ss"));
var diff = moment.duration(msdiff);
uptime.textContent = durationToHumanizedString(diff);
console.log('startedLocal', startedLocal)
if (startedLocal) {
var uptimeLoop = setInterval(() => {
var msdiff = moment() var msdiff = moment()
.diff(startedLocal); .diff(startedLocal);
var diff = moment.duration(msdiff); var diff = moment.duration(msdiff);
uptime.textContent = durationToHumanizedString(diff); uptime.textContent = durationToHumanizedString(diff);
}, 1000) }
if (uptime != null && started != null) {
console.log('startedLocal', startedLocal)
if (startedLocal) {
calculateUptime()
var uptimeLoop = setInterval(calculateUptime, 1000)
} }
} }

View File

@ -106,7 +106,7 @@
"version": "Version", "version": "Version",
"description": "Description", "description": "Description",
"errorCalculatingUptime": "Error Calculating Uptime", "errorCalculatingUptime": "Error Calculating Uptime",
"serverTime": "Server Time", "serverTime": "UTC Time",
"unableToConnect": "Unable To Connect" "unableToConnect": "Unable To Connect"
}, },
"serverDetails": { "serverDetails": {

View File

@ -106,7 +106,7 @@
"version": "Versio", "version": "Versio",
"description": "Kuvaus", "description": "Kuvaus",
"errorCalculatingUptime": "Virhe laskettaessa käyttöaikaa", "errorCalculatingUptime": "Virhe laskettaessa käyttöaikaa",
"serverTime": "Palvelimen aikaa", "serverTime": "UTC aikaa",
"unableToConnect": "Yhteyden muodostaminen epäonnistui" "unableToConnect": "Yhteyden muodostaminen epäonnistui"
}, },
"serverDetails": { "serverDetails": {