Skip to content

Commit

Permalink
fix bug where diff returns -0 in month-related diffs
Browse files Browse the repository at this point in the history
  • Loading branch information
maggie@tempworks.com authored and ichernev committed Apr 16, 2016
1 parent e52fd3a commit 9a7c546
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib/moment/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ function monthDiff (a, b) {
adjust = (b - anchor) / (anchor2 - anchor);
}

return -(wholeMonthDiff + adjust);
//check for negative zero, return zero if negative zero
return -(wholeMonthDiff + adjust) || 0;
}
9 changes: 9 additions & 0 deletions src/test/moment/diff.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,3 +232,12 @@ test('year diffs', function (assert) {
equal(assert, moment([2012, 0, 1]).diff([2013, 6, 1, 12], 'years', true), -1.5 - (0.5 / 31) / 12, 'Jan 1 2012 to Jul 1 2013 noon should be 1.5+(0.5 / 31) / 12 years');
equal(assert, moment([2012, 1, 29]).diff([2013, 1, 28], 'years', true), -1, 'Feb 29 2012 to Feb 28 2013 should be 1-(1 / 28.5) / 12 years');
});

test('negative zero', function (assert) {
function isNegative (n) {
return (1 / n) < 0;
}
assert.ok(!isNegative(moment([2012, 0, 1]).diff(moment([2012, 0, 1]), 'months')), 'month diff on same date is zero, not -0');
assert.ok(!isNegative(moment([2012, 0, 1]).diff(moment([2012, 0, 1]), 'years')), 'year diff on same date is zero, not -0');
assert.ok(!isNegative(moment([2012, 0, 1]).diff(moment([2012, 0, 1]), 'quarters')), 'quarter diff on same date is zero, not -0');
});

0 comments on commit 9a7c546

Please sign in to comment.