From 1298b2384495c4da32094396f0359c5a276e5175 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Tue, 17 Jun 2025 11:32:35 +0800 Subject: [PATCH] update time format function --- .../Components/UptimeStatusBoxes/index.jsx | 2 +- client/src/Utils/timeUtils.js | 43 ++++++++++++------- 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/client/src/Pages/Uptime/Details/Components/UptimeStatusBoxes/index.jsx b/client/src/Pages/Uptime/Details/Components/UptimeStatusBoxes/index.jsx index 0e362e187..3778e4c6d 100644 --- a/client/src/Pages/Uptime/Details/Components/UptimeStatusBoxes/index.jsx +++ b/client/src/Pages/Uptime/Details/Components/UptimeStatusBoxes/index.jsx @@ -80,7 +80,7 @@ const UptimeStatusBoxes = ({ }; UptimeStatusBoxes.propTypes = { - shouldRender: PropTypes.bool, + isLoading: PropTypes.bool, monitor: PropTypes.object, monitorStats: PropTypes.object, certificateExpiry: PropTypes.string, diff --git a/client/src/Utils/timeUtils.js b/client/src/Utils/timeUtils.js index 3a84dfcf4..e03f0b5b7 100644 --- a/client/src/Utils/timeUtils.js +++ b/client/src/Utils/timeUtils.js @@ -79,22 +79,35 @@ export const formatDurationSplit = (ms) => { export const getHumanReadableDuration = (ms) => { const durationObj = dayjs.duration(ms); - if (durationObj.asDays() >= 1) { - const days = Math.floor(durationObj.asDays()); - return { time: days, units: days === 1 ? "day" : "days" }; - } else if (durationObj.asHours() >= 1) { - const hoursRounded = Math.round(durationObj.asHours() * 10) / 10; - const hours = Number.isInteger(hoursRounded) - ? Math.floor(hoursRounded) - : hoursRounded; - return { time: hours, units: hours <= 1 ? "hour" : "hours" }; - } else if (durationObj.asMinutes() >= 1) { - const minutes = Math.floor(durationObj.asMinutes()); - return { time: minutes, units: minutes === 1 ? "minute" : "minutes" }; - } else { - const seconds = Math.floor(durationObj.asSeconds()); - return { time: seconds, units: seconds === 1 ? "second" : "seconds" }; + + const parts = { + days: Math.floor(durationObj.asDays()), + hours: durationObj.hours(), + minutes: durationObj.minutes(), + seconds: durationObj.seconds(), + }; + + const result = []; + + if (parts.days > 0) { + result.push(`${parts.days}d`); } + if (parts.hours > 0) { + result.push(`${parts.hours}h`); + } + if (result.length < 2 && parts.minutes > 0) { + result.push(`${parts.minutes}m`); + } + if (result.length < 2 && parts.seconds > 0) { + result.push(`${parts.seconds}s`); + } + + if (result.length === 0) { + // fallback for durations < 1s + return { time: 0, units: "seconds" }; + } + + return { time: result.join(" "), units: null }; }; export const formatDate = (date, customOptions) => {