Skip to content

Commit

Permalink
Merge pull request #53 from kroo/master
Browse files Browse the repository at this point in the history
Fix for add/subtract when crossing a tz offset

The tests would pass with moment version after (not including) 2.5.1.
  • Loading branch information
ichernev committed Feb 6, 2014
2 parents a522069 + 4d9fc3c commit 0fa84a4
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
13 changes: 9 additions & 4 deletions moment-timezone.js
Original file line number Diff line number Diff line change
Expand Up @@ -447,14 +447,19 @@
}

// overwrite moment.updateOffset
moment.updateOffset = function (mom) {
moment.updateOffset = function (mom, dontAdjustTime) {
var offset;
if (mom._z) {
offset = mom._z.offset(mom);
if (Math.abs(offset) < 16) {
offset = offset / 60;
if (dontAdjustTime) {
mom._offset = offset;
mom._isUTC = true;
} else {
if (Math.abs(offset) < 16) {
offset = offset / 60;
}
mom.zone(offset);
}
mom.zone(offset);
}
};

Expand Down
47 changes: 47 additions & 0 deletions tests/manipulate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
var moment = require("../index");

exports["manipulate"] = {
"add" : function (t) {
t.equal(
moment('2012-10-28 00:00:00 +01:00').tz('Europe/London').add('days', 1).format(),
'2012-10-29T00:00:00+00:00',
"adding 1 day while crossing a DST boundary should not affect time (BST -> GMT).");
t.equal(
moment('2013-11-03T00:00:00-07:00').tz('America/Los_Angeles').add(1, 'day').format(),
"2013-11-04T00:00:00-08:00",
"adding 1 day while crossing a DST boundary should not affect time (PDT-> PST).");
t.equal(
moment("2014-03-09T00:00:00-08:00").tz('America/Los_Angeles').add(1, 'day').format(),
"2014-03-10T00:00:00-07:00",
"adding 1 day while crossing a DST boundary should not affect time (PST -> PDT).");
t.done();
},
"subtract" : function (t) {
t.equal(
moment('2012-10-29 00:00:00 +00:00').tz('Europe/London').subtract('days', 1).format(),
'2012-10-28T00:00:00+01:00',
"subtracting 1 day while crossing a DST boundary should not affect time (GMT -> BST).");
t.equal(
moment("2013-11-04T00:00:00-08:00").tz('America/Los_Angeles').subtract(1, 'day').format(),
'2013-11-03T00:00:00-07:00',
"adding 1 day while crossing a DST boundary should not affect time (PST -> PDT).");
t.equal(
moment("2014-03-10T00:00:00-07:00").tz('America/Los_Angeles').subtract(1, 'day').format(),
"2014-03-09T00:00:00-08:00",
"adding 1 day while crossing a DST boundary should not affect time (PDT -> PST).");
t.done();
},
"month" : function (t) {
t.equal(
moment("2014-03-09T00:00:00-08:00").tz('America/Los_Angeles').add(1, 'month').format(),
"2014-04-09T00:00:00-07:00",
"adding 1 month while crossing a DST boundary should not affect time (PST -> PDT).");
t.equal(
moment("2014-03-09T00:00:00-08:00").tz('America/Los_Angeles').month("April").format(),
"2014-04-09T00:00:00-07:00",
"setting month across a DST boundary should not affect time (PST -> PDT).");

t.done();
}

};

0 comments on commit 0fa84a4

Please sign in to comment.