Comparing version 2.2.1 to 4.0.0
149
index.js
@@ -31,14 +31,2 @@ var cacheHelpers = require("cache-helpers"), | ||
function stringifyOffset(t) { | ||
var abs = Math.abs(t) | ||
return String.fromCharCode.apply(null, [ | ||
t > 0 ? 45 : 43, | ||
48 + Math.floor(abs / 600) % 10, | ||
48 + Math.floor(abs / 60) % 10, | ||
48 + Math.floor(abs / 10) % 6, | ||
48 + abs % 10 | ||
]) | ||
} | ||
function pointInZone(lat, lon, bounds, polygons) { | ||
@@ -83,3 +71,3 @@ lat = Math.round((lat + 90) * 65535 / 180) | ||
function getTimezone(lat, lon, callback) { | ||
module.exports = function(lat, lon, callback) { | ||
return cacheZones(function(err, zones) { | ||
@@ -104,8 +92,2 @@ if(err) | ||
now.setTimezone(tzid) | ||
return callback(null, util.format( | ||
"%s (%s, %s)", | ||
tzid, | ||
now.getTimezoneAbbr(), | ||
stringifyOffset(now.getTimezoneOffset()) | ||
)) | ||
} | ||
@@ -119,2 +101,8 @@ | ||
} | ||
return callback(null, { | ||
tzid: tzid, | ||
abbr: now.getTimezoneAbbr(), | ||
offset: now.getTimezoneOffset() / -60 | ||
}) | ||
} | ||
@@ -130,102 +118,29 @@ } | ||
switch(Math.round((lon + 180) / 15)) { | ||
case 0: return callback(null, "Etc/GMT+12 (GMT+12, -1200)") | ||
case 1: return callback(null, "Etc/GMT+11 (GMT+11, -1100)") | ||
case 2: return callback(null, "Etc/GMT+10 (GMT+10, -1000)") | ||
case 3: return callback(null, "Etc/GMT+9 (GMT+9, -0900)") | ||
case 4: return callback(null, "Etc/GMT+8 (GMT+8, -0800)") | ||
case 5: return callback(null, "Etc/GMT+7 (GMT+7, -0700)") | ||
case 6: return callback(null, "Etc/GMT+6 (GMT+6, -0600)") | ||
case 7: return callback(null, "Etc/GMT+5 (GMT+5, -0500)") | ||
case 8: return callback(null, "Etc/GMT+4 (GMT+4, -0400)") | ||
case 9: return callback(null, "Etc/GMT+3 (GMT+3, -0300)") | ||
case 10: return callback(null, "Etc/GMT+2 (GMT+2, -0200)") | ||
case 11: return callback(null, "Etc/GMT+1 (GMT+1, -0100)") | ||
case 12: return callback(null, "Etc/GMT (GMT, +0000)") | ||
case 13: return callback(null, "Etc/GMT-1 (GMT-1, +0100)") | ||
case 14: return callback(null, "Etc/GMT-2 (GMT-2, +0200)") | ||
case 15: return callback(null, "Etc/GMT-3 (GMT-3, +0300)") | ||
case 16: return callback(null, "Etc/GMT-4 (GMT-4, +0400)") | ||
case 17: return callback(null, "Etc/GMT-5 (GMT-5, +0500)") | ||
case 18: return callback(null, "Etc/GMT-6 (GMT-6, +0600)") | ||
case 19: return callback(null, "Etc/GMT-7 (GMT-7, +0700)") | ||
case 20: return callback(null, "Etc/GMT-8 (GMT-8, +0800)") | ||
case 21: return callback(null, "Etc/GMT-9 (GMT-9, +0900)") | ||
case 22: return callback(null, "Etc/GMT-10 (GMT-10, +1000)") | ||
case 23: return callback(null, "Etc/GMT-11 (GMT-11, +1100)") | ||
case 24: return callback(null, "Etc/GMT-12 (GMT-12, +1200)") | ||
case 0: return callback(null, {tzid: "Etc/GMT+12", offset: -12}) | ||
case 1: return callback(null, {tzid: "Etc/GMT+11", offset: -11}) | ||
case 2: return callback(null, {tzid: "Etc/GMT+10", offset: -10}) | ||
case 3: return callback(null, {tzid: "Etc/GMT+9", offset: -9}) | ||
case 4: return callback(null, {tzid: "Etc/GMT+8", offset: -8}) | ||
case 5: return callback(null, {tzid: "Etc/GMT+7", offset: -7}) | ||
case 6: return callback(null, {tzid: "Etc/GMT+6", offset: -6}) | ||
case 7: return callback(null, {tzid: "Etc/GMT+5", offset: -5}) | ||
case 8: return callback(null, {tzid: "Etc/GMT+4", offset: -4}) | ||
case 9: return callback(null, {tzid: "Etc/GMT+3", offset: -3}) | ||
case 10: return callback(null, {tzid: "Etc/GMT+2", offset: -2}) | ||
case 11: return callback(null, {tzid: "Etc/GMT+1", offset: -1}) | ||
case 12: return callback(null, {tzid: "Etc/GMT", offset: 0}) | ||
case 13: return callback(null, {tzid: "Etc/GMT-1", offset: 1}) | ||
case 14: return callback(null, {tzid: "Etc/GMT-2", offset: 2}) | ||
case 15: return callback(null, {tzid: "Etc/GMT-3", offset: 3}) | ||
case 16: return callback(null, {tzid: "Etc/GMT-4", offset: 4}) | ||
case 17: return callback(null, {tzid: "Etc/GMT-5", offset: 5}) | ||
case 18: return callback(null, {tzid: "Etc/GMT-6", offset: 6}) | ||
case 19: return callback(null, {tzid: "Etc/GMT-7", offset: 7}) | ||
case 20: return callback(null, {tzid: "Etc/GMT-8", offset: 8}) | ||
case 21: return callback(null, {tzid: "Etc/GMT-9", offset: 9}) | ||
case 22: return callback(null, {tzid: "Etc/GMT-10", offset: 10}) | ||
case 23: return callback(null, {tzid: "Etc/GMT-11", offset: 11}) | ||
case 24: return callback(null, {tzid: "Etc/GMT-12", offset: 12}) | ||
} | ||
}) | ||
} | ||
function getTzidFromString(str) { | ||
return str.slice(0, str.indexOf(" ")) | ||
} | ||
function getOffsetFromString(str) { | ||
var len = str.length, | ||
dir = 44 - str.charCodeAt(len - 6), | ||
hourTens = str.charCodeAt(len - 5) - 48, | ||
hourOnes = str.charCodeAt(len - 4) - 48, | ||
minuteTens = str.charCodeAt(len - 3) - 48, | ||
minuteOnes = str.charCodeAt(len - 2) - 48 | ||
return dir * (hourTens * 10 + hourOnes + minuteTens / 6 + minuteOnes / 60) | ||
} | ||
function parseTimestamp(timestamp, timezone) { | ||
var offset = getOffsetFromString(timezone), | ||
date = new Date((timestamp + offset * 3600) * 1000), | ||
year = date.getUTCFullYear(), | ||
month = date.getUTCMonth(), | ||
day = date.getUTCDate(), | ||
hour = date.getUTCHours(), | ||
minute = date.getUTCMinutes(), | ||
second = date.getUTCSeconds(), | ||
dayOfYear = day | ||
switch(month) { | ||
case 0: dayOfYear += 0; break | ||
case 1: dayOfYear += 31; break | ||
case 2: dayOfYear += 59; break | ||
case 3: dayOfYear += 90; break | ||
case 4: dayOfYear += 120; break | ||
case 5: dayOfYear += 151; break | ||
case 6: dayOfYear += 181; break | ||
case 7: dayOfYear += 212; break | ||
case 8: dayOfYear += 243; break | ||
case 9: dayOfYear += 273; break | ||
case 10: dayOfYear += 304; break | ||
case 11: dayOfYear += 334; break | ||
} | ||
var secOfYear = second + minute * 60 + hour * 3600 + dayOfYear * 86400 | ||
/* FIXME: The consequences of the above is that Feb 29 is considered the same | ||
* day as March 1st. Oh well? */ | ||
return { | ||
year: year, | ||
month: month, | ||
day: day, | ||
hour: hour, | ||
minute: minute, | ||
second: second, | ||
dayOfYear: dayOfYear, | ||
hourOfYear: Math.round(secOfYear / 3600), | ||
offset: offset | ||
} | ||
} | ||
function getTimezoneOffset(lat, lon, callback) { | ||
return getTimezone(lat, lon, function(err, str) { | ||
if(err) | ||
return callback(err) | ||
return callback(null, getOffsetFromString(str)) | ||
}) | ||
} | ||
exports.getTimezone = getTimezone | ||
exports.getOffsetFromString = getOffsetFromString | ||
exports.getTimezoneOffset = getTimezoneOffset | ||
exports.parseTimestamp = parseTimestamp |
{ | ||
"name" : "tz-lookup", | ||
"version" : "2.2.1", | ||
"description" : "time zone lookup", | ||
"keywords" : [ | ||
"name": "tz-lookup", | ||
"version": "4.0.0", | ||
"description": "time zone lookup", | ||
"keywords": [ | ||
"tz", | ||
@@ -10,17 +10,20 @@ "timezone", | ||
], | ||
"author" : { | ||
"name" : "The Dark Sky Company", | ||
"email" : "devsupport@darkskyapp.com" | ||
"author": { | ||
"name": "The Dark Sky Company", | ||
"email": "devsupport@darkskyapp.com" | ||
}, | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "git://github.com/darkskyapp/tz-lookup.git" | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/darkskyapp/tz-lookup.git" | ||
}, | ||
"dependencies" : { | ||
"dependencies": { | ||
"cache-helpers": "1.3.x", | ||
"time" : "0.8.x" | ||
"time": "0.8.x" | ||
}, | ||
"devDependencies" : { | ||
"mocha" : "1.6.x" | ||
} | ||
"devDependencies": { | ||
"mocha": "1.6.x" | ||
}, | ||
"readme": "tz-lookup\n=========\n\nThis is a little module that allows you to look up the current time zone of a\nlocation, given it's latitude and longitude. I wrote it because the existing\nNode module that does this (`tzwhere`) was far too slow to be useful in a\nproduction setting. This module attempts to ameliorate that.\n\nUsage\n-----\n\nTo install:\n\n npm install tz-lookup\n\nTo use:\n\n > var tz = require(\"tz-lookup\")\n > tz.getTimezone(42.7235, -73.6931, function(err, string) {\n > console.log(string)\n > })\n \"America/New_York (EDT, -0400)\"\n\nThe data file is only loaded on-demand, so the first call to `getTimezone()`\nwill take a while, but subsequent calls will go quite quickly.\n\nIf you only want the offset (in hours), you may call the similar method\n`getTimezoneOffset()`. Same arguments, different return value.\n", | ||
"_id": "tz-lookup@2.2.1", | ||
"_from": "tz-lookup@2.2.x" | ||
} |
@@ -18,12 +18,9 @@ tz-lookup | ||
> var tz = require("tz-lookup") | ||
> tz.getTimezone(42.7235, -73.6931, function(err, string) { | ||
> console.log(string) | ||
> var tzLookup = require("tz-lookup") | ||
> tzLookup(42.7235, -73.6931, function(err, tz) { | ||
> console.log(tz) | ||
> }) | ||
"America/New_York (EDT, -0400)" | ||
{ tzid: "America/New_York", abbr: "EDT", offset: -4 } | ||
The data file is only loaded on-demand, so the first call to `getTimezone()` | ||
The data file is only loaded on-demand, so the first call to `tzLookup()` | ||
will take a while, but subsequent calls will go quite quickly. | ||
If you only want the offset (in hours), you may call the similar method | ||
`getTimezoneOffset()`. Same arguments, different return value. |
132
test.js
@@ -1,37 +0,24 @@ | ||
var assert = require("assert"), | ||
util = require("util"), | ||
tz = require("./") | ||
var assert = require("assert"), | ||
util = require("util"), | ||
tzLookup = require("./") | ||
function check(lat, lon, opt1, opt2, offset1, offset2) { | ||
function check(lat, lon, tzid, offset1, offset2) { | ||
it( | ||
util.format("should give the right timezone string for %d,%d", lat, lon), | ||
util.format("should give the right tzid and offset for %d,%d", lat, lon), | ||
function(done) { | ||
return tz.getTimezone(lat, lon, function(err, str) { | ||
return tzLookup(lat, lon, function(err, tz) { | ||
if(err) | ||
return done(err) | ||
if(opt2) { | ||
assert( | ||
str === opt1 || str === opt2, | ||
util.format( | ||
"expected \"%s\" to equal \"%s\" or \"%s\"", | ||
str, | ||
opt1, | ||
opt2 | ||
) | ||
) | ||
} | ||
assert.ok(tz.hasOwnProperty("tzid"), "expected object to have property \"tzid\"") | ||
assert.ok(tz.hasOwnProperty("offset"), "expected object to have property \"offset\"") | ||
else { | ||
assert.equal(str, opt1) | ||
} | ||
assert.equal(tzid, tz.tzid) | ||
var offset = tz.getOffsetFromString(str) | ||
if(offset2) { | ||
if(offset2) | ||
assert( | ||
offset === offset1 || offset === offset2, | ||
tz.offset === offset1 || tz.offset === offset2, | ||
util.format( | ||
"expected \"%s\" to equal \"%s\" or \"%s\"", | ||
offset, | ||
"expected %d to equal either %d or %d", | ||
tz.offset, | ||
offset1, | ||
@@ -41,7 +28,5 @@ offset2 | ||
) | ||
} | ||
else { | ||
assert.equal(offset, offset1) | ||
} | ||
else | ||
assert.equal(tz.offset, offset1) | ||
@@ -52,72 +37,25 @@ done() | ||
) | ||
it( | ||
util.format("should give the right timezone offset for %d,%d", lat, lon), | ||
function(done) { | ||
return tz.getTimezoneOffset(lat, lon, function(err, offset) { | ||
if(err) | ||
return done(err) | ||
if(offset2) { | ||
assert( | ||
offset === offset1 || offset === offset2, | ||
util.format( | ||
"expected \"%s\" to equal \"%s\" or \"%s\"", | ||
offset, | ||
offset1, | ||
offset2 | ||
) | ||
) | ||
} | ||
else { | ||
assert.equal(offset, offset1) | ||
} | ||
done() | ||
}) | ||
} | ||
) | ||
} | ||
describe("tz", function() { | ||
check(40.709176, -74.015064, "America/New_York (EST, -0500)", "America/New_York (EDT, -0400)", -5, -4) | ||
check(42.366822, -71.054592, "America/New_York (EST, -0500)", "America/New_York (EDT, -0400)", -5, -4) | ||
check(41.89763, -87.620516, "America/Chicago (CST, -0600)", "America/Chicago (CDT, -0500)", -6, -5) | ||
check(47.689697,-122.40234, "America/Los_Angeles (PST, -0800)", "America/Los_Angeles (PDT, -0700)", -8, -7) | ||
check(42.7235, -73.6931, "America/New_York (EST, -0500)", "America/New_York (EDT, -0400)", -5, -4) | ||
check(42.5807, -83.0223, "America/Detroit (EST, -0500)", "America/Detroit (EDT, -0400)", -5, -4) | ||
check(36.8381, -84.8500, "America/Kentucky/Monticello (EST, -0500)", "America/Kentucky/Monticello (EDT, -0400)", -5, -4) | ||
check(40.1674, -85.3583, "America/Indiana/Indianapolis (EST, -0500)", "America/Indiana/Indianapolis (EDT, -0400)", -5, -4) | ||
check(37.9643, -86.7453, "America/Indiana/Tell_City (CST, -0600)", "America/Indiana/Tell_City (CDT, -0500)", -6, -5) | ||
check(38.6043, -90.2417, "America/Chicago (CST, -0600)", "America/Chicago (CDT, -0500)", -6, -5) | ||
check(41.1591, -104.8261, "America/Denver (MST, -0700)", "America/Denver (MDT, -0600)", -7, -6) | ||
check(35.1991, -111.6348, "America/Phoenix (MST, -0700)", null, -7) | ||
check(43.1432, -115.6750, "America/Boise (MST, -0700)", "America/Boise (MDT, -0600)", -7, -6) | ||
check(47.5886, -122.3382, "America/Los_Angeles (PST, -0800)", "America/Los_Angeles (PDT, -0700)", -8, -7) | ||
check(58.3168, -134.4397, "America/Juneau (AKST, -0900)", "America/Juneau (AKDT, -0800)", -9, -8) | ||
check(21.4381, -158.0493, "Pacific/Honolulu (HST, -1000)", null, -10) | ||
check(42.7000, -80.0000, "Etc/GMT+5 (GMT+5, -0500)", null, -5) | ||
check(51.003616, -114.016113, "America/Edmonton (MST, -0700)", "America/Edmonton (MDT, -0600)", -7, -6) | ||
check(-16.49649, -68.170166, "America/La_Paz (BOT, -0400)", null, -4) | ||
check(-31.936935, 115.845337, "Australia/Perth (WST, +0800)", null, 8) | ||
describe("parseTimestamp", function() { | ||
it("should return an object given a timezone and a unix timestamp", function() { | ||
assert.deepEqual( | ||
tz.parseTimestamp(1350319948, "America/New_York (EDT, -0400)"), | ||
{ | ||
year: 2012, | ||
month: 9, | ||
day: 15, | ||
hour: 12, | ||
minute: 52, | ||
second: 28, | ||
dayOfYear: 288, | ||
hourOfYear: 6925, | ||
offset: -4 | ||
} | ||
) | ||
}) | ||
}) | ||
check( 40.7092, -74.01506, "America/New_York", -5, -4) | ||
check( 42.3668, -71.05459, "America/New_York", -5, -4) | ||
check( 41.8976, -87.62052, "America/Chicago", -6, -5) | ||
check( 47.6897, -122.4023, "America/Los_Angeles", -8, -7) | ||
check( 42.7235, -73.6931, "America/New_York", -5, -4) | ||
check( 42.5807, -83.0223, "America/Detroit", -5, -4) | ||
check( 36.8381, -84.8500, "America/Kentucky/Monticello", -5, -4) | ||
check( 40.1674, -85.3583, "America/Indiana/Indianapolis", -5, -4) | ||
check( 37.9643, -86.7453, "America/Indiana/Tell_City", -6, -5) | ||
check( 38.6043, -90.2417, "America/Chicago", -6, -5) | ||
check( 41.1591, -104.8261, "America/Denver", -7, -6) | ||
check( 35.1991, -111.6348, "America/Phoenix", -7) | ||
check( 43.1432, -115.6750, "America/Boise", -7, -6) | ||
check( 47.5886, -122.3382, "America/Los_Angeles", -8, -7) | ||
check( 58.3168, -134.4397, "America/Juneau", -9, -8) | ||
check( 21.4381, -158.0493, "Pacific/Honolulu", -10) | ||
check( 42.7000, -80.0000, "Etc/GMT+5", -5) | ||
check( 51.0036, -114.0161, "America/Edmonton", -7, -6) | ||
check(-16.4965, -68.1702, "America/La_Paz", -4) | ||
check(-31.9369, 115.84534, "Australia/Perth", 8) | ||
}) |
9853390
1173
26