Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
Switch to moment 0.0.3 (available through github, not through website)
Browse files Browse the repository at this point in the history
This comes with a huge perf boost due to memoization!

See: moment/moment-timezone#39
  • Loading branch information
jlfwong committed Nov 18, 2013
1 parent 937a70a commit a847b81
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
3 changes: 3 additions & 0 deletions server/static/js/ext/moment-timezone-data.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
define(["moment-timezone"], function (moment) {
// This was generated by http://momentjs.com/timezone/data/
// It was also manually modified to get rid of all the rules we don't care
// about for performance resons. This means ignoring dates before 2007.
moment.tz.add({
"zones": {
"America/Toronto": [
Expand Down
42 changes: 35 additions & 7 deletions server/static/js/ext/moment-timezone.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
// moment-timezone.js
// version : 0.0.1
// version : 0.0.3
// author : Tim Wood
// license : MIT
// github.com/timrwood/moment-timezone

(function () {

var VERSION = "0.0.1";
var VERSION = "0.0.3";

function onload(moment) {
var oldZoneName = moment.fn.zoneName,
Expand Down Expand Up @@ -53,6 +53,9 @@
this.timeRule = +timeRule;
this.offset = parseMinutes(offset);
this.letters = letters || '';
this.date = memoize(this.date);
this.weekdayAfter = memoize(this.weekdayAfter);
this.lastWeekday = memoize(this.lastWeekday);
}

Rule.prototype = {
Expand Down Expand Up @@ -135,6 +138,7 @@
function RuleSet (name) {
this.name = name;
this.rules = [];
this.lastYearRule = memoize(this.lastYearRule);
}

RuleSet.prototype = {
Expand Down Expand Up @@ -252,6 +256,7 @@
this.offset = parseMinutes(offset);
this.ruleSet = ruleSet;
this.letters = letters;
this.lastRule = memoize(this.lastRule);

for (i = 0; i < untilArray.length; i++) {
untilArray[i] = +untilArray[i];
Expand All @@ -265,10 +270,7 @@
},

lastRule : function () {
if (!this._lastRule) {
this._lastRule = this.rule(this.until);
}
return this._lastRule;
return this.rule(this.until);
},

format : function (rule) {
Expand All @@ -288,6 +290,9 @@
this.name = normalizeName(name);
this.displayName = name;
this.zones = [];
this.zoneAndRule = memoize(this.zoneAndRule, function (mom) {
return +mom;
});
}

ZoneSet.prototype = {
Expand Down Expand Up @@ -328,6 +333,16 @@
Global Methods
************************************/

function memoize (fn, keyFn) {
var cache = {};
return function (first) {
var key = keyFn ? keyFn.apply(this, arguments) : first;
return key in cache ?
cache[key] :
(cache[key] = fn.apply(this, arguments));
};
}

function addRules (rules) {
var i, j, rule;
for (i in rules) {
Expand Down Expand Up @@ -443,6 +458,15 @@
}
};

function getZoneSets() {
var sets = [],
zoneName;
for (zoneName in zoneSets) {
sets.push(zoneSets[zoneName]);
}
return sets;
}

moment.fn.tz = function (name) {
if (name) {
this._z = getZoneSet(name);
Expand Down Expand Up @@ -475,12 +499,16 @@
for (i = 0; i < len; i++) {
args[i] = arguments[i];
}
return moment.apply(null, args).tz(arguments[len]);
var m = moment.apply(null, args);
var preTzOffset = m.zone();
m.tz(arguments[len]);
return m.add('minutes', m.zone() - preTzOffset);
};

moment.tz.add = add;
moment.tz.addRule = addRule;
moment.tz.addZone = addZone;
moment.tz.zones = getZoneSets;

moment.tz.version = VERSION;

Expand Down
1 change: 0 additions & 1 deletion server/static/js/rmc_moment.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
define(["moment", "moment-timezone", "ext/moment-timezone-data"], function (_moment, __, __) {
// Always use America/Toronto as the timezone.
return function rmc_moment(a, b, c, d) {
countMe("rmc_moment");
return _moment(a, b, c, d)
.tz("America/Toronto");
};
Expand Down

0 comments on commit a847b81

Please sign in to comment.