|
| 1 | +/** |
| 2 | + * Timezone utilities for handling user timezone detection and date conversions |
| 3 | + */ |
| 4 | + |
| 5 | +export interface TimezoneInfo { |
| 6 | + timezone: string; |
| 7 | + detected: boolean; |
| 8 | + source: 'explicit' | 'header' | 'cloudflare' | 'accept-language' | 'default'; |
| 9 | +} |
| 10 | + |
| 11 | +/** |
| 12 | + * Detect user timezone from various sources |
| 13 | + */ |
| 14 | +export function detectUserTimezone(headers: Headers, explicitTimezone?: string): TimezoneInfo { |
| 15 | + // Use explicit timezone if provided and valid |
| 16 | + if (explicitTimezone) { |
| 17 | + try { |
| 18 | + Intl.DateTimeFormat(undefined, { timeZone: explicitTimezone }); |
| 19 | + return { |
| 20 | + timezone: explicitTimezone, |
| 21 | + detected: true, |
| 22 | + source: 'explicit' |
| 23 | + }; |
| 24 | + } catch { |
| 25 | + // Invalid timezone, fall through to header detection |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + // Try to get timezone from various headers |
| 30 | + const timezoneHeader = headers.get('x-timezone') || |
| 31 | + headers.get('timezone') || |
| 32 | + headers.get('x-user-timezone'); |
| 33 | + |
| 34 | + if (timezoneHeader) { |
| 35 | + try { |
| 36 | + // Validate timezone |
| 37 | + Intl.DateTimeFormat(undefined, { timeZone: timezoneHeader }); |
| 38 | + return { |
| 39 | + timezone: timezoneHeader, |
| 40 | + detected: true, |
| 41 | + source: 'header' |
| 42 | + }; |
| 43 | + } catch { |
| 44 | + // Invalid timezone, fall through to other methods |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Try to infer from CloudFlare headers (if using CloudFlare) |
| 49 | + const cfTimezone = headers.get('cf-timezone'); |
| 50 | + if (cfTimezone) { |
| 51 | + try { |
| 52 | + Intl.DateTimeFormat(undefined, { timeZone: cfTimezone }); |
| 53 | + return { |
| 54 | + timezone: cfTimezone, |
| 55 | + detected: true, |
| 56 | + source: 'cloudflare' |
| 57 | + }; |
| 58 | + } catch { |
| 59 | + // Invalid timezone |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Try to extract from Accept-Language header |
| 64 | + const acceptLanguage = headers.get('accept-language'); |
| 65 | + if (acceptLanguage) { |
| 66 | + // Look for timezone info in accept-language (some browsers include it) |
| 67 | + const timezoneMatch = acceptLanguage.match(/timezone=([^,;]+)/i); |
| 68 | + if (timezoneMatch) { |
| 69 | + try { |
| 70 | + Intl.DateTimeFormat(undefined, { timeZone: timezoneMatch[1] }); |
| 71 | + return { |
| 72 | + timezone: timezoneMatch[1], |
| 73 | + detected: true, |
| 74 | + source: 'accept-language' |
| 75 | + }; |
| 76 | + } catch { |
| 77 | + // Invalid timezone |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + // Default to UTC if no timezone detected |
| 83 | + return { |
| 84 | + timezone: 'UTC', |
| 85 | + detected: false, |
| 86 | + source: 'default' |
| 87 | + }; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * Convert date to user's timezone |
| 92 | + */ |
| 93 | +export function convertToUserTimezone(date: string, timezone: string): string { |
| 94 | + try { |
| 95 | + const utcDate = new Date(`${date}T00:00:00Z`); |
| 96 | + const userDate = new Date(utcDate.toLocaleString('en-US', { timeZone: timezone })); |
| 97 | + return userDate.toISOString().split('T')[0]; |
| 98 | + } catch { |
| 99 | + // If timezone conversion fails, return original date |
| 100 | + return date; |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +/** |
| 105 | + * Adjust date range for user timezone to ensure we capture all relevant data |
| 106 | + */ |
| 107 | +export function adjustDateRangeForTimezone( |
| 108 | + startDate: string, |
| 109 | + endDate: string, |
| 110 | + timezone: string |
| 111 | +): { startDate: string, endDate: string } { |
| 112 | + if (timezone === 'UTC') { |
| 113 | + return { startDate, endDate }; |
| 114 | + } |
| 115 | + |
| 116 | + try { |
| 117 | + // Convert start date to user timezone (might need to go back a day) |
| 118 | + const startUTC = new Date(`${startDate}T00:00:00Z`); |
| 119 | + const startInUserTZ = new Date(startUTC.toLocaleString('en-US', { timeZone: timezone })); |
| 120 | + |
| 121 | + // Convert end date to user timezone (might need to go forward a day) |
| 122 | + const endUTC = new Date(`${endDate}T23:59:59Z`); |
| 123 | + const endInUserTZ = new Date(endUTC.toLocaleString('en-US', { timeZone: timezone })); |
| 124 | + |
| 125 | + // Adjust the range to ensure we capture all data for the user's timezone |
| 126 | + const adjustedStart = new Date(startInUserTZ.getTime() - 24 * 60 * 60 * 1000); // Go back 1 day |
| 127 | + const adjustedEnd = new Date(endInUserTZ.getTime() + 24 * 60 * 60 * 1000); // Go forward 1 day |
| 128 | + |
| 129 | + return { |
| 130 | + startDate: adjustedStart.toISOString().split('T')[0], |
| 131 | + endDate: adjustedEnd.toISOString().split('T')[0] |
| 132 | + }; |
| 133 | + } catch { |
| 134 | + // If timezone conversion fails, return original dates |
| 135 | + return { startDate, endDate }; |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +/** |
| 140 | + * Validate timezone string |
| 141 | + */ |
| 142 | +export function isValidTimezone(timezone: string): boolean { |
| 143 | + try { |
| 144 | + Intl.DateTimeFormat(undefined, { timeZone: timezone }); |
| 145 | + return true; |
| 146 | + } catch { |
| 147 | + return false; |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +/** |
| 152 | + * Get current date in user's timezone |
| 153 | + */ |
| 154 | +export function getCurrentDateInTimezone(timezone: string): string { |
| 155 | + try { |
| 156 | + const now = new Date(); |
| 157 | + const userDate = new Date(now.toLocaleString('en-US', { timeZone: timezone })); |
| 158 | + return userDate.toISOString().split('T')[0]; |
| 159 | + } catch { |
| 160 | + // If timezone conversion fails, return UTC date |
| 161 | + return new Date().toISOString().split('T')[0]; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +/** |
| 166 | + * Convert timestamp to user's timezone |
| 167 | + */ |
| 168 | +export function convertTimestampToUserTimezone(timestamp: number, timezone: string): Date { |
| 169 | + try { |
| 170 | + const utcDate = new Date(timestamp); |
| 171 | + return new Date(utcDate.toLocaleString('en-US', { timeZone: timezone })); |
| 172 | + } catch { |
| 173 | + // If timezone conversion fails, return original date |
| 174 | + return new Date(timestamp); |
| 175 | + } |
| 176 | +} |
0 commit comments