Skip to content
Clone
79bd077
56 lines 1.3 KB
export function formatDateTime(value?: string | null): string {
if (!value) {
return '-'
}
const date = new Date(value)
if (Number.isNaN(date.getTime())) {
return value
}
return date.toLocaleString('zh-CN', {
year: 'numeric',
month: '2-digit',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
})
}
export function formatBytes(value: number): string {
if (!Number.isFinite(value) || value <= 0) {
return '0 B'
}
const units = ['B', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB']
let size = value
let unit = 0
while (size >= 1024 && unit < units.length - 1) {
size /= 1024
unit += 1
}
return `${size.toFixed(unit === 0 ? 0 : 2)} ${units[unit]}`
}
export function formatUptime(seconds: number): string {
const days = Math.floor(seconds / 86400)
const hours = Math.floor((seconds % 86400) / 3600)
const minutes = Math.floor((seconds % 3600) / 60)
if (days > 0) {
return `${days}d ${hours}h ${minutes}m`
}
if (hours > 0) {
return `${hours}h ${minutes}m`
}
return `${minutes}m`
}
export function shortId(id: string): string {
return id.length > 10 ? `${id.slice(0, 8)}…` : id
}
export function prettyJson(value: unknown): string {
try {
return JSON.stringify(value, null, 2)
} catch {
return String(value)
}
}