diff --git a/src/libs/DateUtils.ts b/src/libs/DateUtils.ts index 6b43a549256d..2de905ff6047 100644 --- a/src/libs/DateUtils.ts +++ b/src/libs/DateUtils.ts @@ -47,6 +47,8 @@ type CustomStatusTypes = ValueOf; type Locale = ValueOf; type WeekDay = 0 | 1 | 2 | 3 | 4 | 5 | 6; +const TIMEZONE_UPDATE_THROTTLE_MINUTES = 5; + let currentUserAccountID: number | undefined; Onyx.connect({ key: ONYXKEYS.SESSION, @@ -352,12 +354,12 @@ function getDaysOfWeek(preferredLocale: Locale): string[] { return daysOfWeek.map((date) => format(date, 'eeee')); } -// Used to throttle updates to the timezone when necessary -let lastUpdatedTimezoneTime = new Date(); +// Used to throttle updates to the timezone when necessary. Initialize outside the throttle window so it's updated the first time. +let lastUpdatedTimezoneTime = subMinutes(new Date(), TIMEZONE_UPDATE_THROTTLE_MINUTES + 1); function canUpdateTimezone(): boolean { const currentTime = new Date(); - const fiveMinutesAgo = subMinutes(currentTime, 5); + const fiveMinutesAgo = subMinutes(currentTime, TIMEZONE_UPDATE_THROTTLE_MINUTES); // Compare the last updated time with five minutes ago return isBefore(lastUpdatedTimezoneTime, fiveMinutesAgo); } diff --git a/tests/unit/DateUtilsTest.ts b/tests/unit/DateUtilsTest.ts index f3abed2e3372..52ef39ed4da5 100644 --- a/tests/unit/DateUtilsTest.ts +++ b/tests/unit/DateUtilsTest.ts @@ -132,6 +132,7 @@ describe('DateUtils', () => { it('canUpdateTimezone should return false when lastUpdatedTimezoneTime is less than 5 minutes ago', () => { // Use fake timers to control the current time jest.useFakeTimers(); + DateUtils.setTimezoneUpdated(); jest.setSystemTime(addMinutes(new Date(), 4)); const isUpdateTimezoneAllowed = DateUtils.canUpdateTimezone(); expect(isUpdateTimezoneAllowed).toBe(false);