Skip to content

Commit

Permalink
Clear _z when switching to local mode or setting a fixed offset.
Browse files Browse the repository at this point in the history
Fixes #738
  • Loading branch information
mattjohnsonpint committed Apr 18, 2019
1 parent b2fc011 commit 6b7b558
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
17 changes: 14 additions & 3 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,9 @@
offset = offset / 60;
}
if (mom.utcOffset !== undefined) {
var z = mom._z;
mom.utcOffset(-offset, keepTime);
mom._z = z;
} else {
mom.zone(offset, keepTime);
}
Expand Down Expand Up @@ -584,10 +586,19 @@
};
}

fn.zoneName = abbrWrap(fn.zoneName);
fn.zoneAbbr = abbrWrap(fn.zoneAbbr);
fn.utc = resetZoneWrap(fn.utc);
function resetZoneWrap2 (old) {
return function () {
if (arguments.length > 0) this._z = null;
return old.apply(this, arguments);
};
}

fn.zoneName = abbrWrap(fn.zoneName);
fn.zoneAbbr = abbrWrap(fn.zoneAbbr);
fn.utc = resetZoneWrap(fn.utc);
fn.local = resetZoneWrap(fn.local);
fn.utcOffset = resetZoneWrap2(fn.utcOffset);

moment.tz.setDefault = function(name) {
if (major < 2 || (major === 2 && minor < 9)) {
logError('Moment Timezone setDefault() requires Moment.js >= 2.9.0. You are using Moment.js ' + moment.version + '.');
Expand Down
33 changes: 29 additions & 4 deletions tests/moment-timezone/utc.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,65 @@ exports.utc = {
utc : function (test) {
moment.tz.add([
"TestUTC/Pacific|PST|80|0|",
"TestUTC/Mountain|MST|70|0|",
"TestUTC/Central|CST|60|0|",
"TestUTC/Eastern|EST|50|0|"
]);

var m = moment("2014-07-10 12:00:00+00:00"),
var m = moment("2014-07-10T12:00:00+00:00"),
localFormat = m.format(),
localOffset = helpers.getUTCOffset(m);

m.tz("TestUTC/Pacific");

test.equal(helpers.getUTCOffset(m), -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");
test.equal(m.tz(), "TestUTC/Pacific", "Should set the time zone name when using moment.fn.tz");

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

test.equal(helpers.getUTCOffset(m), 0, "Should set the offset to +00:00 when using moment.fn.utc");
test.equal(m.format(), "2014-07-10T12:00:00Z", "Should change the offset when using moment.fn.utc");
test.equal(m.tz(), undefined, "Should clear the time zone name when using moment.fn.utc");

m.tz("TestUTC/Eastern");
m.tz("TestUTC/Mountain");

test.equal(helpers.getUTCOffset(m), -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");
test.equal(helpers.getUTCOffset(m), -420, "Should change the offset when using moment.fn.tz");
test.equal(m.format(), "2014-07-10T05:00:00-07:00", "Should change the offset when using moment.fn.tz");
test.equal(m.tz(), "TestUTC/Mountain", "Should set the time zone name when using moment.fn.tz");

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

test.equal(helpers.getUTCOffset(m), 0, "Should set the offset to +00:00 when using moment.fn.utc");
test.equal(m.format(), "2014-07-10T12:00:00Z", "Should change the offset when using moment.fn.utc");
test.equal(m.tz(), undefined, "Should clear the time zone name when using moment.fn.utc");

m.tz("TestUTC/Central");

test.equal(helpers.getUTCOffset(m), -360, "Should change the offset when using moment.fn.tz");
test.equal(m.format(), "2014-07-10T06:00:00-06:00", "Should change the offset when using moment.fn.tz");
test.equal(m.tz(), "TestUTC/Central", "Should set the time zone name when using moment.fn.tz");

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

test.equal(helpers.getUTCOffset(m), localOffset, "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.equal(m.tz(), undefined, "Should clear the time zone name when using moment.fn.local");

m.tz("TestUTC/Eastern");

test.equal(helpers.getUTCOffset(m), -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");
test.equal(m.tz(), "TestUTC/Eastern", "Should set the time zone name when using moment.fn.tz");

m.utcOffset('+02:00')

test.equal(helpers.getUTCOffset(m), 120, "Should change the offset when using moment.fn.utcOffset");
test.equal(m.format(), "2014-07-10T14:00:00+02:00", "Should change the offset when using moment.fn.utcOffset");
test.equal(m.tz(), undefined, "Should clear the time zone name when using moment.fn.utcOffset");

m = moment('2017-01-01T00:00:00');
var utcWallTimeFormat = m.clone().utcOffset('-05:00', true).format();
Expand Down

0 comments on commit 6b7b558

Please sign in to comment.