Skip to content

Commit

Permalink
fix(find): return all ocean zones for North Pole queries
Browse files Browse the repository at this point in the history
  • Loading branch information
evansiroky committed Apr 9, 2019
1 parent 3b9b3cb commit 62d749c
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 45 deletions.
51 changes: 6 additions & 45 deletions lib/find.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ var Pbf = require('pbf')
var point = require('@turf/helpers').point

var tzData = require('../data/index.json')
const {getTimezoneAtSea, oceanZones} = require('./oceanUtils')

let featureCache = new Cache()

Expand Down Expand Up @@ -45,51 +46,6 @@ var loadFeatures = function (quadPos) {
return geoJson
}

const oceanZones = [
{ tzid: 'Etc/GMT-12', left: 172.5, right: 180 },
{ tzid: 'Etc/GMT-11', left: 157.5, right: 172.5 },
{ tzid: 'Etc/GMT-10', left: 142.5, right: 157.5 },
{ tzid: 'Etc/GMT-9', left: 127.5, right: 142.5 },
{ tzid: 'Etc/GMT-8', left: 112.5, right: 127.5 },
{ tzid: 'Etc/GMT-7', left: 97.5, right: 112.5 },
{ tzid: 'Etc/GMT-6', left: 82.5, right: 97.5 },
{ tzid: 'Etc/GMT-5', left: 67.5, right: 82.5 },
{ tzid: 'Etc/GMT-4', left: 52.5, right: 67.5 },
{ tzid: 'Etc/GMT-3', left: 37.5, right: 52.5 },
{ tzid: 'Etc/GMT-2', left: 22.5, right: 37.5 },
{ tzid: 'Etc/GMT-1', left: 7.5, right: 22.5 },
{ tzid: 'Etc/GMT', left: -7.5, right: 7.5 },
{ tzid: 'Etc/GMT+1', left: -22.5, right: -7.5 },
{ tzid: 'Etc/GMT+2', left: -37.5, right: -22.5 },
{ tzid: 'Etc/GMT+3', left: -52.5, right: -37.5 },
{ tzid: 'Etc/GMT+4', left: -67.5, right: -52.5 },
{ tzid: 'Etc/GMT+5', left: -82.5, right: -67.5 },
{ tzid: 'Etc/GMT+6', left: -97.5, right: -82.5 },
{ tzid: 'Etc/GMT+7', left: -112.5, right: -97.5 },
{ tzid: 'Etc/GMT+8', left: -127.5, right: -112.5 },
{ tzid: 'Etc/GMT+9', left: -142.5, right: -127.5 },
{ tzid: 'Etc/GMT+10', left: -157.5, right: -142.5 },
{ tzid: 'Etc/GMT+11', left: -172.5, right: -157.5 },
{ tzid: 'Etc/GMT+12', left: -180, right: -172.5 }
]

var getTimezoneAtSea = function (lon) {
// coordinates along the 180 longitude should return two zones
if (lon === -180 || lon === 180) {
return ['Etc/GMT+12', 'Etc/GMT-12']
}
const tzs = []
for (var i = 0; i < oceanZones.length; i++) {
var z = oceanZones[i]
if (z.left <= lon && z.right >= lon) {
tzs.push(z.tzid)
} else if (z.right < lon) {
break
}
}
return tzs
}

var getTimezone = function (originalLat, originalLon) {
let lat = parseFloat(originalLat)
let lon = parseFloat(originalLon)
Expand All @@ -108,6 +64,11 @@ var getTimezone = function (originalLat, originalLon) {
throw err
}

// North Pole should return all zones
if (lat === 90) {
return oceanZones.map(zone => zone.tzid)
}

// fix edges of the world
if (lat >= 89.9999) {
lat = 89.9999
Expand Down
47 changes: 47 additions & 0 deletions lib/oceanUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const oceanZones = [
{ tzid: 'Etc/GMT-12', left: 172.5, right: 180 },
{ tzid: 'Etc/GMT-11', left: 157.5, right: 172.5 },
{ tzid: 'Etc/GMT-10', left: 142.5, right: 157.5 },
{ tzid: 'Etc/GMT-9', left: 127.5, right: 142.5 },
{ tzid: 'Etc/GMT-8', left: 112.5, right: 127.5 },
{ tzid: 'Etc/GMT-7', left: 97.5, right: 112.5 },
{ tzid: 'Etc/GMT-6', left: 82.5, right: 97.5 },
{ tzid: 'Etc/GMT-5', left: 67.5, right: 82.5 },
{ tzid: 'Etc/GMT-4', left: 52.5, right: 67.5 },
{ tzid: 'Etc/GMT-3', left: 37.5, right: 52.5 },
{ tzid: 'Etc/GMT-2', left: 22.5, right: 37.5 },
{ tzid: 'Etc/GMT-1', left: 7.5, right: 22.5 },
{ tzid: 'Etc/GMT', left: -7.5, right: 7.5 },
{ tzid: 'Etc/GMT+1', left: -22.5, right: -7.5 },
{ tzid: 'Etc/GMT+2', left: -37.5, right: -22.5 },
{ tzid: 'Etc/GMT+3', left: -52.5, right: -37.5 },
{ tzid: 'Etc/GMT+4', left: -67.5, right: -52.5 },
{ tzid: 'Etc/GMT+5', left: -82.5, right: -67.5 },
{ tzid: 'Etc/GMT+6', left: -97.5, right: -82.5 },
{ tzid: 'Etc/GMT+7', left: -112.5, right: -97.5 },
{ tzid: 'Etc/GMT+8', left: -127.5, right: -112.5 },
{ tzid: 'Etc/GMT+9', left: -142.5, right: -127.5 },
{ tzid: 'Etc/GMT+10', left: -157.5, right: -142.5 },
{ tzid: 'Etc/GMT+11', left: -172.5, right: -157.5 },
{ tzid: 'Etc/GMT+12', left: -180, right: -172.5 }
]

function getTimezoneAtSea (lon) {
// coordinates along the 180 longitude should return two zones
if (lon === -180 || lon === 180) {
return ['Etc/GMT+12', 'Etc/GMT-12']
}
const tzs = []
for (var i = 0; i < oceanZones.length; i++) {
var z = oceanZones[i]
if (z.left <= lon && z.right >= lon) {
tzs.push(z.tzid)
} else if (z.right < lon) {
break
}
}
return tzs
}

module.exports.oceanZones = oceanZones
module.exports.getTimezoneAtSea = getTimezoneAtSea
5 changes: 5 additions & 0 deletions tests/find.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ var assert = require('chai').assert

var geoTz = require('../index.js')
var issueCoords = require('./fixtures/issues.json')
const {oceanZones} = require('../lib/oceanUtils')

process.chdir('/tmp')

Expand Down Expand Up @@ -56,6 +57,10 @@ describe('find tests', function () {
assertTzResultContainsTzs(40, -157.5, ['Etc/GMT+10', 'Etc/GMT+11'])
})

it('should return all ocean timezones for coordinate at the North Pole', function () {
assertTzResultContainsTzs(90, 0, oceanZones.map(zone => zone.tzid))
})

describe('issue cases', function () {
issueCoords.forEach(function (spot) {
const spotDescription = spot.zids
Expand Down

0 comments on commit 62d749c

Please sign in to comment.