(timezone: string, date: Date = new Date())
| 5 | * @returns A simplified timezone string (e.g., "PST" instead of "America/Los_Angeles") |
| 6 | */ |
| 7 | export function getTimezoneAbbreviation(timezone: string, date: Date = new Date()): string { |
| 8 | if (timezone === 'UTC') return 'UTC' |
| 9 | |
| 10 | const timezoneMap: Record<string, { standard: string; daylight: string }> = { |
| 11 | 'America/Los_Angeles': { standard: 'PST', daylight: 'PDT' }, |
| 12 | 'America/Denver': { standard: 'MST', daylight: 'MDT' }, |
| 13 | 'America/Chicago': { standard: 'CST', daylight: 'CDT' }, |
| 14 | 'America/New_York': { standard: 'EST', daylight: 'EDT' }, |
| 15 | 'Europe/London': { standard: 'GMT', daylight: 'BST' }, |
| 16 | 'Europe/Paris': { standard: 'CET', daylight: 'CEST' }, |
| 17 | 'Asia/Tokyo': { standard: 'JST', daylight: 'JST' }, // Japan doesn't use DST |
| 18 | 'Australia/Sydney': { standard: 'AEST', daylight: 'AEDT' }, |
| 19 | 'Asia/Singapore': { standard: 'SGT', daylight: 'SGT' }, // Singapore doesn't use DST |
| 20 | } |
| 21 | |
| 22 | if (timezone in timezoneMap) { |
| 23 | const januaryDate = new Date(date.getFullYear(), 0, 1) |
| 24 | const julyDate = new Date(date.getFullYear(), 6, 1) |
| 25 | |
| 26 | const januaryFormatter = new Intl.DateTimeFormat('en-US', { |
| 27 | timeZone: timezone, |
| 28 | timeZoneName: 'short', |
| 29 | }) |
| 30 | |
| 31 | const julyFormatter = new Intl.DateTimeFormat('en-US', { |
| 32 | timeZone: timezone, |
| 33 | timeZoneName: 'short', |
| 34 | }) |
| 35 | |
| 36 | const isDSTObserved = januaryFormatter.format(januaryDate) !== julyFormatter.format(julyDate) |
| 37 | |
| 38 | if (isDSTObserved) { |
| 39 | const currentFormatter = new Intl.DateTimeFormat('en-US', { |
| 40 | timeZone: timezone, |
| 41 | timeZoneName: 'short', |
| 42 | }) |
| 43 | |
| 44 | const isDST = currentFormatter.format(date) !== januaryFormatter.format(januaryDate) |
| 45 | return isDST ? timezoneMap[timezone].daylight : timezoneMap[timezone].standard |
| 46 | } |
| 47 | |
| 48 | return timezoneMap[timezone].standard |
| 49 | } |
| 50 | |
| 51 | return timezone |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Format a date into a human-readable format |
no test coverage detected