From 2faa58928fb4fc772002aa40df0fbddee3d3457b Mon Sep 17 00:00:00 2001 From: MeiMei <30769358+mei23@users.noreply.github.com> Date: Sun, 16 Dec 2018 07:06:43 +0900 Subject: [PATCH] Format uptimes (#3629) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Format uptime * 逆だわ * ザ * 1個多い * Fix comment --- .../app/common/scripts/format-uptime.ts | 25 +++++++++++++++++++ .../common/views/widgets/server.uptimes.vue | 9 ++++--- 2 files changed, 30 insertions(+), 4 deletions(-) create mode 100644 src/client/app/common/scripts/format-uptime.ts diff --git a/src/client/app/common/scripts/format-uptime.ts b/src/client/app/common/scripts/format-uptime.ts new file mode 100644 index 000000000..6a2718f64 --- /dev/null +++ b/src/client/app/common/scripts/format-uptime.ts @@ -0,0 +1,25 @@ + +/** + * Format like the uptime command + */ +export default function(sec) { + if (!sec) return sec; + + const day = Math.floor(sec / 86400); + const tod = sec % 86400; + + // Days part in string: 2 days, 1 day, null + const d = day >= 2 ? `${day} days` : day >= 1 ? `${day} day` : null; + + // Time part in string: 1 sec, 1 min, 1:01 + const t + = tod < 60 ? `${Math.floor(tod)} sec` + : tod < 3600 ? `${Math.floor(tod / 60)} min` + : `${Math.floor(tod / 60 / 60)}:${Math.floor((tod / 60) % 60).toString().padStart(2, "0")}`; + + let str = ''; + if (d) str += `${d}, `; + str += t; + + return str; +} diff --git a/src/client/app/common/views/widgets/server.uptimes.vue b/src/client/app/common/views/widgets/server.uptimes.vue index 06713d83c..56d0398e3 100644 --- a/src/client/app/common/views/widgets/server.uptimes.vue +++ b/src/client/app/common/views/widgets/server.uptimes.vue @@ -1,13 +1,14 @@