Skip to content

Commit

Permalink
retrieve timezone from DateUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
tienifr committed Jan 3, 2024
1 parent ba1a2d7 commit f9afabe
Showing 1 changed file with 2 additions and 35 deletions.
37 changes: 2 additions & 35 deletions src/libs/IntlPolyfill/polyfillDateTimeFormat.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import {DateTimeFormatConstructor} from '@formatjs/intl-datetimeformat';
import Onyx from 'react-native-onyx';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import {Timezone} from '@src/types/onyx/PersonalDetails';
import DateUtils from '@libs/DateUtils';

/* eslint-disable @typescript-eslint/naming-convention */
const tzLinks: Record<string, string> = {
Expand All @@ -25,40 +22,10 @@ const tzLinks: Record<string, string> = {
};
/* eslint-disable @typescript-eslint/naming-convention */

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.
let currentTimezone = timezone.automatic ? Intl.DateTimeFormat().resolvedOptions().timeZone : timezone.selected;
let currentTimezone = DateUtils.getCurrentTimezone().selected as string;
if (currentTimezone in tzLinks) {
currentTimezone = tzLinks[currentTimezone];
}
Expand Down

0 comments on commit f9afabe

Please sign in to comment.