From: Iskren Chernev Date: Mon, 23 Dec 2013 06:07:00 +0000 (-0800) Subject: Fix isSame for differently zoned moments X-Git-Tag: 2.5.0^2~1^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c7617c15409fdb2db52a0ea41bea74558fabe64;p=thirdparty%2Fmoment.git Fix isSame for differently zoned moments isSame uses startOf and then compares milliseconds, but this misses the case of different zones across the moments. startOf('day') in one timezone is different than the other, so it was never successful. --- diff --git a/moment.js b/moment.js index b97dff98b..c29b111b7 100644 --- a/moment.js +++ b/moment.js @@ -584,6 +584,12 @@ return key ? key.toLowerCase().replace('_', '-') : key; } + // Return a moment from input, that is local/utc/zone equivalent to model. + function makeAs(input, model) { + return model._isUTC ? moment(input).zone(model._offset || 0) : + moment(input).local(); + } + /************************************ Languages ************************************/ @@ -1826,7 +1832,7 @@ }, diff : function (input, units, asFloat) { - var that = this._isUTC ? moment(input).zone(this._offset || 0) : moment(input).local(), + var that = makeAs(input, this), zoneDiff = (this.zone() - that.zone()) * 6e4, diff, output; @@ -1977,8 +1983,8 @@ }, isSame: function (input, units) { - units = typeof units !== 'undefined' ? units : 'millisecond'; - return +this.clone().startOf(units) === +moment(input).startOf(units); + units = units || 'ms'; + return +this.clone().startOf(units) === +makeAs(input, this).startOf(units); }, min: function (other) { diff --git a/test/moment/is_same.js b/test/moment/is_same.js index 108141669..71fc25bb9 100644 --- a/test/moment/is_same.js +++ b/test/moment/is_same.js @@ -159,5 +159,14 @@ exports.is_same = { test.equal(m.isSame(m, 'millisecond'), true, "same moments are in the same millisecond"); test.equal(+m, +mCopy, "isSame millisecond should not change moment"); test.done(); + }, + + "is same with zone'd moments" : function (test) { + test.expect(3); + test.ok(moment.parseZone('2013-01-01T-05:00').isSame(moment('2013-01-01'), 'year'), "zoned vs local moment"); + test.ok(moment('2013-01-01').isSame(moment('2013-01-01').zone('-05:00'), 'year'), "local vs zoned moment"); + test.ok(moment.parseZone('2013-01-01T-05:00').isSame(moment.parseZone('2013-01-01T-06:30'), 'year'), + "zoned vs (differently) zoned moment"); + test.done(); } };