Skip to content

Commit

Permalink
Only set timezone when user setting is a valid timezone (#57850)
Browse files Browse the repository at this point in the history
* Only set timezone when user setting is a valid timezone

Currently we set the global browser timezone based on the user's
advanced settings.  This setting includes a list of timezones and a
non-standard 'Browser' option which can be translated as set the
timezone to my current.  In order to avoid warnings and possible future
errors we only set timezone if it exists in moments list of installed
timezones

Closes #38515

* feedback

* only set timezone if defined

* Update src/core/public/integrations/moment/moment_service.ts

Co-Authored-By: Mikhail Shustov <[email protected]>

* Update src/core/public/integrations/moment/moment_service.ts

Co-Authored-By: Mikhail Shustov <[email protected]>

* Update src/core/public/integrations/moment/moment_service.test.ts

Co-Authored-By: Mikhail Shustov <[email protected]>

* Update src/core/public/integrations/moment/moment_service.test.ts

Co-Authored-By: Mikhail Shustov <[email protected]>

* update test name

Co-authored-by: Mikhail Shustov <[email protected]>
Co-authored-by: Elastic Machine <[email protected]>
  • Loading branch information
3 people committed Mar 20, 2020
1 parent e8a28a9 commit f0059a3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export const momentMock = {
locale: jest.fn(() => 'default-locale'),
tz: {
setDefault: jest.fn(),
zone: jest.fn(
z => [{ name: 'tz1' }, { name: 'tz2' }, { name: 'tz3' }].find(f => z === f.name) || null
),
},
weekdays: jest.fn(() => ['dow1', 'dow2', 'dow3']),
updateLocale: jest.fn(),
Expand Down
20 changes: 20 additions & 0 deletions src/core/public/integrations/moment/moment_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,26 @@ describe('MomentService', () => {
expect(momentMock.updateLocale).toHaveBeenCalledWith('default-locale', { week: { dow: 0 } });
});

it('does not set unknkown zone', async () => {
const tz$ = new BehaviorSubject('timezone/undefined');
const uiSettings = uiSettingsServiceMock.createSetupContract();
uiSettings.get$.mockReturnValueOnce(tz$);

service.start({ uiSettings });
await flushPromises();
expect(momentMock.tz.setDefault).not.toHaveBeenCalled();
});

it('sets timezone when a zone is defined', async () => {
const tz$ = new BehaviorSubject('tz3');
const uiSettings = uiSettingsServiceMock.createSetupContract();
uiSettings.get$.mockReturnValueOnce(tz$);

service.start({ uiSettings });
await flushPromises();
expect(momentMock.tz.setDefault).toHaveBeenCalledWith('tz3');
});

test('updates moment config', async () => {
const tz$ = new BehaviorSubject('tz1');
const dow$ = new BehaviorSubject('dow1');
Expand Down
5 changes: 4 additions & 1 deletion src/core/public/integrations/moment/moment_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export class MomentService implements CoreService {
public async setup() {}

public async start({ uiSettings }: StartDeps) {
const setDefaultTimezone = (tz: string) => moment.tz.setDefault(tz);
const setDefaultTimezone = (tz: string) => {
const zone = moment.tz.zone(tz);
if (zone) moment.tz.setDefault(zone.name);
};
const setStartDayOfWeek = (day: string) => {
const dow = moment.weekdays().indexOf(day);
moment.updateLocale(moment.locale(), { week: { dow } } as any);
Expand Down

0 comments on commit f0059a3

Please sign in to comment.