From: Iskren Chernev Date: Thu, 25 Apr 2013 07:51:10 +0000 (-0700) Subject: Ignore DST for day, week, month and year diffs X-Git-Tag: 2.1.0~19^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5cf3e300465d2a465cba1902372aebd2f4649768;p=thirdparty%2Fmoment.git Ignore DST for day, week, month and year diffs --- diff --git a/moment.js b/moment.js index fd67ba564..15b1b633d 100644 --- a/moment.js +++ b/moment.js @@ -1228,19 +1228,27 @@ units = normalizeUnits(units); if (units === 'year' || units === 'month') { + // average number of days in the months in the given dates diff = (this.daysInMonth() + that.daysInMonth()) * 432e5; // 24 * 60 * 60 * 1000 / 2 + // difference in months output = ((this.year() - that.year()) * 12) + (this.month() - that.month()); - output += ((this - moment(this).startOf('month')) - (that - moment(that).startOf('month'))) / diff; + // adjust by taking difference in days, average number of days + // and dst in the given months. + output += ((this - moment(this).startOf('month')) - + (that - moment(that).startOf('month'))) / diff; + // same as above but with zones, to negate all dst + output -= ((this.zone() - moment(this).startOf('month').zone()) - + (that.zone() - moment(that).startOf('month').zone())) * 6e4 / diff; if (units === 'year') { output = output / 12; } } else { - diff = (this - that) - zoneDiff; + diff = (this - that); output = units === 'second' ? diff / 1e3 : // 1000 units === 'minute' ? diff / 6e4 : // 1000 * 60 units === 'hour' ? diff / 36e5 : // 1000 * 60 * 60 - units === 'day' ? diff / 864e5 : // 1000 * 60 * 60 * 24 - units === 'week' ? diff / 6048e5 : // 1000 * 60 * 60 * 24 * 7 + units === 'day' ? (diff - zoneDiff) / 864e5 : // 1000 * 60 * 60 * 24, negate dst + units === 'week' ? (diff - zoneDiff) / 6048e5 : // 1000 * 60 * 60 * 24 * 7, negate dst diff; } return asFloat ? output : absRound(output); diff --git a/test/moment/diff.js b/test/moment/diff.js index 957624582..c3f2f8c72 100644 --- a/test/moment/diff.js +++ b/test/moment/diff.js @@ -91,10 +91,17 @@ exports.diff = { }, "diff across DST" : function(test) { - test.expect(2); + test.expect(9); test.equal(moment([2012, 2, 24]).diff([2012, 2, 10], 'weeks', true), 2, "diff weeks across DST"); - test.equal(moment([2012, 2, 24]).diff([2012, 2, 10], 'days', true), 14, "diff weeks across DST"); + test.equal(moment([2012, 2, 24]).diff([2012, 2, 10], 'days', true), 14, "diff days across DST"); + test.equal(moment([2012, 2, 11]).diff([2012, 2, 11, 12], 'hours', true), -11, "diff hours across DST"); + test.equal(moment([2012, 2, 11]).diff([2012, 2, 11, 12], 'days', true), -0.5, "diff days across DST"); + test.equal(moment([2012, 2, 11]).diff([2012, 2, 11, 12], 'months', true), -1/62, "diff months across DST (half day in 31 day month)"); + test.equal(moment([2012, 2, 11]).diff([2012, 2, 11, 12], 'months', true), -1/62, "diff months across DST (half day in 31 day month)"); + test.equal(moment([2013, 10, 2]).diff([2013, 10, 17], 'months', true), -0.5, 'diff months across DST (15 days in 30 day month)') + test.equal(moment([2013, 10, 2]).diff([2013, 10, 17], 'days', true), -15, 'diff days across DST') + test.equal(moment([2013, 10, 2]).diff([2013, 10, 17], 'hours', true), -15 * 24 - 1, 'diff hours across DST (15 days minus 1 hour DST)') test.done(); },