File size: 3,341 Bytes
a21c316
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { formatDistanceToNow } from 'date-fns';
import { zhCN, zhTW, enUS, ja, tr, vi, ptBR } from 'date-fns/locale';

export function formatRelativeTime(timestamp: number, language: string = 'zh-CN'): string {
    let locale = enUS;
    if (language === 'zh-CN' || language === 'zh') locale = zhCN;
    else if (language === 'zh-TW') locale = zhTW;
    else if (language === 'ja') locale = ja;
    else if (language === 'tr') locale = tr;
    else if (language === 'vi') locale = vi;
    else if (language === 'pt' || language === 'pt-BR') locale = ptBR;

    return formatDistanceToNow(new Date(timestamp * 1000), {
        addSuffix: true,
        locale,
    });
}

export function formatBytes(bytes: number): string {
    if (bytes === 0) return '0 Bytes';

    const k = 1024;
    const sizes = ['Bytes', 'KB', 'MB', 'GB'];
    const i = Math.floor(Math.log(bytes) / Math.log(k));

    return Math.round(bytes / Math.pow(k, i) * 100) / 100 + ' ' + sizes[i];
}

export function getQuotaColor(percentage: number): string {
    if (percentage >= 50) return 'success';
    if (percentage >= 20) return 'warning';
    return 'error';
}

export function formatTimeRemaining(dateStr: string): string {
    const targetDate = new Date(dateStr);
    const now = new Date();
    const diffMs = targetDate.getTime() - now.getTime();

    if (diffMs <= 0) return '0h 0m';

    const diffHrs = Math.floor(diffMs / (1000 * 60 * 60));
    const diffMins = Math.floor((diffMs % (1000 * 60 * 60)) / (1000 * 60));

    if (diffHrs >= 24) {
        const diffDays = Math.floor(diffHrs / 24);
        const remainingHrs = diffHrs % 24;
        return `${diffDays}d ${remainingHrs}h`;
    }

    return `${diffHrs}h ${diffMins}m`;
}

export function getTimeRemainingColor(dateStr: string | undefined): string {
    if (!dateStr) return 'gray';
    const targetDate = new Date(dateStr);
    const now = new Date();
    const diffMs = targetDate.getTime() - now.getTime();

    if (diffMs <= 0) return 'success'; // 已经过期的也算成功(即将重置或已重置)

    const diffHrs = diffMs / (1000 * 60 * 60);

    if (diffHrs < 1) return 'success';   // < 1h: 绿色 (快重置了)
    if (diffHrs < 6) return 'warning';   // 1-6h: 琥珀色 (等待中)
    return 'neutral';                   // > 6h: 灰色 (长等待)
}

export function formatDate(timestamp: string | number | undefined | null): string | null {
    if (!timestamp) return null;
    const date = typeof timestamp === 'number'
        ? new Date(timestamp * 1000)
        : new Date(timestamp);

    if (isNaN(date.getTime())) return null;

    return date.toLocaleString(undefined, {
        year: 'numeric',
        month: '2-digit',
        day: '2-digit',
        hour: '2-digit',
        minute: '2-digit',
        second: '2-digit',
        hour12: false
    });
}

export function formatCompactNumber(num: number): string {
    if (num === 0) return '0';
    if (num < 1000 && num > -1000) return num.toString();

    const units = ['', 'k', 'M', 'G', 'T', 'P'];
    const absNum = Math.abs(num);
    const i = Math.floor(Math.log10(absNum) / 3);
    const value = num / Math.pow(1000, i);

    // Round to 1 decimal place if needed
    const formatted = value.toFixed(Math.abs(value) < 10 && i > 0 ? 1 : 0);
    return `${formatted.replace(/\.0$/, '')}${units[i]}`;
}