Skip to content

Commit

Permalink
Merge branch olson into master.
Browse files Browse the repository at this point in the history
    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
flatheadmill committed Apr 30, 2012
2 parents 1671063 + ea23213 commit 39e518d
Show file tree
Hide file tree
Showing 2 changed files with 144 additions and 201 deletions.
326 changes: 144 additions & 182 deletions bin/tz2json.js
Original file line number Original file line Diff line number Diff line change
@@ -1,199 +1,161 @@
// Generated by CoffeeScript 1.3.1 var fs = require("fs")
(function() { , MONTH = "Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec".split(/\s+/)
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; , 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) { if (type !== "-") throw Error("A type! What does it mean?");
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;
};


getDate = function(month, day) { if (time == "0") time = "0:00";
var date, last, least, match, _ref, _ref1; time = time.replace(/[suzgw]$/, '');
if (match = /^last(.*)$/.exec(day)) { time = /^(\d+):(\d+)(?::(\d+))?$/.exec(time);
_ref = [month, DAY.indexOf(match[1])], date = _ref[0], day = _ref[1], last = _ref[2]; if (time[3]) throw new Error("A rule time with seconds.");
while (date.getUTCMonth() === month.getUTCMonth()) { time = parseInt(time[1], 10) * 60 + parseInt(time[2], 10);
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;
}
};


_ref = process.argv.slice(2); if (! info.rules[name]) info.rules[name] = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
file = _ref[_i]; info.rules[name].push({
info = { from: parseInt(from, 10),
rules: {}, to: (function() {
zones: {} switch (to) {
}; case "only": return parseInt(from, 10);
base = file.replace(/^.*\/(.*)$/, "$1"); case "max": return Number.MAX_VALUE;
name = null; default: return parseInt(to, 10);
_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);
} }
info.zones[name].push({ })(),
offset: parseOffset(record[0], true) / 1000, month: MONTH.indexOf(month),
rules: record[1], day: day,
format: record[2], time: time,
until: record.slice(3) 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; if (record.until.length) {
for (name in _ref3) { date = new Date(Date.UTC(parseInt(record.until.shift(), 10), MONTH.indexOf(record.until.shift() || "Jan")));
zone = _ref3[name]; if (record.until.length) {
zone.reverse(); date = getDate(date, record.until.shift());
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) { if (record.until.length) {
date = new Date(Date.UTC(parseInt(record.until.shift(), 10), MONTH.indexOf(record.until.shift() || "Jan"))); time = record.until.shift();
if (record.until.length) { record.clock = nameOfClock(time);
date = getDate(date, record.until.shift()); match = /^(\d+):(\d+)(?::(\d+))?[swguz]?$/.exec(time);
if (record.until.length) { date.setUTCHours(parseInt(match[1], 10));
_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.setUTCMinutes(parseInt(match[2], 10));
date.setUTCHours(parseInt(hour, 10)); date.setUTCSeconds(parseInt(match[3] || 0, 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;
} }
} }
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");
19 changes: 0 additions & 19 deletions timezones/index.js

This file was deleted.

0 comments on commit 39e518d

Please sign in to comment.