Skip to content

Commit

Permalink
Include IANA timezone canonical name in TimeZoneInfo (#27591)
Browse files Browse the repository at this point in the history
  • Loading branch information
dprokop committed Sep 17, 2020
1 parent 0e34474 commit 7db050d
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions jest.config.js
@@ -1,3 +1,5 @@
process.env.TZ = 'UTC';

module.exports = {
verbose: false,
transform: {
Expand Down
31 changes: 31 additions & 0 deletions packages/grafana-data/src/datetime/timezones.test.ts
@@ -0,0 +1,31 @@
import { getTimeZoneInfo } from './timezones';
import { setTimeZoneResolver } from './common';

describe('getTimeZoneInfo', () => {
// global timezone is set to UTC, see jest-config.js file

describe('IANA canonical name of the timezone', () => {
it('should resolve for default timezone', () => {
setTimeZoneResolver(() => 'browser');
const result = getTimeZoneInfo('', Date.now());
expect(result?.ianaName).toBe('Africa/Abidjan');
});

it('should resolve for browser timezone', () => {
// global timezone is set to UTC
const result = getTimeZoneInfo('browser', Date.now());
expect(result?.ianaName).toBe('Africa/Abidjan');
});
it('should resolve for utc timezone', () => {
// global timezone is set to UTC
const result = getTimeZoneInfo('utc', Date.now());
expect(result?.ianaName).toBe('UTC');
});

it('should resolve for given timezone', () => {
// global timezone is set to UTC
const result = getTimeZoneInfo('Europe/Warsaw', Date.now());
expect(result?.ianaName).toBe('Europe/Warsaw');
});
});
});
6 changes: 5 additions & 1 deletion packages/grafana-data/src/datetime/timezones.ts
Expand Up @@ -30,6 +30,7 @@ export interface TimeZoneInfo {
countries: TimeZoneCountry[];
abbreviation: string;
offsetInMins: number;
ianaName: string;
}

export interface GroupedTimeZones {
Expand Down Expand Up @@ -98,6 +99,7 @@ const mapInternal = (zone: string, timestamp: number): TimeZoneInfo | undefined
case InternalTimeZones.utc: {
return {
name: 'Coordinated Universal Time',
ianaName: 'UTC',
zone,
countries: [],
abbreviation: 'UTC, GMT',
Expand All @@ -115,6 +117,7 @@ const mapInternal = (zone: string, timestamp: number): TimeZoneInfo | undefined
abbreviation: '',
offsetInMins: 0,
...info,
ianaName: (info as TimeZoneInfo).ianaName,
name: 'Default',
zone,
};
Expand All @@ -130,6 +133,7 @@ const mapInternal = (zone: string, timestamp: number): TimeZoneInfo | undefined
offsetInMins: new Date().getTimezoneOffset(),
...info,
name: 'Browser Time',
ianaName: (info as TimeZoneInfo).ianaName,
zone,
};
}
Expand All @@ -148,13 +152,13 @@ const abbrevationWithoutOffset = (abbrevation: string): string => {

const mapToInfo = (timeZone: TimeZone, timestamp: number): TimeZoneInfo | undefined => {
const momentTz = moment.tz.zone(timeZone);

if (!momentTz) {
return undefined;
}

return {
name: timeZone,
ianaName: momentTz.name,
zone: timeZone,
countries: countriesByTimeZone[timeZone] ?? [],
abbreviation: abbrevationWithoutOffset(momentTz.abbr(timestamp)),
Expand Down

0 comments on commit 7db050d

Please sign in to comment.