From 736924c83997588c45632cae644db33635f40106 Mon Sep 17 00:00:00 2001 From: luukas Date: Sun, 18 Apr 2021 00:20:23 +0300 Subject: [PATCH] Fix uptime negative time error by saving it as UTC from the start. --- app/classes/shared/server.py | 4 +-- .../templates/panel/parts/details_stats.html | 32 +++++++------------ app/translations/en_EN.json | 2 +- app/translations/fi_FI.json | 2 +- 4 files changed, 15 insertions(+), 25 deletions(-) diff --git a/app/classes/shared/server.py b/app/classes/shared/server.py index 7059a7a0..ba1a5e05 100644 --- a/app/classes/shared/server.py +++ b/app/classes/shared/server.py @@ -130,8 +130,8 @@ class Server: self.process = pexpect.spawn(self.server_command, cwd=self.server_path, timeout=None, encoding=None) self.is_crashed = False - ts = time.time() - self.start_time = str(datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')) + # see https://docs.python.org/3/library/datetime.html#datetime.datetime.now + self.start_time = str(datetime.datetime.utcnow().strftime('%Y-%m-%d %H:%M:%S')) if psutil.pid_exists(self.process.pid): self.PID = self.process.pid diff --git a/app/frontend/templates/panel/parts/details_stats.html b/app/frontend/templates/panel/parts/details_stats.html index 046cf3c2..c3288385 100644 --- a/app/frontend/templates/panel/parts/details_stats.html +++ b/app/frontend/templates/panel/parts/details_stats.html @@ -86,11 +86,8 @@ let startedLocal; if (started != null) { - console.log('88', '{{ data['server_stats']['started'] }}'); - {% if data['server_stats']['started'] != 'False' %} - 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 = '{{ data['server_stats']['started'] }}'; + console.log('started utc:', startedUTC); startedUTC = moment.utc(startedUTC, 'YYYY-MM-DD HH:mm:ss'); let browserUTCOffset = moment().utcOffset(); // This is in minutes @@ -98,32 +95,25 @@ startedLocal = startedUTC.utcOffset(browserUTCOffset); startedLocalFormatted = startedLocal.format('YYYY-MM-DD HH:mm:ss'); - console.log('startedLocal', startedLocal); - console.log('startedLocalFormatted', startedLocalFormatted); + console.log('started local time:', startedLocalFormatted); started.textContent = startedLocalFormatted } - let nowServerTime = '{{ data['time'] }}'; - 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 calculateUptime = () => { + var msdiff = moment() + .diff(startedLocal); var diff = moment.duration(msdiff); uptime.textContent = durationToHumanizedString(diff); + } + + if (uptime != null && started != null) { console.log('startedLocal', startedLocal) if (startedLocal) { - var uptimeLoop = setInterval(() => { - var msdiff = moment() - .diff(startedLocal); - var diff = moment.duration(msdiff); - - uptime.textContent = durationToHumanizedString(diff); - }, 1000) + calculateUptime() + var uptimeLoop = setInterval(calculateUptime, 1000) } } diff --git a/app/translations/en_EN.json b/app/translations/en_EN.json index c24f35d3..a5549fa8 100644 --- a/app/translations/en_EN.json +++ b/app/translations/en_EN.json @@ -106,7 +106,7 @@ "version": "Version", "description": "Description", "errorCalculatingUptime": "Error Calculating Uptime", - "serverTime": "Server Time", + "serverTime": "UTC Time", "unableToConnect": "Unable To Connect" }, "serverDetails": { diff --git a/app/translations/fi_FI.json b/app/translations/fi_FI.json index b12ecb80..c0d89946 100644 --- a/app/translations/fi_FI.json +++ b/app/translations/fi_FI.json @@ -106,7 +106,7 @@ "version": "Versio", "description": "Kuvaus", "errorCalculatingUptime": "Virhe laskettaessa käyttöaikaa", - "serverTime": "Palvelimen aikaa", + "serverTime": "UTC aikaa", "unableToConnect": "Yhteyden muodostaminen epäonnistui" }, "serverDetails": {