Unify uptime formatting (#2483)

This commit is contained in:
shamoon 2023-12-15 14:08:37 -08:00 committed by GitHub
parent 24e25e8953
commit e768b1c83a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 36 additions and 62 deletions

View file

@ -84,6 +84,22 @@ function prettyBytes(number, options) {
return `${prefix + numberString} ${unit}`;
}
function uptime(uptimeInSeconds, i18next) {
const mo = Math.floor(uptimeInSeconds / (3600 * 24 * 31));
const d = Math.floor((uptimeInSeconds % (3600 * 24 * 31)) / (3600 * 24));
const h = Math.floor((uptimeInSeconds % (3600 * 24)) / 3600);
const m = Math.floor((uptimeInSeconds % 3600) / 60);
const s = Math.floor(uptimeInSeconds % 60);
const moDisplay = mo > 0 ? mo + i18next.t("common.months") : "";
const dDisplay = d > 0 ? d + i18next.t("common.days") : "";
const hDisplay = h > 0 && mo === 0 ? h + i18next.t("common.hours") : "";
const mDisplay = m > 0 && mo === 0 && d === 0 ? m + i18next.t("common.minutes") : "";
const sDisplay = s > 0 && mo === 0 && d === 0 && h === 0 ? s + i18next.t("common.seconds") : "";
return (moDisplay + dDisplay + hDisplay + mDisplay + sDisplay).replace(/,\s*$/, "");
}
module.exports = {
i18n: {
defaultLocale: "en",
@ -126,6 +142,7 @@ module.exports = {
i18next.services.formatter.add("date", (value, lng, options) =>
new Intl.DateTimeFormat(lng, { ...options }).format(new Date(value)),
);
i18next.services.formatter.add("uptime", (value, lng) => uptime(value, i18next));
},
type: "3rdParty",
},