Skip to content

Commit

Permalink
Merge pull request #101 from moment/feature/configurable-rollback
Browse files Browse the repository at this point in the history
Configuration for moving ambiguous or invalid input forward or backward
  • Loading branch information
timrwood committed Jul 7, 2014
2 parents 73a1ff3 + 162dbb9 commit c0fe03e
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 24 deletions.
21 changes: 18 additions & 3 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,26 @@
var target = +timestamp,
offsets = this.offsets,
untils = this.untils,
i;
max = untils.length - 1,
offset, offsetNext, offsetPrev, i;

for (i = 0; i < max; i++) {
offset = offsets[i];
offsetNext = offsets[i + 1];
offsetPrev = offsets[i ? i - 1 : i];

if (offset < offsetNext && tz.moveAmbiguousForward) {
offset = offsetNext;
} else if (offset > offsetPrev && tz.moveInvalidForward) {
offset = offsetPrev;
}

for (i = 0; i < untils.length; i++) {
if (target < untils[i] - (offsets[i] * 60000)) {
if (target < untils[i] - (offset * 60000)) {
return offsets[i];
}
}

return offsets[max];
},

abbr : function (mom) {
Expand Down Expand Up @@ -272,6 +285,8 @@
tz.unpack = unpack;
tz.unpackBase60 = unpackBase60;
tz.needsOffset = needsOffset;
tz.moveInvalidForward = true;
tz.moveAmbiguousForward = false;

/************************************
Interface with Moment.js
Expand Down
156 changes: 135 additions & 21 deletions tests/moment-timezone/parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,202 @@ var moment = require("../../index");
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 11z0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 RB0 8x40 iv0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1qN0 WL0 1qN0 11z0 1o10 11z0 1o10 11z0 1o10 11z0 1o10 1fz0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1cL0 1cN0 1fz0 1cN0 1cL0 1cN0 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 moveAmbiguousForward, moveInvalidForward;

exports.parse = {
setUp : function (done) {
moment.tz.add([Los_Angeles, New_York]);

moveAmbiguousForward = moment.tz.moveAmbiguousForward;
moveInvalidForward = moment.tz.moveInvalidForward;
done();
},

tearDown : function (done) {
moment.tz.moveAmbiguousForward = moveAmbiguousForward;
moment.tz.moveInvalidForward = moveInvalidForward;
done();
},

"ambiguous input losing an hour - America/Los_Angeles" : function (t) {
"default states" : function (t) {
t.ok(moment.tz.moveInvalidForward, "Should default to moving invalid input forward");
t.ok(!moment.tz.moveAmbiguousForward, "Should default to moving ambiguous input backward");

t.done();
},

"invalid input - moveInvalidForward = false - Los Angeles" : function (t) {
moment.tz.moveInvalidForward = false;

// the hour from 2am to 3am does not exist on March 11 2011 in America/Los_Angeles
var before = moment.tz([2012, 2, 11, 1, 59, 59], "America/Los_Angeles"),
atStart = moment.tz([2012, 2, 11, 2, 0, 0], "America/Los_Angeles"),
atEnd = moment.tz([2012, 2, 11, 2, 59, 59], "America/Los_Angeles"),
after = moment.tz([2012, 2, 11, 3, 0, 0], "America/Los_Angeles");

t.equal( before.format("HH mm ss Z"), "01 59 59 -08:00", "Before the lost hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -08:00", "During the lost hour, the time should fall back to the previous time");
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -08:00", "During the lost hour, the time should fall 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(atStart.format("HH mm ss Z"), "01 00 00 -08:00", "During the lost hour, the time should roll back to the previous time");
t.equal( 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 time should match the dst offset");
t.equal( after.zone(), 420, "After the lost hour, the offset should match the dst offset");

t.done();
},

"ambiguous input losing an hour - America/New_York" : function (t) {
"invalid input - moveInvalidForward = false - New York" : function (t) {
moment.tz.moveInvalidForward = false;

// the hour from 2am to 3am does not exist on March 11 2011 in America/New_York
var before = moment.tz([2012, 2, 11, 1, 59, 59], "America/New_York"),
atStart = moment.tz([2012, 2, 11, 2, 0, 0], "America/New_York"),
atEnd = moment.tz([2012, 2, 11, 2, 59, 59], "America/New_York"),
after = moment.tz([2012, 2, 11, 3, 0, 0], "America/New_York");

t.equal( before.format("HH mm ss Z"), "01 59 59 -05:00", "Before the lost hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -05:00", "During the lost hour, the time should fall back to the previous time");
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -05:00", "During the lost hour, the time should fall 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(atStart.format("HH mm ss Z"), "01 00 00 -05:00", "During the lost hour, the time should roll back to the previous time");
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 time should match the dst offset");
t.equal( after.zone(), 240, "After the lost hour, the offset should match the dst offset");

t.done();
},

"invalid input - moveInvalidForward = true - Los Angeles" : function (t) {
// moment.tz.moveInvalidForward = true; Should default to true

// the hour from 2am to 3am does not exist on March 11 2011 in America/Los_Angeles
var before = moment.tz([2012, 2, 11, 1, 59, 59], "America/Los_Angeles"),
atStart = moment.tz([2012, 2, 11, 2, 0, 0], "America/Los_Angeles"),
atEnd = moment.tz([2012, 2, 11, 2, 59, 59], "America/Los_Angeles"),
after = moment.tz([2012, 2, 11, 3, 0, 0], "America/Los_Angeles");

t.equal( before.format("HH mm ss Z"), "01 59 59 -08:00", "Before the lost hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "03 00 00 -07:00", "During the lost hour, the time should roll forward to the previous time");
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.done();
},

"invalid input - moveInvalidForward = true - New York" : function (t) {
// moment.tz.moveInvalidForward = true; Should default to true

// the hour from 2am to 3am does not exist on March 11 2011 in America/New_York
var before = moment.tz([2012, 2, 11, 1, 59, 59], "America/New_York"),
atStart = moment.tz([2012, 2, 11, 2, 0, 0], "America/New_York"),
atEnd = moment.tz([2012, 2, 11, 2, 59, 59], "America/New_York"),
after = moment.tz([2012, 2, 11, 3, 0, 0], "America/New_York");

t.equal( before.format("HH mm ss Z"), "01 59 59 -05:00", "Before the lost hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "03 00 00 -04:00", "During the lost hour, the time should roll forward to the previous time");
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.done();
},

"ambiguous input gaining an hour - America/Los_Angeles" : function (t) {
"ambiguous input - moveAmbiguousForward = false - Los Angeles" : function (t) {
// moment.tz.moveAmbiguousForward = false; Should default to false

// the hour from 1am to 2am happens twice on Nov 4 2011 in America/Los_Angeles
var before = moment.tz([2012, 10, 4, 0, 59, 59], "America/Los_Angeles"),
atStart = moment.tz([2012, 10, 4, 1, 0, 0], "America/Los_Angeles"),
atEnd = moment.tz([2012, 10, 4, 1, 59, 59], "America/Los_Angeles"),
after = moment.tz([2012, 10, 4, 2, 0, 0], "America/Los_Angeles");

t.equal( before.format("HH mm ss Z"), "00 59 59 -07:00", "Before the duplicated hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -07:00", "During the duplicated hour, the time should match the input time");
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -07:00", "During the duplicated hour, the time should match the 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(atStart.format("HH mm ss Z"), "01 00 00 -07:00", "During the duplicated hour, the time should match the earlier input time");
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 time should match the non-dst offset");
t.equal( after.zone(), 480, "After the duplicated hour, the offset should match the non-dst offset");

t.done();
},

"ambiguous input gaining an hour - America/New_York" : function (t) {
"ambiguous input - moveAmbiguousForward = false - New York" : function (t) {
// moment.tz.moveAmbiguousForward = false; Should default to false

// the hour from 1am to 2am happens twice on Nov 4 2011 in America/Los_Angeles
var before = moment.tz([2012, 10, 4, 0, 59, 59], "America/New_York"),
atStart = moment.tz([2012, 10, 4, 1, 0, 0], "America/New_York"),
atEnd = moment.tz([2012, 10, 4, 1, 59, 59], "America/New_York"),
after = moment.tz([2012, 10, 4, 2, 0, 0], "America/New_York");

t.equal( before.format("HH mm ss Z"), "00 59 59 -04:00", "Before the duplicated hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -04:00", "During the duplicated hour, the time should match the input time");
t.equal( atEnd.format("HH mm ss Z"), "01 59 59 -04:00", "During the duplicated hour, the time should match the 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(atStart.format("HH mm ss Z"), "01 00 00 -04:00", "During the duplicated hour, the time should match the earlier input time");
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 time should match the non-dst offset");
t.equal( after.zone(), 300, "After the duplicated hour, the offset should match the non-dst offset");

t.done();
},

"ambiguous input - moveAmbiguousForward = true - Los Angeles" : function (t) {
moment.tz.moveAmbiguousForward = true;

// the hour from 1am to 2am happens twice on Nov 4 2011 in America/Los_Angeles
var before = moment.tz([2012, 10, 4, 0, 59, 59], "America/Los_Angeles"),
atStart = moment.tz([2012, 10, 4, 1, 0, 0], "America/Los_Angeles"),
atEnd = moment.tz([2012, 10, 4, 1, 59, 59], "America/Los_Angeles"),
after = moment.tz([2012, 10, 4, 2, 0, 0], "America/Los_Angeles");

t.equal( before.format("HH mm ss Z"), "00 59 59 -07:00", "Before the duplicated hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -08:00", "During the duplicated hour, the time should match the later input time");
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.done();
},

"ambiguous input - moveAmbiguousForward = true - New York" : function (t) {
moment.tz.moveAmbiguousForward = true;

// the hour from 1am to 2am happens twice on Nov 4 2011 in America/Los_Angeles
var before = moment.tz([2012, 10, 4, 0, 59, 59], "America/New_York"),
atStart = moment.tz([2012, 10, 4, 1, 0, 0], "America/New_York"),
atEnd = moment.tz([2012, 10, 4, 1, 59, 59], "America/New_York"),
after = moment.tz([2012, 10, 4, 2, 0, 0], "America/New_York");

t.equal( before.format("HH mm ss Z"), "00 59 59 -04:00", "Before the duplicated hour, the time should match the input time");
t.equal(atStart.format("HH mm ss Z"), "01 00 00 -05:00", "During the duplicated hour, the time should match the later input time");
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.done();
},
Expand All @@ -110,5 +224,5 @@ exports.parse = {
}

t.done();
},
}
};
16 changes: 16 additions & 0 deletions tests/moment-timezone/zone.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,21 @@ var tz = require("../../").tz;
// gE = 1000; 1E = 100; 2k = 140
var PACKED = "SomeZone|TIM TAM IAM|60.u 50 60|012101|gE 1E 2k 1E 2k";

var moveAmbiguousForward, moveInvalidForward;

exports.zone = {
setUp : function (done) {
moveAmbiguousForward = tz.moveAmbiguousForward;
moveInvalidForward = tz.moveInvalidForward;
done();
},

tearDown : function (done) {
tz.moveAmbiguousForward = moveAmbiguousForward;
tz.moveInvalidForward = moveInvalidForward;
done();
},

construct : function (test) {
var zone = new tz.Zone(PACKED);

Expand Down Expand Up @@ -121,6 +135,8 @@ exports.zone = {
[(1480 - 360.5) * 60000, 300]
], i, source, expected;

tz.moveInvalidForward = false;

for (i = 0; i < tests.length; i++) {
source = tests[i][0];
expected = tests[i][1];
Expand Down

0 comments on commit c0fe03e

Please sign in to comment.