summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/internal/console/constructor.js44
1 files changed, 30 insertions, 14 deletions
diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js
index 554323d9e3..c3339241aa 100644
--- a/lib/internal/console/constructor.js
+++ b/lib/internal/console/constructor.js
@@ -3,7 +3,7 @@
// The Console constructor is not actually used to construct the global
// console. It's exported for backwards compatibility.
-const { Object, ObjectPrototype, Reflect } = primordials;
+const { Object, ObjectPrototype, Reflect, Math } = primordials;
const { trace } = internalBinding('trace_events');
const {
@@ -533,22 +533,38 @@ function timeLogImpl(self, name, label, data) {
return true;
}
+function pad(value) {
+ return `${value}`.padStart(2, '0');
+}
+
function formatTime(ms) {
- let value = ms;
- let unit = 'ms';
-
- if (ms >= kHour) {
- value = ms / kHour;
- unit = 'h';
- } else if (ms >= kMinute) {
- value = ms / kMinute;
- unit = 'min';
- } else if (ms >= kSecond) {
- value = ms / kSecond;
- unit = 's';
+ let hours = 0;
+ let minutes = 0;
+ let seconds = 0;
+
+ if (ms >= kSecond) {
+ if (ms >= kMinute) {
+ if (ms >= kHour) {
+ hours = Math.floor(ms / kHour);
+ ms = ms % kHour;
+ }
+ minutes = Math.floor(ms / kMinute);
+ ms = ms % kMinute;
+ }
+ seconds = ms / kSecond;
+ }
+
+ if (hours !== 0 || minutes !== 0) {
+ [seconds, ms] = seconds.toFixed(3).split('.');
+ const res = hours !== 0 ? `${hours}:${pad(minutes)}` : minutes;
+ return `${res}:${pad(seconds)}.${ms} (${hours !== 0 ? 'h:m' : ''}m:ss.mmm)`;
+ }
+
+ if (seconds !== 0) {
+ return `${seconds.toFixed(3)}s`;
}
- return value.toFixed(3) + unit;
+ return `${Number(ms.toFixed(3))}ms`;
}
const keyKey = 'Key';