[Utils] fixing rounding issues in time display

Prevent getting strings like "5m60s" when rounding up.
This commit is contained in:
Candice Bentéjac 2023-11-20 11:51:33 +01:00 committed by Fabien Castan
parent 9a09310f07
commit 7abbb50302
4 changed files with 53 additions and 36 deletions

View file

@ -14,40 +14,57 @@ function plainToHtml(t) {
return escaped.replace(/\n/g, '<br>') // replace line breaks
}
function sec2time(time) {
var pad = function(num, size) { return ('000' + num).slice(size * -1) },
hours = Math.floor(time / 60 / 60),
minutes = Math.floor(time / 60) % 60,
seconds = Math.floor(time - minutes * 60);
return pad(hours, 2) + ':' + pad(minutes, 2) + ':' + pad(seconds, 2)
function divmod(x, y) {
// Perform the division and get the quotient
const quotient = Math.floor(x / y);
// Compute the remainder
const remainder = x % y;
return [quotient, remainder];
}
function getTimeStr(elapsed)
{
if (elapsed <= 0)
return ""
function sec2timeHMS(totalSeconds) {
const [totalMinutes, seconds] = divmod(totalSeconds, 60.0)
const [hours, minutes] = divmod(totalMinutes, 60.0)
var hours = 0
var min = 0
var finalTime = ""
return {
hours: hours,
minutes: minutes,
seconds: seconds
};
}
if (elapsed > 3600) {
hours = Math.floor(elapsed / 3600)
elapsed = elapsed - (hours * 3600)
finalTime += hours + "h"
}
if (elapsed > 60) {
min = Math.floor(elapsed / 60)
elapsed = elapsed - (min * 60)
finalTime += min + "m"
}
if (hours === 0 && min === 0) {
// Millisecond precision for execution times below 1 min
finalTime += Number(elapsed.toLocaleString(Qt.locale('en-US'))) + "s"
function sec2timecode(timeSeconds) {
var pad = function(num, size) { return ('000' + num).slice(size * -1) }
var timeObj = sec2timeHMS(Math.round(timeSeconds))
var timeStr = pad(timeObj.hours, 2) + ':' + pad(timeObj.minutes, 2) + ':' + pad(timeObj.seconds, 2)
return timeStr
}
function sec2timeStr(timeSeconds) {
// Need to decide the rounding precision first
// to propagate the right values
if(timeSeconds >= 60.0) {
timeSeconds = Math.round(timeSeconds)
} else {
finalTime += Math.round(elapsed) + "s"
timeSeconds = parseFloat(timeSeconds.toFixed(2))
}
return finalTime
var timeObj = sec2timeHMS(timeSeconds)
var timeStr = ""
if(timeObj.hours > 0) {
timeStr += timeObj.hours + "h"
}
if(timeObj.hours > 0 || timeObj.minutes > 0) {
timeStr += timeObj.minutes + "m"
}
if(timeObj.hours === 0) {
// seconds only matter if the elapsed time is less than 1 hour
if(timeObj.minutes === 0) {
// If less than a minute, keep millisecond precision
timeStr += timeObj.seconds.toFixed(2) + "s"
} else {
// If more than a minute, do not need more precision than seconds
timeStr += Math.round(timeObj.seconds) + "s"
}
}
return timeStr
}