Skip to content

Commit

Permalink
Allow moment.fn.utc to clear the timezone flag #86
Browse files Browse the repository at this point in the history
  • Loading branch information
timrwood committed Jun 19, 2014
1 parent 25a6389 commit eba2393
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
16 changes: 12 additions & 4 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@
}
}

function intToUntil (array) {
for (var i = 0; i < array.length; i++) {
function intToUntil (array, length) {
for (var i = 0; i < length; i++) {
array[i] = Math.round((array[i - 1] || 0) + (array[i] * 60000)); // minutes to milliseconds
}

array.push(Infinity);
array[length - 1] = Infinity;
}

function mapIndices (source, indices) {
Expand All @@ -104,7 +104,7 @@
arrayToInt(indices);
arrayToInt(untils);

intToUntil(untils);
intToUntil(untils, indices.length);

return {
name : data[0],
Expand Down Expand Up @@ -310,8 +310,16 @@
};
}

function resetZoneWrap (old) {
return function () {
this._z = null;
return old.call(this);
};
}

fn.zoneName = abbrWrap(fn.zoneName);
fn.zoneAbbr = abbrWrap(fn.zoneAbbr);
fn.utc = resetZoneWrap(fn.utc);

// Cloning a moment should include the _z property.
moment.momentProperties._z = null;
Expand Down
46 changes: 46 additions & 0 deletions tests/moment-timezone/utc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
"use strict";

var moment = require("../../");

exports.utc = {
utc : function (test) {
moment.tz.add([
"TestUTC/Pacific|PST|80|0|",
"TestUTC/Eastern|EST|50|0|"
]);

var m = moment("2014-07-10 12:00:00+00:00"),
localFormat = m.format(),
localZone = m.zone();

m.tz("TestUTC/Pacific");

test.equal(m.zone(), 480, "Should change the offset when using moment.fn.tz");
test.equal(m.format(), "2014-07-10T04:00:00-08:00", "Should change the offset when using moment.fn.tz");

m.utc();
moment.updateOffset(m);

test.equal(m.zone(), 0, "Should set the offset to +00:00 when using moment.fn.utc");
test.equal(m.format(), "2014-07-10T12:00:00+00:00", "Should change the offset when using moment.fn.utc");

m.tz("TestUTC/Eastern");

test.equal(m.zone(), 300, "Should change the offset when using moment.fn.tz");
test.equal(m.format(), "2014-07-10T07:00:00-05:00", "Should change the offset when using moment.fn.tz");

m.utc();
moment.updateOffset(m);

test.equal(m.zone(), 0, "Should set the offset to +00:00 when using moment.fn.utc");
test.equal(m.format(), "2014-07-10T12:00:00+00:00", "Should change the offset when using moment.fn.utc");

m.local();
moment.updateOffset(m);

test.equal(m.zone(), localZone, "Should reset the offset to local time when using moment.fn.local");
test.equal(m.format(), localFormat, "Should reset the offset to local time when using moment.fn.local");

test.done();
}
};

0 comments on commit eba2393

Please sign in to comment.