Files
Checkmate/client/src/Utils/timeUtils.js
T

137 lines
3.5 KiB
JavaScript
Raw Normal View History

2024-09-12 12:17:31 +08:00
import dayjs from "dayjs";
2025-01-27 13:17:05 -08:00
import duration from "dayjs/plugin/duration";
2024-09-12 12:17:31 +08:00
import utc from "dayjs/plugin/utc";
import timezone from "dayjs/plugin/timezone";
2024-12-30 16:07:28 -08:00
import customParseFormat from "dayjs/plugin/customParseFormat";
2024-09-12 12:17:31 +08:00
dayjs.extend(utc);
dayjs.extend(timezone);
2024-12-30 16:07:28 -08:00
dayjs.extend(customParseFormat);
2025-01-27 13:17:05 -08:00
dayjs.extend(duration);
2024-09-12 12:17:31 +08:00
2024-10-04 11:40:01 +08:00
export const MS_PER_SECOND = 1000;
export const MS_PER_MINUTE = 60 * MS_PER_SECOND;
export const MS_PER_HOUR = 60 * MS_PER_MINUTE;
export const MS_PER_DAY = 24 * MS_PER_HOUR;
2024-10-09 14:10:38 +08:00
export const MS_PER_WEEK = MS_PER_DAY * 7;
2024-10-04 11:40:01 +08:00
2024-07-28 16:51:47 -04:00
export const formatDuration = (ms) => {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
2024-07-28 16:51:47 -04:00
let dateStr = "";
2024-07-28 16:51:47 -04:00
days && (dateStr += `${days}d `);
hours && (dateStr += `${hours % 24}h `);
minutes && (dateStr += `${minutes % 60}m `);
seconds && (dateStr += `${seconds % 60}s `);
2024-07-28 16:51:47 -04:00
dateStr === "" && (dateStr = "0s");
2024-07-28 16:51:47 -04:00
return dateStr;
2024-07-28 16:51:47 -04:00
};
export const formatDurationRounded = (ms) => {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
2024-07-28 16:51:47 -04:00
let time = "";
if (days > 0) {
time += `${days} day${days !== 1 ? "s" : ""}`;
return time;
}
if (hours > 0) {
time += `${hours} hour${hours !== 1 ? "s" : ""}`;
return time;
}
if (minutes > 0) {
time += `${minutes} minute${minutes !== 1 ? "s" : ""}`;
return time;
}
if (seconds > 0) {
time += `${seconds} second${seconds !== 1 ? "s" : ""}`;
return time;
}
2024-07-28 16:51:47 -04:00
return time;
2024-07-28 16:51:47 -04:00
};
export const formatDurationSplit = (ms) => {
const seconds = Math.floor(ms / 1000);
const minutes = Math.floor(seconds / 60);
const hours = Math.floor(minutes / 60);
const days = Math.floor(hours / 24);
return days > 0
? { time: days, format: days === 1 ? "day" : "days" }
: hours > 0
? { time: hours, format: hours === 1 ? "hour" : "hours" }
: minutes > 0
? { time: minutes, format: minutes === 1 ? "minute" : "minutes" }
: seconds > 0
? { time: seconds, format: seconds === 1 ? "second" : "seconds" }
: { time: 0, format: "seconds" };
};
2025-01-27 13:17:05 -08:00
export const getHumanReadableDuration = (ms) => {
const durationObj = dayjs.duration(ms);
2025-06-17 11:32:35 +08:00
const parts = {
days: Math.floor(durationObj.asDays()),
hours: durationObj.hours(),
minutes: durationObj.minutes(),
seconds: durationObj.seconds(),
2025-07-11 11:23:50 -07:00
milliseconds: durationObj.milliseconds(),
2025-06-17 11:32:35 +08:00
};
const result = [];
if (parts.days > 0) {
result.push(`${parts.days}d`);
}
if (parts.hours > 0) {
result.push(`${parts.hours}h`);
2025-01-27 13:17:05 -08:00
}
2025-06-17 11:32:35 +08:00
if (result.length < 2 && parts.minutes > 0) {
result.push(`${parts.minutes}m`);
}
if (result.length < 2 && parts.seconds > 0) {
result.push(`${parts.seconds}s`);
}
2025-07-11 11:23:50 -07:00
if (result.length < 2 && parts.milliseconds > 0 && parts.seconds < 1) {
result.push(`${parts.milliseconds.toFixed(2)}ms`);
}
2025-06-17 11:32:35 +08:00
if (result.length === 0) {
// fallback for durations < 1s
2025-06-17 13:41:25 +08:00
return "0s";
2025-06-17 11:32:35 +08:00
}
2025-06-17 13:41:25 +08:00
return result.join(" ");
2025-01-27 13:17:05 -08:00
};
2024-08-22 20:15:49 -04:00
export const formatDate = (date, customOptions) => {
const options = {
year: "numeric",
month: "long",
day: "numeric",
hour: "numeric",
minute: "numeric",
hour12: true,
...customOptions,
};
2024-07-28 16:51:47 -04:00
// Return the date using the specified options
return date
.toLocaleString("en-US", options)
.replace(/\b(AM|PM)\b/g, (match) => match.toLowerCase());
2024-07-28 16:51:47 -04:00
};
2024-09-12 12:17:31 +08:00
export const formatDateWithTz = (timestamp, format, timezone) => {
2024-12-30 16:07:28 -08:00
const formattedDate = dayjs(timestamp).tz(timezone).format(format);
return formattedDate;
2024-09-12 12:17:31 +08:00
};