geo-tz
Version:
A node.js module to find the timezone at specific gps coordinates
57 lines (54 loc) • 2 kB
text/typescript
type OceanZone = {
left: number
right: number
tzid: string
}
export const oceanZones: OceanZone[] = [
{ 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 },
]
/**
* Find the Etc/GMT* timezone name(s) corresponding to the given longitue.
*
* @param lon The longitude to analyze
* @returns An array of strings of TZIDs
*/
export function getTimezoneAtSea(lon: number): string[] {
// coordinates along the 180 longitude should return two zones
if (lon === -180 || lon === 180) {
return ['Etc/GMT+12', 'Etc/GMT-12']
}
const tzs = []
for (let i = 0; i < oceanZones.length; i++) {
const z = oceanZones[i]
if (z.left <= lon && z.right >= lon) {
tzs.push(z.tzid)
} else if (z.right < lon) {
break
}
}
return tzs
}