Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/test/unit/marker-schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe('marker schema formatting', function () {
['duration', 10],
['duration', 12.3456789],
['duration', 123456.789],
['duration', 123456789],
['duration', 0.000123456],
['time', 12.3456789],
['seconds', 0],
Expand Down Expand Up @@ -250,7 +251,8 @@ describe('marker schema formatting', function () {
"duration - 0s",
"duration - 10ms",
"duration - 12.346ms",
"duration - 123.46s",
"duration - 2.06min",
"duration - 34.3h",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the tests!

"duration - 123.46ns",
"time - 12.346ms",
"seconds - 0.000s",
Expand Down
44 changes: 43 additions & 1 deletion src/utils/format-numbers.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,13 +244,55 @@ export function formatSeconds(
);
}

export function formatMinutes(
time: Milliseconds,
significantDigits: number = 5,
maxFractionalDigits: number = 2
) {
return (
formatNumber(
time / (60 * 1000),
significantDigits,
Math.min(2, maxFractionalDigits)
) + 'min'
);
}

export function formatHours(
time: Milliseconds,
significantDigits: number = 5,
maxFractionalDigits: number = 1
) {
return (
formatNumber(
time / (3600 * 1000),
significantDigits,
Math.min(1, maxFractionalDigits)
) + 'h'
);
}

export function formatTimestamp(
time: Milliseconds,
significantDigits: number = 5,
maxFractionalDigits: number = 3
) {
// Format in the closest base (seconds, milliseconds, microseconds, or nanoseconds),
// Format in the closest base (hours, minutes, seconds, milliseconds, microseconds, or nanoseconds),
// to avoid cases where times are displayed with too many leading zeroes to be useful.
if (time >= 3600 * 1000) {
return formatHours(
time,
significantDigits,
Number.isInteger(time / (3600 * 1000)) ? 0 : maxFractionalDigits
);
}
if (time >= 60 * 1000) {
return formatMinutes(
time,
significantDigits,
Number.isInteger(time / (60 * 1000)) ? 0 : maxFractionalDigits
);
}
if (time >= 1000) {
return formatSeconds(
time,
Expand Down