From: Tim Wood Date: Mon, 23 Apr 2012 23:25:34 +0000 (-0700) Subject: Merging in changes for 1.5.2 X-Git-Tag: 1.6.0~1^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3271ddcc3f71a4efc41bc971afab7784dc2f9931;p=thirdparty%2Fmoment.git Merging in changes for 1.5.2 --- diff --git a/moment.js b/moment.js index adea93514..794f48052 100644 --- a/moment.js +++ b/moment.js @@ -757,7 +757,7 @@ }, diff : function (input, val, asFloat) { - var inputMoment = moment(input), + var inputMoment = this._isUTC ? moment(input).utc() : moment(input).local(), zoneDiff = (this.zone() - inputMoment.zone()) * 6e4, diff = this._d - inputMoment._d - zoneDiff, year = this.year() - inputMoment.year(), @@ -767,7 +767,7 @@ if (val === 'months') { output = year * 12 + month + date / 30; } else if (val === 'years') { - output = year + month / 12; + output = year + (month + date / 30) / 12; } else { output = val === 'seconds' ? diff / 1e3 : // 1000 val === 'minutes' ? diff / 6e4 : // 1000 * 60 diff --git a/test/moment/create.js b/test/moment/create.js index 6f10f1906..88b520135 100644 --- a/test/moment/create.js +++ b/test/moment/create.js @@ -195,6 +195,21 @@ exports.create = { test.done(); }, + "cloning carrying over utc mode" : function(test) { + test.expect(8); + + test.equal(moment().local().clone()._isUTC, false, "An explicit cloned local moment should have _isUTC == false"); + test.equal(moment().utc().clone()._isUTC, true, "An cloned utc moment should have _isUTC == true"); + test.equal(moment().clone()._isUTC, false, "An explicit cloned local moment should have _isUTC == false"); + test.equal(moment.utc().clone()._isUTC, true, "An explicit cloned utc moment should have _isUTC == true"); + test.equal(moment(moment().local())._isUTC, false, "An implicit cloned local moment should have _isUTC == false"); + test.equal(moment(moment().utc())._isUTC, true, "An implicit cloned utc moment should have _isUTC == true"); + test.equal(moment(moment())._isUTC, false, "An implicit cloned local moment should have _isUTC == false"); + test.equal(moment(moment.utc())._isUTC, true, "An implicit cloned utc moment should have _isUTC == true"); + + test.done(); + }, + "parsing iso" : function(test) { var offset = moment([2011, 9, 08]).zone(); var pad = function(input) { diff --git a/test/moment/diff.js b/test/moment/diff.js index 9170ea7ac..504d16497 100644 --- a/test/moment/diff.js +++ b/test/moment/diff.js @@ -67,6 +67,28 @@ exports.diff = { test.equal(moment([2010, 0, 2]).diff([2010], 'hours'), 24, "hour diff"); test.equal(moment([2010, 0, 1, 2]).diff([2010], 'minutes'), 120, "minute diff"); test.equal(moment([2010, 0, 1, 0, 4]).diff([2010], 'seconds'), 240, "second diff"); + test.done(); + }, + + "diff between utc and local" : function(test) { + test.expect(7); + + test.equal(moment([2011]).utc().diff([2010], 'years'), 1, "year diff"); + test.equal(moment([2010, 2]).utc().diff([2010], 'months'), 2, "month diff"); + test.equal(moment([2010, 0, 4]).utc().diff([2010], 'days'), 3, "day diff"); + test.equal(moment([2010, 0, 21]).utc().diff([2010], 'weeks'), 3, "week diff"); + test.equal(moment([2010, 0, 1, 4]).utc().diff([2010], 'hours'), 4, "hour diff"); + test.equal(moment([2010, 0, 1, 0, 5]).utc().diff([2010], 'minutes'), 5, "minute diff"); + test.equal(moment([2010, 0, 1, 0, 0, 6]).utc().diff([2010], 'seconds'), 6, "second diff"); + + test.done(); + }, + + "year diffs include dates" : function(test) { + test.expect(1); + + test.ok(moment([2012, 1, 19]).diff(moment([2002, 1, 20]), 'years', true) < 10, "year diff should include date of month"); + test.done(); } };