Skip to content

Commit

Permalink
more simplification
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Jun 30, 2012
1 parent 7b5c582 commit 994068f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
8 changes: 8 additions & 0 deletions spec/tz.sync.spec.js
Expand Up @@ -134,6 +134,14 @@ describe('TimezoneJS', function () {
}
});

it('should get tzInfo quickly', function () {
var time = Date.now();
for (var i = 0; i < 5000; i++) {
timezoneJS.timezone.getTzInfo(new Date(), 'America/Chicago');
}
console.log('Took ' + (Date.now() - time) + 'ms to get 5000 same tzInfo');
});

it('should throw error with invalid zone', function () {
var testFn = function () {
timezoneJS.timezone.getTzInfo(new Date(), 'asd')
Expand Down
29 changes: 10 additions & 19 deletions src/date.js
Expand Up @@ -497,11 +497,11 @@
}
return zoneList[i+1];
}
function getBasicOffset(z) {
var off = parseTimeString(z[0])
, adj = z[0].indexOf('-') === 0 ? -1 : 1;
function getBasicOffset(time) {
var off = parseTimeString(time)
, adj = time.indexOf('-') === 0 ? -1 : 1;
off = adj * (((off[1] * 60 + off[2]) * 60 + off[3]) * 1000);
return -off/60/1000;
return off/60/1000;
}

//if isUTC is true, date is given in UTC, otherwise it's given
Expand All @@ -528,9 +528,9 @@
} else if (type === 's') { // Standard Time
offset = basicOffset;
} else if (type === 'w' || !type) { // Wall Clock Time
offset = getAdjustedOffset(basicOffset,rule);
offset = getAdjustedOffset(basicOffset, rule);
} else {
throw("unknown type "+type);
throw("unknown type " + type);
}
offset *= 60 * 1000; // to millis

Expand Down Expand Up @@ -689,14 +689,7 @@
return applicableRules[pinpoint - 1][1];
}
function getAdjustedOffset(off, rule) {
var save = rule[6][0];
var t = rule[6][1]
var adj = save.indexOf('-') === 0 ? -1 : 1;
var ret = (adj*(((t[1] *60 + t[2]) * 60 + t[3]) * 1000));
ret = ret/60/1000;
ret -= off;
ret = -Math.ceil(ret);
return ret;
return -Math.ceil(rule[6] - off);
}
function getAbbreviation(zone, rule) {
var res;
Expand All @@ -716,9 +709,7 @@
}
else if (base.indexOf('/') > -1) {
//Chose one of two alternative strings.
var t = rule[6][1];
var isDst = t[1] || t[2] || t[3];
res = base.split("/", 2)[isDst ? 1 : 0];
res = base.split("/", 2)[rule[6] ? 1 : 0];
} else {
res = base;
}
Expand Down Expand Up @@ -832,7 +823,7 @@
//Process zone right here and replace 3rd element with the processed array.
arr.splice(3, arr.length, processZone(arr));
if (arr[3]) arr[3] = Date.UTC.apply(null, arr[3]);
arr[0] = getBasicOffset(arr);
arr[0] = -getBasicOffset(arr[0]);
_this.zones[zone].push(arr);
break;
case 'Rule':
Expand All @@ -841,7 +832,7 @@
_this.rules[rule] = [];
}
arr[5] = parseTimeString(arr[5]);
arr[6] = [arr[6], parseTimeString(arr[6])];
arr[6] = getBasicOffset(arr[6]);
_this.rules[rule].push(arr);
break;
case 'Link':
Expand Down

0 comments on commit 994068f

Please sign in to comment.