Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch `olson` into `master`.

    File                             Raw         Min         Zip     Min/Zip
    lib/timezone.js          10583/10.33  4823/04.71  3125/03.05  2174/02.12
    lib/transitions.js        6791/06.63  3044/02.97  2154/02.10  1312/01.28
    date.js                   4515/04.41  3090/03.02  1200/01.17  1007/00.98
    timezones/asia.js      131526/128.44 59645/58.25  7566/07.39  6450/06.30
    zones/America/Detroit.js  2454/02.40  2452/02.39   830/00.81   830/00.81
  • Loading branch information...
commit 39e518d53883b4e6d79cb7ba960a0278dc21b05e 2 parents 1671063 + ea23213
@bigeasy authored
Showing with 144 additions and 201 deletions.
  1. +144 −182 bin/tz2json.js
  2. +0 −19 timezones/index.js
View
326 bin/tz2json.js
@@ -1,199 +1,161 @@
-// Generated by CoffeeScript 1.3.1
-(function() {
- var DAY, HOUR, MINUTE, MONTH, base, clock, date, day, file, from, fs, getDate, hour, i, info, letter, line, minute, month, name, parseOffset, record, save, second, time, to, type, zone, _base, _i, _j, _k, _l, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _ref4;
+var fs = require("fs")
+ , MONTH = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(/\s+/)
+ , DAY = "Sun Mon Tue Wed Thu Fri Sat".split(/\s+/)
+ ;
- fs = require("fs");
+function parseOffset (pattern, seconds) {
+ var i, match, milliseconds, offset = 0;
+ if (pattern == "0") pattern = "0:00";
+ match = /^(-?)(\d+)(?::(\d+))?(?::(\d+))?$/.exec(pattern);
+ if (!match) throw new Error("pattern: " + pattern);
+ if (!seconds && match[4]) throw new Error("" + pattern + " " + match[3]);
+ match[1] += '1';
+ for (i = 1; i < 5; i++) match[i] = parseInt(match[i] || 0, 10);
+ milliseconds = [ 36e5, 6e4, 1e3 ];
+ for (i = 0; i < 3; i++) offset += match[i + 2] * milliseconds[i];
+ return offset * match[1];
+}
- MONTH = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(/\s+/);
+function getDate (month, day) {
+ var date, last, least, match;
+ if (match = /^last(.*)$/.exec(day)) {
+ date = month, day = DAY.indexOf(match[1]);
+ while (date.getUTCMonth() == month.getUTCMonth()) {
+ if (date.getUTCDay() == day) last = date;
+ date = new Date(date.getTime() + 864e5);
+ }
+ return last;
+ } else if (match = /^first(.*)$/.exec(day)) {
+ return getDate(year, month, match[1] + ">=1");
+ } else if (match = /^(\w+)>=(\d+)$/.exec(day)) {
+ date = month, day = DAY.indexOf(match[1]), least = parseInt(match[2], 10);
+ while (date.getUTCDate() < least) date.setUTCDate(date.getUTCDate() + 1);
+ while (date.getUTCDay() != day) date.setUTCDate(date.getUTCDate() + 1);
+ return date;
+ } else {
+ month.setUTCDate(parseInt(day, 10));
+ return month;
+ }
+};
- DAY = "Sun Mon Tue Wed Thu Fri Sat".split(/\s+/);
+function nameOfClock (time) {
+ switch (time[time.length - 1]) {
+ case "s":
+ return "standard";
+ case "g":
+ case "u":
+ case "z":
+ return "posix";
+ default:
+ return "wallclock";
+ }
+}
- MINUTE = 60 * 1000;
+var file = process.argv[2]
+ , info = { rules: {}, zones: {} }
+ , base = file.replace(/^.*\/(.*)$/, "$1")
+ , name = null
+ , lines = fs.readFileSync(file, "utf8").split(/\n/)
+ , k, K
+ ;
- HOUR = MINUTE * 60;
+for (k = 0, K = lines.length; k < K; k++) {
+ line = lines[k].trim();
+ if (line == "" || /^\s*#/.test(line)) continue;
+ line = line.replace(/\s*#.*$/, "");
+ record = line.split(/\s+/);
+ switch (record[0]) {
+ case "Rule":
+ var rule = record.slice(1)
+ , name = rule[0]
+ , from = rule[1]
+ , to = rule[2]
+ , type = rule[3]
+ , month = rule[4]
+ , day = rule[5]
+ , time = rule[6]
+ , save = rule[7]
+ , letter = rule[8]
+ , clock = nameOfClock(time)
+ ;
- parseOffset = function(pattern, seconds) {
- var i, match, milliseconds, offset, _i, _ref;
- offset = 0;
- if (pattern === "0") {
- pattern = "0:00";
- }
- match = /^(-?)(\d+)(?::(\d+))?(?::(\d+))?$/.exec(pattern);
- if (!match) {
- throw new Error("pattern: " + pattern);
- }
- match = match.slice(1, 5);
- match[0] += '1';
- if (!seconds && match[3]) {
- throw new Error("" + pattern + " " + match[3]);
- }
- milliseconds = [HOUR, MINUTE, 1000];
- for (i = _i = 1, _ref = match.length; 1 <= _ref ? _i < _ref : _i > _ref; i = 1 <= _ref ? ++_i : --_i) {
- offset += parseInt(match[i] || '0', 10) * milliseconds[i - 1];
- }
- offset *= parseInt(match[0]);
- return offset;
- };
+ if (type !== "-") throw Error("A type! What does it mean?");
- getDate = function(month, day) {
- var date, last, least, match, _ref, _ref1;
- if (match = /^last(.*)$/.exec(day)) {
- _ref = [month, DAY.indexOf(match[1])], date = _ref[0], day = _ref[1], last = _ref[2];
- while (date.getUTCMonth() === month.getUTCMonth()) {
- if (date.getUTCDay() === day) {
- last = date;
- }
- date = new Date(date.getTime() + 24 * HOUR);
- }
- return last;
- } else if (match = /^first(.*)$/.exec(day)) {
- return getDate(year, month, "" + match[1] + ">=1}");
- } else if (match = /^(\w+)>=(\d+)$/.exec(day)) {
- _ref1 = [month, DAY.indexOf(match[1]), parseInt(match[2], 10)], date = _ref1[0], day = _ref1[1], least = _ref1[2];
- while (date.getUTCDate() < least) {
- date.setUTCDate(date.getUTCDate() + 1);
- }
- while (date.getUTCDay() !== day) {
- date.setUTCDate(date.getUTCDate() + 1);
- }
- return date;
- } else {
- month.setUTCDate(parseInt(day, 10));
- return month;
- }
- };
+ if (time == "0") time = "0:00";
+ time = time.replace(/[suzgw]$/, '');
+ time = /^(\d+):(\d+)(?::(\d+))?$/.exec(time);
+ if (time[3]) throw new Error("A rule time with seconds.");
+ time = parseInt(time[1], 10) * 60 + parseInt(time[2], 10);
- _ref = process.argv.slice(2);
- for (_i = 0, _len = _ref.length; _i < _len; _i++) {
- file = _ref[_i];
- info = {
- rules: {},
- zones: {}
- };
- base = file.replace(/^.*\/(.*)$/, "$1");
- name = null;
- _ref1 = fs.readFileSync(file, "utf8").split(/\n/);
- for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
- line = _ref1[_j];
- line = line.trim();
- if (line === "" || /^\s*#/.test(line)) {
- continue;
- }
- line = line.replace(/\s*#.*$/, "");
- record = line.split(/\s+/);
- switch (record[0]) {
- case "Rule":
- _ref2 = record.slice(1), name = _ref2[0], from = _ref2[1], to = _ref2[2], type = _ref2[3], month = _ref2[4], day = _ref2[5], time = _ref2[6], save = _ref2[7], letter = _ref2[8];
- if (type !== "-") {
- console.log(type);
- }
- if (time === "0") {
- time = "0:00";
- }
- clock = (function() {
- switch (time[time.length - 1]) {
- case "s":
- return "standard";
- case "g":
- case "u":
- case "z":
- return "posix";
- default:
- return "wallclock";
- }
- })();
- time = time.replace(/[suzgw]$/, '');
- time = /^(\d+):(\d+)(?::(\d+))?$/.exec(time).slice(1);
- if (time[2]) {
- throw new Error(time);
- }
- for (i = _k = 0; _k <= 1; i = ++_k) {
- time[i] = parseInt(time[i] || 0, 10);
- }
- time = time[0] * 60 + time[1];
- (_base = info.rules)[name] || (_base[name] = []);
- info.rules[name].push({
- from: parseInt(from, 10),
- to: (function() {
- switch (to) {
- case "only":
- return parseInt(from, 10);
- case "max":
- return Number.MAX_VALUE;
- default:
- return parseInt(to, 10);
- }
- })(),
- month: MONTH.indexOf(month),
- day: day,
- time: time,
- clock: clock,
- save: parseOffset(save) / 6e4,
- letter: letter === "-" ? "" : letter
- });
- break;
- case "Link":
- break;
- default:
- if (record[0] === "Zone") {
- name = record[1];
- info.zones[name] = [];
- record = record.slice(2);
+ if (! info.rules[name]) info.rules[name] = [];
+
+ info.rules[name].push({
+ from: parseInt(from, 10),
+ to: (function() {
+ switch (to) {
+ case "only": return parseInt(from, 10);
+ case "max": return Number.MAX_VALUE;
+ default: return parseInt(to, 10);
}
- info.zones[name].push({
- offset: parseOffset(record[0], true) / 1000,
- rules: record[1],
- format: record[2],
- until: record.slice(3)
- });
+ })(),
+ month: MONTH.indexOf(month),
+ day: day,
+ time: time,
+ clock: clock,
+ save: parseOffset(save) / 6e4,
+ letter: letter == "-" ? "" : letter
+ });
+
+ break;
+ case "Link":
+ break;
+ default:
+ if (record[0] == "Zone") {
+ name = record[1];
+ info.zones[name] = [];
+ record = record.slice(2);
}
+ info.zones[name].push({
+ offset: parseOffset(record[0], true) / 1000,
+ rules: record[1],
+ format: record[2],
+ until: record.slice(3)
+ });
+ }
+}
+
+var zone, match, name;
+for (name in info.zones) {
+ zone = info.zones[name];
+ zone.reverse();
+ for (i = 0, I = zone.length; i < I; i++) {
+ record = zone[i];
+ record.clock = "wallclock";
+ if (record.rules == "-") {
+ record.rules = false;
+ } else if (/^\d+:\d+$/.test(record.rules)) {
+ record.rules = parseOffset(record.rules) / 6e4;
}
- _ref3 = info.zones;
- for (name in _ref3) {
- zone = _ref3[name];
- zone.reverse();
- for (_l = 0, _len2 = zone.length; _l < _len2; _l++) {
- record = zone[_l];
- record.clock = "wallclock";
- if (record.rules === "-") {
- record.rules = false;
- } else if (/^\d+:\d+$/.test(record.rules)) {
- record.rules = parseOffset(record.rules) / 6e4;
- }
+ if (record.until.length) {
+ date = new Date(Date.UTC(parseInt(record.until.shift(), 10), MONTH.indexOf(record.until.shift() || "Jan")));
+ if (record.until.length) {
+ date = getDate(date, record.until.shift());
if (record.until.length) {
- date = new Date(Date.UTC(parseInt(record.until.shift(), 10), MONTH.indexOf(record.until.shift() || "Jan")));
- if (record.until.length) {
- date = getDate(date, record.until.shift());
- if (record.until.length) {
- _ref4 = /^(\d+):(\d+)(?::(\d+))?(s|w|g|u|z)?$/.exec(record.until.shift()).slice(1), hour = _ref4[0], minute = _ref4[1], second = _ref4[2], type = _ref4[3];
- date.setUTCHours(parseInt(hour, 10));
- date.setUTCMinutes(parseInt(minute, 10));
- if (second != null) {
- date.setUTCSeconds(parseInt(minute, 10));
- }
- record.clock = (function() {
- switch (type) {
- case "s":
- return "standard";
- case "g":
- case "u":
- case "z":
- return "posix";
- default:
- return "wallclock";
- }
- })();
- }
- }
- record.until = date.getTime() / 1000;
- } else {
- record.until = false;
+ time = record.until.shift();
+ record.clock = nameOfClock(time);
+ match = /^(\d+):(\d+)(?::(\d+))?[swguz]?$/.exec(time);
+ date.setUTCHours(parseInt(match[1], 10));
+ date.setUTCMinutes(parseInt(match[2], 10));
+ date.setUTCSeconds(parseInt(match[3] || 0, 10));
}
}
+ record.until = date.getTime() / 1000;
+ } else {
+ record.until = false;
}
- process.stdout.write("module.exports = ");
- process.stdout.write(JSON.stringify(info, null, 2));
- process.stdout.write("\n");
}
+}
-}).call(this);
+process.stdout.write("module.exports = ");
+process.stdout.write(JSON.stringify(info, null, 2));
+process.stdout.write("\n");
View
19 timezones/index.js
@@ -1,19 +0,0 @@
-var fs = require("fs");
-
-module.exports = { zones: {}, rules: {} };
-
-fs.readdirSync(__dirname).forEach(function (file) {
- if (file !== "index.js") {
- data = require(__dirname + "/" + file);
- for (var key in data.zones) {
- if (data.zones.hasOwnProperty(key)) {
- module.exports.zones[key] = data.zones[key];
- }
- }
- for (var key in data.rules) {
- if (data.rules.hasOwnProperty(key)) {
- module.exports.rules[key] = data.rules[key];
- }
- }
- }
-});
Please sign in to comment.
Something went wrong with that request. Please try again.