diff --git a/README.md b/README.md new file mode 100644 index 0000000..a93482c --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +tzdetect.js +=========== + +Goals +----- + +* detect the user timezone, or more precisely list all IANA time zone ids that are compatible with the running javascript engine +* limit data duplication by using the Moment.js version of the Olson tables + +Demo +---- + +See http://dystroy.org/stackoverflow/timezonedetect.html + +Usage +----- + + + + + + +Related +------- + + http://stackoverflow.com/questions/19420582/detect-the-id-of-the-current-user-timezone-using-moment-js diff --git a/bower.json b/bower.json new file mode 100644 index 0000000..be1bffd --- /dev/null +++ b/bower.json @@ -0,0 +1,22 @@ +{ + "name": "moment-tzdetect", + "version": "0.1.0", + "homepage": "https://github.com/jamesjieye/tzdetect.js", + "authors": [ + "Canop " + ], + "description": "A JavaScript library to detect the timezone using Moment.js", + "main": "tzdetect.js", + "keywords": [ + "moment", + "timezone" + ], + "license": "MIT", + "ignore": [ + "**/.*", + "node_modules", + "bower_components", + "test", + "tests" + ] +} diff --git a/tzdetect.js b/tzdetect.js new file mode 100644 index 0000000..77cadb7 --- /dev/null +++ b/tzdetect.js @@ -0,0 +1,21 @@ +// A small public domain library detecting the user's timezone using moment.js +// Repository : https://github.com/Canop/tzdetect.js +// Usage : +// tzdetect.matches() : array of all timezones matching the user's one +// tzdetect.matches(base) : array of all timezones matching the supplied one +var tzdetect = { + names: moment.tz.names(), + matches: function(base){ + var results = [], now = Date.now(), makekey = function(id){ + return [0, 4, 8, -5*12, 4-5*12, 8-5*12, 4-2*12, 8-2*12].map(function(months){ + var m = moment(now + months*30*24*60*60*1000); + if (id) m.tz(id); + return m.format("DDHHmm"); + }).join(' '); + }, lockey = makekey(base); + tzdetect.names.forEach(function(id){ + if (makekey(id)===lockey) results.push(id); + }); + return results; + } +};