@@ -26,3 +26,36 @@ function parseDate(dateString: string): Date {
|
||||
parts[2] ? parseInt(parts[2], 10) : 1
|
||||
);
|
||||
}
|
||||
|
||||
export function formatDuration(duration?: string | null, __?: (s: string) => string): string | null {
|
||||
if (!duration || !__) return null;
|
||||
|
||||
const timeMatch = duration.match(/PT(\d+)([MH])/);
|
||||
if (timeMatch) {
|
||||
const amount = parseInt(timeMatch[1], 10) || 0;
|
||||
const unit = timeMatch[2];
|
||||
if (unit === "M") {
|
||||
return `${amount} ${amount === 1 ? __("Minute") : __("Minutes")}`;
|
||||
} else if (unit === "H") {
|
||||
return `${amount} ${amount === 1 ? __("Hour") : __("Hours")}`;
|
||||
}
|
||||
}
|
||||
|
||||
const dateMatch = duration.match(/P(\d+)([DW])/);
|
||||
if (dateMatch) {
|
||||
const amount = parseInt(dateMatch[1], 10) || 0;
|
||||
const unit = dateMatch[2];
|
||||
if (unit === "W") {
|
||||
return `${amount} ${amount === 1 ? __("Week") : __("Weeks")}`;
|
||||
} else if (unit === "D") {
|
||||
const days = amount;
|
||||
if (days % 7 === 0 && days > 0) {
|
||||
const weeks = days / 7;
|
||||
return `${weeks} ${weeks === 1 ? __("Week") : __("Weeks")}`;
|
||||
}
|
||||
return `${days} ${days === 1 ? __("Day") : __("Days")}`;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -61,7 +61,7 @@ export {
|
||||
} from "./trustCenterVisibility";
|
||||
export { promisifyMutation } from "./relay";
|
||||
export { fileType, fileSize } from "./file";
|
||||
export { formatDatetime, formatDate, toDateInput } from "./date";
|
||||
export { formatDatetime, formatDate, toDateInput, formatDuration } from "./date";
|
||||
export { getTrustCenterUrl } from "./trustCenter";
|
||||
export { formatError, type GraphQLError } from "./error";
|
||||
export { Role, getAssignableRoles } from "./roles";
|
||||
|
||||
Reference in New Issue
Block a user