From: maggie@tempworks.com Date: Wed, 17 Feb 2016 23:58:39 +0000 (-0600) Subject: change add subtract to handle decimal values by abs rounding X-Git-Tag: 2.12.0~16^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4aba8bbafa8e5497c0985f3713d504d5f918e4f0;p=thirdparty%2Fmoment.git change add subtract to handle decimal values by abs rounding --- diff --git a/src/lib/moment/add-subtract.js b/src/lib/moment/add-subtract.js index 988eff321..dd3333323 100644 --- a/src/lib/moment/add-subtract.js +++ b/src/lib/moment/add-subtract.js @@ -3,6 +3,8 @@ import { setMonth } from '../units/month'; import { createDuration } from '../duration/create'; import { deprecateSimple } from '../utils/deprecate'; import { hooks } from '../utils/hooks'; +import absRound from '../utils/abs-round'; + // TODO: remove 'name' arg after deprecation is removed function createAdder(direction, name) { @@ -23,8 +25,8 @@ function createAdder(direction, name) { export function addSubtract (mom, duration, isAdding, updateOffset) { var milliseconds = duration._milliseconds, - days = duration._days, - months = duration._months; + days = absRound(duration._days), + months = absRound(duration._months); if (!mom.isValid()) { // No op diff --git a/src/lib/utils/abs-round.js b/src/lib/utils/abs-round.js new file mode 100644 index 000000000..98f54bc68 --- /dev/null +++ b/src/lib/utils/abs-round.js @@ -0,0 +1,7 @@ +export default function absRound (number) { + if (number < 0) { + return Math.round(-1 * number) * -1; + } else { + return Math.round(number); + } +} diff --git a/src/test/moment/add_subtract.js b/src/test/moment/add_subtract.js index 3a4077e59..0a0cfcc38 100644 --- a/src/test/moment/add_subtract.js +++ b/src/test/moment/add_subtract.js @@ -323,3 +323,23 @@ test('add across DST', function (assert) { assert.equal(c.hours(), 5, 'adding months over DST difference should result in the same hour'); assert.equal(e.hours(), 5, 'adding quarters over DST difference should result in the same hour'); }); + +test('add decimal values of days and months', function (assert) { + assert.equal(moment([2016,3,3]).add(1.5, 'days').date(), 5, 'adding 1.5 days is rounded to adding 2 day'); + assert.equal(moment([2016,3,3]).add(-1.5, 'days').date(), 1, 'adding -1.5 days is rounded to adding -2 day'); + assert.equal(moment([2016,3,1]).add(-1.5, 'days').date(), 30, 'adding -1.5 days on first of month wraps around'); + assert.equal(moment([2016,3,3]).add(1.5, 'months').month(), 5, 'adding 1.5 months adds 2 month'); + assert.equal(moment([2016,3,3]).add(-1.5, 'months').month(), 1, 'adding -1.5 months adds -1 month'); + assert.equal(moment([2016,0,3]).add(-1.5, 'months').month(), 10, 'adding -1.5 months at start of year wraps back'); + assert.equal(moment([2016,3,3]).subtract(1.5, 'days').date(),1, 'subtract 1.5 days is rounded to subtract 2 day'); + assert.equal(moment([2016,3,2]).subtract(1.5, 'days').date(), 31, 'subtract 1.5 days subtracts 2 days'); + assert.equal(moment([2016,1,1]).subtract(1.1, 'days').date(), 31, 'subtract 1.1 days wraps to previous month'); + assert.equal(moment([2016,3,3]).subtract(-1.5, 'days').date(), 5, 'subtract -1.5 days is rounded to subtract -1 day'); + assert.equal(moment([2016,3,30]).subtract(-1.5, 'days').date(), 2, 'subtract -1.5 days on last of month wraps around'); + assert.equal(moment([2016,3,3]).subtract(1.5, 'months').month(), 1, 'subtract 1.5 months subtract 2 months'); + assert.equal(moment([2016,3,3]).subtract(-1.5, 'months').month(), 5, 'subtract -1.5 months subtract -1 month'); + assert.equal(moment([2016,11,31]).subtract(-1.5, 'months').month(),1, 'subtract -1.5 months at end of year wraps back'); + assert.equal(moment([2016, 0,1]).add(1.5, 'years').format('YYYY-MM-DD'), '2017-07-01', 'add 1.5 years adds 1 year six months'); + assert.equal(moment([2016, 0,1]).add(1.6, 'years').format('YYYY-MM-DD'), '2017-08-01', 'add 1.6 years becomes 1.6*12 = 19.2, round, 19 months'); + assert.equal(moment([2016,0,1]).add(1.1, 'quarters').format('YYYY-MM-DD'), '2016-04-01', 'add 1.1 quarters 1.1*3=3.3, round, 3 months'); +});