From 6b7b558b685a5433e5af2ff30edc4b4e7de82d95 Mon Sep 17 00:00:00 2001 From: Matt Johnson Date: Wed, 17 Apr 2019 21:58:39 -0700 Subject: [PATCH] Clear _z when switching to local mode or setting a fixed offset. Fixes #738 --- moment-timezone.js | 17 ++++++++++++++--- tests/moment-timezone/utc.js | 33 +++++++++++++++++++++++++++++---- 2 files changed, 43 insertions(+), 7 deletions(-) diff --git a/moment-timezone.js b/moment-timezone.js index 79a0c035..f4b4ae15 100644 --- a/moment-timezone.js +++ b/moment-timezone.js @@ -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); } @@ -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 + '.'); diff --git a/tests/moment-timezone/utc.js b/tests/moment-timezone/utc.js index ce78026a..6fa0c184 100644 --- a/tests/moment-timezone/utc.js +++ b/tests/moment-timezone/utc.js @@ -7,10 +7,12 @@ 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); @@ -18,29 +20,52 @@ exports.utc = { 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();