Skip to content

Commit

Permalink
Merge pull request #168 from moment/utc-offset
Browse files Browse the repository at this point in the history
Prefer moment.fn.utcOffset to moment.fn.zone
  • Loading branch information
timrwood committed Dec 19, 2014
2 parents 305b9bc + 26289da commit 7a6f7f1
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 42 deletions.
6 changes: 5 additions & 1 deletion moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,11 @@
if (Math.abs(offset) < 16) {
offset = offset / 60;
}
mom.zone(offset, keepTime);
if (mom.utcOffset !== undefined) {
mom.utcOffset(-offset, keepTime);
} else {
mom.zone(offset, keepTime);
}
}
};

Expand Down
16 changes: 13 additions & 3 deletions tests/helpers/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

var moment = require('../../index');

function getUTCOffset (m) {
if (m.utcOffset !== undefined) {
return m.utcOffset();
} else {
return -m.zone();
}
}

function testYear(test, name, expected) {
var len = expected.length,
i,
Expand All @@ -16,7 +24,7 @@ function testYear(test, name, expected) {
offset = expected[i][3];
m = moment(date).tz(name);
test.equal(m.format("HH:mm:ss"), time, date + ' should be ' + time + ' ' + abbr);
test.equal(m.zone(), offset, date + ' should be ' + offset + ' minutes offset in ' + abbr);
test.equal(getUTCOffset(m), -offset, date + ' should be ' + offset + ' minutes offset in ' + abbr);
test.equal(m.zoneAbbr(), abbr, date + ' should be ' + abbr);
}

Expand All @@ -28,5 +36,7 @@ module.exports = {
return function (test) {
testYear(test, name, expected);
};
}
};
},

getUTCOffset : getUTCOffset
};
65 changes: 33 additions & 32 deletions tests/moment-timezone/parse.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

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

var Los_Angeles = "America/Los_Angeles|PST PDT PWT PPT|80 70 70 70|010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261q0 1nX0 11B0 1nX0 SgN0 8x10 iy0 5Wp0 1Vb0 3dB0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 1cL0 s10 1Vz0 LB0 1BX0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1a10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 14p0 1lb0 14p0 1lb0 14p0 1nX0 11B0 1nX0 11B0 1nX0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Rd0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0 Op0 1zb0";
var New_York = "America/New_York|EST EDT EWT EPT|50 40 40 40|01010101010101010101010101010101010101010101010102301010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010|-261t0 1nX0 11B0 1nX0 11B0 1qL0 1a10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z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atEnd.format("HH mm ss Z"), "01 59 59 -08:00", "During the lost hour, the time should roll back to the previous time");
t.equal( after.format("HH mm ss Z"), "03 00 00 -07:00", "After the lost hour, the time should match the input time");

t.equal( before.zone(), 480, "Before the lost hour, the offset should match the non-dst offset");
t.equal(atStart.zone(), 480, "During the lost hour, the offset should match the non-dst offset");
t.equal( atEnd.zone(), 480, "During the lost hour, the offset should match the non-dst offset");
t.equal( after.zone(), 420, "After the lost hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(before), -480, "Before the lost hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(atStart), -480, "During the lost hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(atEnd), -480, "During the lost hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(after), -420, "After the lost hour, the offset should match the dst offset");

