From 3ccdce40553d28eddd8cbb1a60534f7e77478f90 Mon Sep 17 00:00:00 2001 From: Thomas Lamprecht Date: Tue, 23 Nov 2021 09:34:41 +0100 Subject: [PATCH] utils: format_duration_human: calculate years too Add years and skip showing minute scale once we got over a year, that's just not relevant anymore at that scale.. Months are just not an ideal thing to show, as they have different lengths too (leap years have similar issue, but they differ 0.27% from a normal year, while shortest to longest month is 10.7% difference!) Weeks could be done though, they're fixed at 7 days, but for now I want to avoid unwieldy day numbers like 2634 d as that's just hard to frame correctly. Also adding years now does not makes adding weeks in the future impossible anyway.. Signed-off-by: Thomas Lamprecht --- src/Utils.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/Utils.js b/src/Utils.js index 9ceea62..15baac3 100644 --- a/src/Utils.js +++ b/src/Utils.js @@ -155,7 +155,7 @@ utilities: { // somewhat like a human would tell durations, omit zero values and do not // give seconds precision if we talk days already format_duration_human: function(ut) { - let seconds = 0, minutes = 0, hours = 0, days = 0; + let seconds = 0, minutes = 0, hours = 0, days = 0, years = 0; if (ut <= 0.1) { return '<0.1s'; @@ -171,7 +171,11 @@ utilities: { hours = remaining % 24; remaining = Math.trunc(remaining / 24); if (remaining > 0) { - days = remaining; + days = remaining % 365; + remaining = Math.trunc(remaining / 365); // yea, just lets ignore leap years... + if (remaining > 0) { + years = remaining; + } } } } @@ -182,11 +186,14 @@ utilities: { return t > 0; }; + let addMinutes = !add(years, 'y'); let addSeconds = !add(days, 'd'); add(hours, 'h'); - add(minutes, 'm'); - if (addSeconds) { - add(seconds, 's'); + if (addMinutes) { + add(minutes, 'm'); + if (addSeconds) { + add(seconds, 's'); + } } return res.join(' '); },