woodpecker/web/src/utils/duration.ts
Anbraten 58838f225c
Rewrite of WebUI (#245)
Rewrite of the UI using Typescript, Vue3, Windicss and Vite. The design should  be close to the current one with some changes:
- latest pipeline in a sidebar on the right
- secrets and registry as part of the repo-settings (secrets and registry entries shouldn't be used as much so they can be "hidden" under settings IMO)
- start page shows list of active repositories with button to enable / add new ones (currently you see all repositories and in most cases you only add new repositories once in a while)
2021-11-03 17:40:31 +01:00

42 lines
1.1 KiB
TypeScript

import humanizeDuration from 'humanize-duration';
const enShort = {
w: () => 'w',
d: () => 'd',
h: () => 'h',
m: () => 'min',
s: () => 'sec',
};
const durationOptions: humanizeDuration.HumanizerOptions = {
round: true,
languages: { en_short: enShort },
language: 'en_short',
};
export function prettyDuration(durationMs: number): string {
if (durationMs < 1000 * 60 * 60) {
return humanizeDuration(durationMs, durationOptions);
}
return humanizeDuration(durationMs, { ...durationOptions, units: ['y', 'mo', 'd', 'h', 'm'] });
}
function leadingZeros(n: number, length: number): string {
let res = n.toString();
while (res.length < length) {
res = `0${res}`;
}
return res;
}
export function durationAsNumber(durationMs: number): string {
const durationSeconds = durationMs / 1000;
const seconds = leadingZeros(Math.floor(durationSeconds % 60), 2);
const minutes = leadingZeros(Math.floor(durationSeconds / 60) % 60, 2);
const hours = Math.floor(durationSeconds / 3600);
if (hours !== 0) {
return `${hours}:${minutes}:${seconds}`;
}
return `${minutes}:${seconds}`;
}