Skip to content

Commit

Permalink
feat(extracted-tz-data): add location and geographic area to the extr…
Browse files Browse the repository at this point in the history
…acted timezone data
vespertilian committed May 12, 2018
1 parent 3d01dc4 commit 82f8c0d
Showing 4 changed files with 42 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/extract-tz-data/extract-geographic-area-and-location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export function extractGeographicAreaAndLocation(input: string): {geographicArea: string, location: string} {
const firstSlash = input.indexOf('/');
const geographicArea = input.slice(0, firstSlash);
// + 1 (don't actually copy the first slash)
const location = input.slice(firstSlash + 1, input.length);
return {geographicArea, location}
}
24 changes: 24 additions & 0 deletions src/extract-tz-data/extract-geographic-area.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// From the IANA timezone theory file
// Names normally have the form AREA/LOCATION,
// where AREA is the name of a continent or ocean,
// and LOCATION is the name of a specific location within that region.
// North and South America share the same area, 'America'.
// Typical names are 'Africa/Cairo', 'America/New_York', and 'Pacific/Honolulu'.

import {extractGeographicAreaAndLocation} from './extract-geographic-area-and-location';

describe('extract geographic area and location', () => {
it('should extract geographic area and location from a two parameter tz', () => {
const {geographicArea, location} = extractGeographicAreaAndLocation('Europe/Andorra');

expect(geographicArea).toEqual('Europe');
expect(location).toEqual('Andorra')
});

it('should extract geographic area and location from a three parameter tz', () =>{
const {geographicArea, location} = extractGeographicAreaAndLocation('America/Argentina/Buenos_Aires');

expect(geographicArea).toEqual('America');
expect(location).toEqual('Argentina/Buenos_Aires')
})
});
5 changes: 4 additions & 1 deletion src/extract-tz-data/extract-tz-data.spec.ts
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@ describe('extractTzData', () => {
longitude: { sign: '-', degree: 60, minute: 40, negative: true, second: null, decimal: -60.666667}
},
timezoneName: 'Cameron/Test',
geographicArea: 'Cameron',
location: 'Test',
comments: 'Foo'
};

@@ -53,10 +55,11 @@ describe('extractTzData', () => {
latitude: { sign: '+', degree: 17, minute: 58, second: 5, negative: false, decimal: 17.968056 },
longitude: { sign: '-', degree: 76, minute: 47, second: 36, negative: true, decimal: -76.793333 } },
timezoneName: 'Cameron/TestA',
geographicArea: 'Cameron',
location: 'TestA',
comments: null
};

expect(result.zones[2]).toEqual(extractedTimezone)
})

});
7 changes: 7 additions & 0 deletions src/extract-tz-data/extract-tz-data.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ import * as parseTzdataCoordinate from 'parse-tzdata-coordinate';
import {removeLineBreaks} from '../util/util';
import * as math from 'mathjs'
import {isValidZoneTabRow} from './is-valid-zone-tab-row';
import {extractGeographicAreaAndLocation} from './extract-geographic-area-and-location';

export interface ICoordinates {
latitude: {
@@ -29,6 +30,8 @@ export interface IExtractedTimezone {
countryCodes: string[],
coordinates: ICoordinates
timezoneName: string
geographicArea: string
location: string
comments: string | null
}

@@ -49,10 +52,14 @@ export function extractTzData(zoneData: any, zoneFileName: string): IExtractedTi
.map(zoneData => {
const [countryCodes , coordinates, timezoneName, comments] = zoneData;
const allCodes = countryCodes.split(',');
const {geographicArea, location} = extractGeographicAreaAndLocation(timezoneName);

return {
countryCodes: allCodes,
coordinates: parseCoordinates(coordinates),
timezoneName: removeLineBreaks(timezoneName),
geographicArea: geographicArea,
location: location,
comments: comments || null
}
});

0 comments on commit 82f8c0d

Please sign in to comment.