Skip to content

Commit

Permalink
Init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesjieye committed Oct 3, 2016
0 parents commit 3e5770b
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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
-----

<script src="moment-with-langs.min.js"></script>
<script src="moment-timezone-with-data.min.js"></script>
<script src="tzdetect.js"></script>
<script>
var tzid = tzdetect.matches()[0]; // for example : "Europe/Paris"
</script>

Related
-------

http://stackoverflow.com/questions/19420582/detect-the-id-of-the-current-user-timezone-using-moment-js
22 changes: 22 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "moment-tzdetect",
"version": "0.1.0",
"homepage": "https://github.com/jamesjieye/tzdetect.js",
"authors": [
"Canop <[email protected]>"
],
"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"
]
}
21 changes: 21 additions & 0 deletions tzdetect.js
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit 3e5770b

Please sign in to comment.