mirror of
https://github.com/prometheus/prometheus.git
synced 2024-12-26 14:09:41 -08:00
Time formatting / parsing improvements
Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
parent
70221fc4a0
commit
1f5605fe63
|
@ -4,7 +4,7 @@ dayjs.extend(duration);
|
||||||
import utc from "dayjs/plugin/utc";
|
import utc from "dayjs/plugin/utc";
|
||||||
dayjs.extend(utc);
|
dayjs.extend(utc);
|
||||||
|
|
||||||
export const parseDuration = (durationStr: string): number | null => {
|
export const parsePrometheusDuration = (durationStr: string): number | null => {
|
||||||
if (durationStr === "") {
|
if (durationStr === "") {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -44,7 +44,7 @@ export const parseDuration = (durationStr: string): number | null => {
|
||||||
return dur;
|
return dur;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const formatDuration = (d: number): string => {
|
export const formatPrometheusDuration = (d: number): string => {
|
||||||
let ms = d;
|
let ms = d;
|
||||||
let r = "";
|
let r = "";
|
||||||
if (ms === 0) {
|
if (ms === 0) {
|
||||||
|
@ -87,6 +87,7 @@ export const humanizeDuration = (milliseconds: number): string => {
|
||||||
return "0s";
|
return "0s";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sign = milliseconds < 0 ? "-" : "";
|
||||||
const duration = dayjs.duration(Math.abs(milliseconds), "ms");
|
const duration = dayjs.duration(Math.abs(milliseconds), "ms");
|
||||||
const ms = Math.floor(duration.milliseconds());
|
const ms = Math.floor(duration.milliseconds());
|
||||||
const s = Math.floor(duration.seconds());
|
const s = Math.floor(duration.seconds());
|
||||||
|
@ -104,15 +105,18 @@ export const humanizeDuration = (milliseconds: number): string => {
|
||||||
parts.push(`${m}m`);
|
parts.push(`${m}m`);
|
||||||
}
|
}
|
||||||
if (s !== 0) {
|
if (s !== 0) {
|
||||||
|
if (ms !== 0) {
|
||||||
|
parts.push(`${s}.${ms}s`);
|
||||||
|
} else {
|
||||||
parts.push(`${s}s`);
|
parts.push(`${s}s`);
|
||||||
}
|
}
|
||||||
if (ms !== 0) {
|
} else if (milliseconds !== 0) {
|
||||||
parts.push(`${ms}ms`);
|
parts.push(`${milliseconds.toFixed(3)}ms`);
|
||||||
}
|
}
|
||||||
return (milliseconds < 0 ? "-" : "") + parts.join(" ");
|
return sign + parts.join(" ");
|
||||||
};
|
};
|
||||||
|
|
||||||
export const formatRelative = (
|
export const humanizeDurationRelative = (
|
||||||
startStr: string,
|
startStr: string,
|
||||||
end: number,
|
end: number,
|
||||||
suffix: string = " ago"
|
suffix: string = " ago"
|
||||||
|
@ -123,3 +127,8 @@ export const formatRelative = (
|
||||||
}
|
}
|
||||||
return humanizeDuration(end - start) + suffix;
|
return humanizeDuration(end - start) + suffix;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const formatTimestamp = (t: number, useLocalTime: boolean) =>
|
||||||
|
useLocalTime
|
||||||
|
? dayjs.unix(t).tz(dayjs.tz.guess()).format()
|
||||||
|
: dayjs.unix(t).utc().format();
|
||||||
|
|
Loading…
Reference in a new issue