forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
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 Expensify#29113 from tienifr/fix/27927
Add `DateTimeFormat` polyfill
- Loading branch information
Showing
3 changed files
with
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
import polyfillNumberFormat from './polyfillNumberFormat'; | ||
import IntlPolyfill from './types'; | ||
import polyfillDateTimeFormat from './polyfillDateTimeFormat'; | ||
|
||
/** | ||
* Polyfill the Intl API if the ICU version is old. | ||
* This ensures that the currency data is consistent across platforms and browsers. | ||
*/ | ||
const intlPolyfill: IntlPolyfill = () => { | ||
// Just need to polyfill Intl.NumberFormat for web based platforms | ||
polyfillNumberFormat(); | ||
require('@formatjs/intl-datetimeformat'); | ||
polyfillDateTimeFormat(); | ||
}; | ||
export default intlPolyfill; |
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,52 @@ | ||
import Onyx from 'react-native-onyx'; | ||
import ONYXKEYS from '../../ONYXKEYS'; | ||
import {Timezone} from '../../types/onyx/PersonalDetails'; | ||
import CONST from '../../CONST'; | ||
|
||
let currentUserAccountID: number | undefined; | ||
Onyx.connect({ | ||
key: ONYXKEYS.SESSION, | ||
callback: (val) => { | ||
// When signed out, val is undefined | ||
if (!val) { | ||
return; | ||
} | ||
|
||
currentUserAccountID = val.accountID; | ||
}, | ||
}); | ||
|
||
let timezone: Required<Timezone> = CONST.DEFAULT_TIME_ZONE; | ||
Onyx.connect({ | ||
key: ONYXKEYS.PERSONAL_DETAILS_LIST, | ||
callback: (value) => { | ||
if (!currentUserAccountID) { | ||
return; | ||
} | ||
|
||
const personalDetailsTimezone = value?.[currentUserAccountID]?.timezone; | ||
|
||
timezone = { | ||
selected: personalDetailsTimezone?.selected ?? CONST.DEFAULT_TIME_ZONE.selected, | ||
automatic: personalDetailsTimezone?.automatic ?? CONST.DEFAULT_TIME_ZONE.automatic, | ||
}; | ||
}, | ||
}); | ||
|
||
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. | ||
const currentTimezone = timezone.automatic ? Intl.DateTimeFormat().resolvedOptions().timeZone : timezone.selected; | ||
|
||
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 @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
// eslint-disable-next-line no-underscore-dangle | ||
Intl.DateTimeFormat.__setDefaultTimeZone(currentTimezone); | ||
} | ||
} |