Skip to content

Commit

Permalink
Improved human readable duration behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
vertigo17 committed Jun 14, 2024
1 parent 0e261ff commit bc7853f
Showing 1 changed file with 27 additions and 5 deletions.
32 changes: 27 additions & 5 deletions source/src/main/webapp/js/global/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -2554,26 +2554,48 @@ function getDateMedium(date) {
}
}

function getHumanReadableDuration(durInSec) {
function getHumanReadableDuration(durInSec, nbUnits = 2) {
let dur = durInSec;
let unit = "s";
let cnt1 = 0;
let cnt2 = 0;
if (dur > 60) {
dur = dur / 60
unit = "min"
} else {
return Math.round(dur * 10) / 10 + " " + unit;
return Math.round(dur) + " " + unit;
}
if (dur > 60) {
if (dur >= 60) {
dur = dur / 60
unit = "h"
} else {
return Math.round(dur * 10) / 10 + " " + unit;
cnt1 = Math.floor(dur);
cnt2 = durInSec - (cnt1 * 60)
if ((cnt2 > 0) && (nbUnits > 1)) {
return cnt1 + " " + unit + " " + cnt2 + " s";
} else {
return cnt1 + " " + unit;
}
}
if (dur > 24) {
dur = dur / 24
unit = "d"
} else {
cnt1 = Math.floor(dur);
cnt2 = durInSec - (cnt1 * 60 * 60)
if ((cnt2 > 0) && (nbUnits > 1)) {
return cnt1 + " " + unit + " " + getHumanReadableDuration(cnt2, (nbUnits - 1));
} else {
return cnt1 + " " + unit;
}
}
cnt1 = Math.floor(dur);
cnt2 = durInSec - (cnt1 * 60 * 60 * 24)
if ((cnt2 > 0) && (nbUnits > 1)) {
return cnt1 + " " + unit + " " + getHumanReadableDuration(cnt2, (nbUnits - 1));
} else {
return cnt1 + " " + unit;
}
return Math.round(dur * 10) / 10 + " " + unit;
}


Expand Down

0 comments on commit bc7853f

Please sign in to comment.