Skip to content

Commit 205076e

Browse files
refactor: change how timings are formatted (#17623)
1 parent ef101ae commit 205076e

File tree

1 file changed

+21
-21
lines changed
  • site/src/modules/workspaces/WorkspaceTiming/Chart

1 file changed

+21
-21
lines changed

site/src/modules/workspaces/WorkspaceTiming/Chart/utils.ts

+21-21
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,29 @@ export const makeTicks = (time: number) => {
6161
};
6262

6363
export const formatTime = (time: number): string => {
64-
const seconds = Math.floor(time / 1000);
65-
const minutes = Math.floor(seconds / 60);
66-
const hours = Math.floor(minutes / 60);
67-
const days = Math.floor(hours / 24);
64+
const absTime = Math.abs(time);
65+
let unit = "";
66+
let value = 0;
6867

69-
const parts: string[] = [];
70-
if (days > 0) {
71-
parts.push(`${days}d`);
68+
if (absTime < second) {
69+
value = time;
70+
unit = "ms";
71+
} else if (absTime < minute) {
72+
value = time / second;
73+
unit = "s";
74+
} else if (absTime < hour) {
75+
value = time / minute;
76+
unit = "m";
77+
} else if (absTime < day) {
78+
value = time / hour;
79+
unit = "h";
80+
} else {
81+
value = time / day;
82+
unit = "d";
7283
}
73-
if (hours > 0) {
74-
parts.push(`${hours % 24}h`);
75-
}
76-
if (minutes > 0) {
77-
parts.push(`${minutes % 60}m`);
78-
}
79-
if (seconds > 0) {
80-
parts.push(`${seconds % 60}s`);
81-
}
82-
if (time % 1000 > 0) {
83-
parts.push(`${time % 1000}ms`);
84-
}
85-
86-
return parts.join(" ");
84+
return `${value.toLocaleString(undefined, {
85+
maximumFractionDigits: 2,
86+
})}${unit}`;
8787
};
8888

8989
export const calcOffset = (range: TimeRange, baseRange: TimeRange): number => {

0 commit comments

Comments
 (0)