Spaces:
Running

File size: 3,275 Bytes
d985d6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/**
 * Timezone utilities - uses client device timezone
 */

/**
 * Get the browser/device timezone
 * Returns IANA timezone string like 'Asia/Bishkek' or 'Europe/Moscow'
 */
export function getClientTimezone(): string {
    try {
        return Intl.DateTimeFormat().resolvedOptions().timeZone
    } catch {
        return 'UTC'
    }
}

/**
 * Get timezone offset in hours from UTC
 * Positive for east of UTC (e.g., +6 for Asia/Bishkek)
 */
export function getTimezoneOffset(): number {
    return -new Date().getTimezoneOffset() / 60
}

/**
 * Format date in client's local timezone
 */
export function formatLocalDate(date: Date | string, formatStr: string = 'dd.MM.yyyy'): string {
    const d = typeof date === 'string' ? new Date(date) : date

    const options: Intl.DateTimeFormatOptions = {}

    if (formatStr.includes('dd') || formatStr.includes('d')) {
        options.day = formatStr.includes('dd') ? '2-digit' : 'numeric'
    }
    if (formatStr.includes('MM') || formatStr.includes('M')) {
        options.month = formatStr.includes('MMMM') ? 'long' : formatStr.includes('MMM') ? 'short' : '2-digit'
    }
    if (formatStr.includes('yyyy') || formatStr.includes('y')) {
        options.year = 'numeric'
    }
    if (formatStr.includes('HH') || formatStr.includes('H')) {
        options.hour = '2-digit'
        options.hour12 = false
    }
    if (formatStr.includes('mm') || formatStr.includes('m')) {
        options.minute = '2-digit'
    }
    if (formatStr.includes('EEEE') || formatStr.includes('EEE')) {
        options.weekday = formatStr.includes('EEEE') ? 'long' : 'short'
    }

    return d.toLocaleString('ru-RU', options)
}

/**
 * Format time in client's local timezone  
 */
export function formatLocalTime(date: Date | string): string {
    const d = typeof date === 'string' ? new Date(date) : date
    return d.toLocaleTimeString('ru-RU', { hour: '2-digit', minute: '2-digit', hour12: false })
}

/**
 * Format full datetime in client's local timezone
 */
export function formatLocalDateTime(date: Date | string): string {
    const d = typeof date === 'string' ? new Date(date) : date
    return d.toLocaleString('ru-RU', {
        day: '2-digit',
        month: 'long',
        year: 'numeric',
        hour: '2-digit',
        minute: '2-digit',
        hour12: false
    })
}

/**
 * Convert local date/time to ISO string for API
 * This preserves the local time when sent to server
 */
export function localToISOString(date: Date): string {
    return date.toISOString()
}

/**
 * Parse ISO string from API to local Date
 */
export function isoToLocalDate(isoString: string): Date {
    return new Date(isoString)
}

/**
 * Get current date/time in client timezone
 */
export function nowLocal(): Date {
    return new Date()
}

/**
 * Check if date is today in client timezone
 */
export function isToday(date: Date | string): boolean {
    const d = typeof date === 'string' ? new Date(date) : date
    const today = new Date()
    return d.toDateString() === today.toDateString()
}

/**
 * Get timezone display name
 */
export function getTimezoneDisplayName(): string {
    const tz = getClientTimezone()
    const offset = getTimezoneOffset()
    const sign = offset >= 0 ? '+' : ''
    return `${tz} (UTC${sign}${offset})`
}