Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/libs/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type CustomStatusTypes = ValueOf<typeof CONST.CUSTOM_STATUS_TYPES>;
type Locale = ValueOf<typeof CONST.LOCALES>;
type WeekDay = 0 | 1 | 2 | 3 | 4 | 5 | 6;

const TIMEZONE_UPDATE_THROTTLE_MINUTES = 5;

let currentUserAccountID: number | undefined;
Onyx.connect({
key: ONYXKEYS.SESSION,
Expand Down Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions tests/unit/DateUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down