From: Rocky Meza Date: Tue, 31 Jan 2012 23:29:20 +0000 (-0700) Subject: make diff across DST work X-Git-Tag: 1.4.0~15^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F149%2Fhead;p=thirdparty%2Fmoment.git make diff across DST work --- diff --git a/moment.js b/moment.js index dda6d16b0..37fffe2ab 100644 --- a/moment.js +++ b/moment.js @@ -532,11 +532,13 @@ diff : function (input, val, asFloat) { var inputMoment = moment(input), - diff = this._d - inputMoment._d, + zoneDiff = (this.zone() - inputMoment.zone()) * 6e4, + diff = this._d - inputMoment._d - zoneDiff, year = this.year() - inputMoment.year(), month = this.month() - inputMoment.month(), date = this.date() - inputMoment.date(), output; + if (val === 'months') { output = year * 12 + month + date / 30; } else if (val === 'years') { diff --git a/sitesrc/js/unit-tests.js b/sitesrc/js/unit-tests.js index ae2f5c660..c8cb2a98f 100755 --- a/sitesrc/js/unit-tests.js +++ b/sitesrc/js/unit-tests.js @@ -294,8 +294,9 @@ test("diff month", 1, function() { equal(moment([2011, 0, 31]).diff([2011, 2, 1], 'months'), -1, "month diff"); }); -test("diff week", 1, function() { - equal(moment([2012, 2, 18]).diff([2012], 'weeks'), 12, "week diff"); +test("diff across DST", 2, function() { + equal(moment([2012, 2, 24]).diff([2012, 2, 10], 'weeks', true), 2, "diff weeks across DST"); + equal(moment([2012, 2, 24]).diff([2012, 2, 10], 'days', true), 14, "diff weeks across DST"); }); test("diff overflow", 4, function() {