Files

23 lines
728 B
TypeScript
Raw Permalink Normal View History

export function formatUptime(seconds: number): string {
if (seconds < 60) return `${seconds}s`;
const days = Math.floor(seconds / 86_400);
const hours = Math.floor((seconds % 86_400) / 3_600);
const minutes = Math.floor((seconds % 3_600) / 60);
if (days > 0) return `${days}d ${hours}h`;
if (hours > 0) return `${hours}h ${minutes}m`;
return `${minutes}m`;
}
const BYTE_UNITS = ["KB", "MB", "GB", "TB"];
export function formatBytes(bytes: number): string {
if (bytes < 1024) return `${bytes} B`;
let value = bytes / 1024;
let unitIndex = 0;
while (value >= 1024 && unitIndex < BYTE_UNITS.length - 1) {
value /= 1024;
unitIndex += 1;
}
return `${value.toFixed(1)} ${BYTE_UNITS[unitIndex]}`;
}