t.done();
},
Expand All @@ -65,10 +66,10 @@ exports.parse = {
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -05:00", "During the lost hour, the time should roll back to the previous time");
t.equal( after.format("HH mm ss Z"), "03 00 00 -04:00", "After the lost hour, the time should match the input time");

t.equal( before.zone(), 300, "Before the lost hour, the offset should match the non-dst offset");
t.equal(atStart.zone(), 300, "During the lost hour, the offset should match the non-dst offset");
t.equal( atEnd.zone(), 300, "During the lost hour, the offset should match the non-dst offset");
t.equal( after.zone(), 240, "After the lost hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(before), -300, "Before the lost hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(atStart), -300, "During the lost hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(atEnd), -300, "During the lost hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(after), -240, "After the lost hour, the offset should match the dst offset");

t.done();
},
Expand All @@ -87,10 +88,10 @@ exports.parse = {
t.equal( atEnd.format("HH mm ss Z"), "03 59 59 -07:00", "During the lost hour, the time should roll forward to the previous time");
t.equal( after.format("HH mm ss Z"), "03 00 00 -07:00", "After the lost hour, the time should match the input time");

t.equal( before.zone(), 480, "Before the lost hour, the offset should match the non-dst offset");
t.equal(atStart.zone(), 420, "During the lost hour, the offset should match the dst offset");
t.equal( atEnd.zone(), 420, "During the lost hour, the offset should match the dst offset");
t.equal( after.zone(), 420, "After the lost hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(before), -480, "Before the lost hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(atStart), -420, "During the lost hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(atEnd), -420, "During the lost hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(after), -420, "After the lost hour, the offset should match the dst offset");

t.done();
},
Expand All @@ -109,10 +110,10 @@ exports.parse = {
t.equal( atEnd.format("HH mm ss Z"), "03 59 59 -04:00", "During the lost hour, the time should roll forward to the previous time");
t.equal( after.format("HH mm ss Z"), "03 00 00 -04:00", "After the lost hour, the time should match the input time");

t.equal( before.zone(), 300, "Before the lost hour, the offset should match the non-dst offset");
t.equal(atStart.zone(), 240, "During the lost hour, the offset should match the dst offset");
t.equal( atEnd.zone(), 240, "During the lost hour, the offset should match the dst offset");
t.equal( after.zone(), 240, "After the lost hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(before), -300, "Before the lost hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(atStart), -240, "During the lost hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(atEnd), -240, "During the lost hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(after), -240, "After the lost hour, the offset should match the dst offset");

t.done();
},
Expand All @@ -131,10 +132,10 @@ exports.parse = {
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -07:00", "During the duplicated hour, the time should match the earlier input time");
t.equal( after.format("HH mm ss Z"), "02 00 00 -08:00", "After the duplicated hour, the time should match the input time");

t.equal( before.zone(), 420, "Before the duplicated hour, the offset should match the dst offset");
t.equal(atStart.zone(), 420, "During the duplicated hour, the offset should match the dst offset");
t.equal( atEnd.zone(), 420, "During the duplicated hour, the offset should match the dst offset");
t.equal( after.zone(), 480, "After the duplicated hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(before), -420, "Before the duplicated hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(atStart), -420, "During the duplicated hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(atEnd), -420, "During the duplicated hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(after), -480, "After the duplicated hour, the offset should match the non-dst offset");

t.done();
},
Expand All @@ -153,10 +154,10 @@ exports.parse = {
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -04:00", "During the duplicated hour, the time should match the earlier input time");
t.equal( after.format("HH mm ss Z"), "02 00 00 -05:00", "After the duplicated hour, the time should match the input time");

t.equal( before.zone(), 240, "Before the duplicated hour, the offset should match the dst offset");
t.equal(atStart.zone(), 240, "During the duplicated hour, the offset should match the dst offset");
t.equal( atEnd.zone(), 240, "During the duplicated hour, the offset should match the dst offset");
t.equal( after.zone(), 300, "After the duplicated hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(before), -240, "Before the duplicated hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(atStart), -240, "During the duplicated hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(atEnd), -240, "During the duplicated hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(after), -300, "After the duplicated hour, the offset should match the non-dst offset");

t.done();
},
Expand All @@ -175,10 +176,10 @@ exports.parse = {
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -08:00", "During the duplicated hour, the time should match the later input time");
t.equal( after.format("HH mm ss Z"), "02 00 00 -08:00", "After the duplicated hour, the time should match the input time");

t.equal( before.zone(), 420, "Before the duplicated hour, the offset should match the dst offset");
t.equal(atStart.zone(), 480, "During the duplicated hour, the offset should match the non-dst offset");
t.equal( atEnd.zone(), 480, "During the duplicated hour, the offset should match the non-dst offset");
t.equal( after.zone(), 480, "After the duplicated hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(before), -420, "Before the duplicated hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(atStart), -480, "During the duplicated hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(atEnd), -480, "During the duplicated hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(after), -480, "After the duplicated hour, the offset should match the non-dst offset");

t.done();
},
Expand All @@ -197,10 +198,10 @@ exports.parse = {
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -05:00", "During the duplicated hour, the time should match the later input time");
t.equal( after.format("HH mm ss Z"), "02 00 00 -05:00", "After the duplicated hour, the time should match the input time");

t.equal( before.zone(), 240, "Before the duplicated hour, the offset should match the dst offset");
t.equal(atStart.zone(), 300, "During the duplicated hour, the offset should match the non-dst offset");
t.equal( atEnd.zone(), 300, "During the duplicated hour, the offset should match the non-dst offset");
t.equal( after.zone(), 300, "After the duplicated hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(before), -240, "Before the duplicated hour, the offset should match the dst offset");
t.equal(helpers.getUTCOffset(atStart), -300, "During the duplicated hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(atEnd), -300, "During the duplicated hour, the offset should match the non-dst offset");
t.equal(helpers.getUTCOffset(after), -300, "After the duplicated hour, the offset should match the non-dst offset");

t.done();
},
Expand Down
13 changes: 7 additions & 6 deletions tests/moment-timezone/utc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use strict";

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

exports.utc = {
utc : function (test) {
Expand All @@ -11,34 +12,34 @@ exports.utc = {

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

m.tz("TestUTC/Pacific");

test.equal(m.zone(), 480, "Should change the offset when using moment.fn.tz");
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");

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

test.equal(m.zone(), 0, "Should set the offset to +00:00 when using moment.fn.utc");
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: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(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");

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

test.equal(m.zone(), 0, "Should set the offset to +00:00 when using moment.fn.utc");
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: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(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.done();
Expand Down

0 comments on commit 7a6f7f1

Please sign in to comment.