[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

@ -226,7 +226,7 @@ Item {
anchors.fill: parent anchors.fill: parent
} }
enabled: logLine.duration > 0 enabled: logLine.duration > 0
ToolTip.text: "Elapsed time: " + Format.getTimeStr(logLine.duration) ToolTip.text: "Elapsed time: " + Format.sec2timeStr(logLine.duration)
ToolTip.visible: mouseArea.containsMouse ToolTip.visible: mouseArea.containsMouse
} }

View file

@ -34,7 +34,7 @@ Panel {
else { else {
timer.stop() timer.stop()
if (node !== null && (node.isFinishedOrRunning() || globalStatus == "ERROR")) { if (node !== null && (node.isFinishedOrRunning() || globalStatus == "ERROR")) {
computationInfo.text = Format.getTimeStr(node.elapsedTime) computationInfo.text = Format.sec2timeStr(node.elapsedTime)
} }
else{ else{
computationInfo.text = "" computationInfo.text = ""
@ -57,7 +57,7 @@ Panel {
nodeStartDateTime = new Date(node.getStartDateTime()).getTime() nodeStartDateTime = new Date(node.getStartDateTime()).getTime()
} }
var now = new Date().getTime() var now = new Date().getTime()
parent.text = Format.getTimeStr((now-nodeStartDateTime)/1000) parent.text = Format.sec2timeStr((now-nodeStartDateTime)/1000)
} }
} }
padding: 2 padding: 2
@ -75,7 +75,7 @@ Panel {
if (node !== null && (node.isFinishedOrRunning() || (node.isSubmittedOrRunning() && node.elapsedTime > 0))) { if (node !== null && (node.isFinishedOrRunning() || (node.isSubmittedOrRunning() && node.elapsedTime > 0))) {
var longestChunkTime = getLongestChunkTime(node.chunks) var longestChunkTime = getLongestChunkTime(node.chunks)
if (longestChunkTime > 0) if (longestChunkTime > 0)
return "Longest chunk: " + Format.getTimeStr(longestChunkTime) + " (" + node.chunks.count + " chunks)" return "Longest chunk: " + Format.sec2timeStr(longestChunkTime) + " (" + node.chunks.count + " chunks)"
else else
return "" return ""
} else { } else {

View file

@ -50,12 +50,12 @@ FocusScope {
KeyValue { KeyValue {
key: "Time" key: "Time"
property real time: node.elapsedTime property real time: node.elapsedTime
value: time > 0.0 ? Format.sec2time(time) : "-" value: time > 0.0 ? Format.sec2timecode(time) : "-"
} }
KeyValue { KeyValue {
key: "Cumulated Time" key: "Cumulated Time"
property real time: node.recursiveElapsedTime property real time: node.recursiveElapsedTime
value: time > 0.0 ? Format.sec2time(time) : "-" value: time > 0.0 ? Format.sec2timecode(time) : "-"
} }
} }
} }

View file

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