-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31074 from tienifr/fix/27392
Add `DateTimeFormat` polyfills with timezone abbreviations
- Loading branch information
Showing
3 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type {DateTimeFormatConstructor} from '@formatjs/intl-datetimeformat'; | ||
import DateUtils from '@libs/DateUtils'; | ||
|
||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
const tzLinks: Record<string, string> = { | ||
'Africa/Abidjan': 'Africa/Accra', | ||
CET: 'Europe/Paris', | ||
CST6CDT: 'America/Chicago', | ||
EET: 'Europe/Sofia', | ||
EST: 'America/Cancun', | ||
EST5EDT: 'America/New_York', | ||
'Etc/GMT': 'UTC', | ||
'Etc/UTC': 'UTC', | ||
Factory: 'UTC', | ||
GMT: 'UTC', | ||
HST: 'Pacific/Honolulu', | ||
MET: 'Europe/Paris', | ||
MST: 'America/Phoenix', | ||
MST7MDT: 'America/Denver', | ||
PST8PDT: 'America/Los_Angeles', | ||
WET: 'Europe/Lisbon', | ||
}; | ||
/* eslint-enable @typescript-eslint/naming-convention */ | ||
|
||
export default function () { | ||
// Because JS Engines do not expose default timezone, the polyfill cannot detect local timezone that a browser is in. | ||
// We must manually do this by getting the local timezone before adding polyfill. | ||
let currentTimezone = DateUtils.getCurrentTimezone().selected as string; | ||
if (currentTimezone in tzLinks) { | ||
currentTimezone = tzLinks[currentTimezone]; | ||
} | ||
|
||
require('@formatjs/intl-datetimeformat/polyfill-force'); | ||
require('@formatjs/intl-datetimeformat/locale-data/en'); | ||
require('@formatjs/intl-datetimeformat/locale-data/es'); | ||
require('@formatjs/intl-datetimeformat/add-all-tz'); | ||
|
||
if ('__setDefaultTimeZone' in Intl.DateTimeFormat) { | ||
// eslint-disable-next-line no-underscore-dangle | ||
(Intl.DateTimeFormat as DateTimeFormatConstructor).__setDefaultTimeZone(currentTimezone); | ||
} | ||
} |