Skip to content

Commit

Permalink
Fix negative zero bug in differenceInX functions (closes #692) (#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
leshakoss committed May 10, 2018
1 parent b7b593f commit 0bf2133
Show file tree
Hide file tree
Showing 21 changed files with 240 additions and 12 deletions.
14 changes: 14 additions & 0 deletions src/differenceInCalendarDays/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInCalendarDays', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInCalendarDays(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInCalendarISOWeekYears/test.js
Expand Up @@ -80,6 +80,20 @@ describe('differenceInCalendarISOWeekYears', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInCalendarISOWeekYears(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInCalendarISOWeeks/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInCalendarISOWeeks', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInCalendarISOWeeks(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInCalendarMonths/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInCalendarMonths', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInCalendarMonths(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInCalendarQuarters/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInCalendarQuarters', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInCalendarQuarters(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInCalendarWeeks/test.js
Expand Up @@ -117,6 +117,20 @@ describe('differenceInCalendarWeeks', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInCalendarWeeks(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInCalendarYears/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInCalendarYears', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInCalendarYears(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
7 changes: 3 additions & 4 deletions src/differenceInDays/index.js
Expand Up @@ -45,13 +45,12 @@ export default function differenceInDays (dirtyDateLeft, dirtyDateRight, dirtyOp
var sign = compareAsc(dateLeft, dateRight, dirtyOptions)
var difference = Math.abs(differenceInCalendarDays(dateLeft, dateRight, dirtyOptions))

// exit early if the days are the same
if (difference === 0) return 0

dateLeft.setDate(dateLeft.getDate() - sign * difference)

// Math.abs(diff in full days - diff in calendar days) === 1 if last calendar day is not full
// If so, result must be decreased by 1 in absolute value
var isLastDayNotFull = compareAsc(dateLeft, dateRight, dirtyOptions) === -sign
return sign * (difference - isLastDayNotFull)
var result = sign * (difference - isLastDayNotFull)
// Prevent negative zero
return result === 0 ? 0 : result
}
9 changes: 4 additions & 5 deletions src/differenceInDays/test.js
Expand Up @@ -70,18 +70,17 @@ describe('differenceInDays', function () {
assert(result === 0)
})

it('does not return -0 when given dates are the same', () => {
// thank you Chris West! @ChrisWestBlog, http://cwestblog.com/2014/02/25/javascript-testing-for-negative-zero/
function isNegative (n) {
return ((n = +n) || 1 / n) < 0
it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInDays(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegative(result)
var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInHours/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInHours', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInHours(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
4 changes: 3 additions & 1 deletion src/differenceInISOWeekYears/index.js
Expand Up @@ -45,5 +45,7 @@ export default function differenceInISOWeekYears (dirtyDateLeft, dirtyDateRight,
// if last calendar ISO year is not full
// If so, result must be decreased by 1 in absolute value
var isLastISOWeekYearNotFull = compareAsc(dateLeft, dateRight, dirtyOptions) === -sign
return sign * (difference - isLastISOWeekYearNotFull)
var result = sign * (difference - isLastISOWeekYearNotFull)
// Prevent negative zero
return result === 0 ? 0 : result
}
14 changes: 14 additions & 0 deletions src/differenceInISOWeekYears/test.js
Expand Up @@ -80,6 +80,20 @@ describe('differenceInISOWeekYears', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInISOWeekYears(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInMilliseconds/test.js
Expand Up @@ -37,6 +37,20 @@ describe('differenceInMilliseconds', function () {
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInMilliseconds(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})

it('returns NaN if the first date is `Invalid Date`', function () {
var result = differenceInMilliseconds(
new Date(NaN),
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInMinutes/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInMinutes', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInMinutes(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
4 changes: 3 additions & 1 deletion src/differenceInMonths/index.js
Expand Up @@ -41,5 +41,7 @@ export default function differenceInMonths (dirtyDateLeft, dirtyDateRight, dirty
// Math.abs(diff in full months - diff in calendar months) === 1 if last calendar month is not full
// If so, result must be decreased by 1 in absolute value
var isLastMonthNotFull = compareAsc(dateLeft, dateRight, dirtyOptions) === -sign
return sign * (difference - isLastMonthNotFull)
var result = sign * (difference - isLastMonthNotFull)
// Prevent negative zero
return result === 0 ? 0 : result
}
14 changes: 14 additions & 0 deletions src/differenceInMonths/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInMonths', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInMonths(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInQuarters/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInQuarters', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInQuarters(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInSeconds/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInSeconds', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInSeconds(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
14 changes: 14 additions & 0 deletions src/differenceInWeeks/test.js
Expand Up @@ -69,6 +69,20 @@ describe('differenceInWeeks', function () {
)
assert(result === 0)
})

it('does not return -0 when the given dates are the same', () => {
function isNegativeZero (x) {
return x === 0 && (1 / x < 0)
}

var result = differenceInWeeks(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
)

var resultIsNegative = isNegativeZero(result)
assert(resultIsNegative === false)
})
})

it('returns NaN if the first date is `Invalid Date`', function () {
Expand Down
4 changes: 3 additions & 1 deletion src/differenceInYears/index.js
Expand Up @@ -41,5 +41,7 @@ export default function differenceInYears (dirtyDateLeft, dirtyDateRight, dirtyO
// Math.abs(diff in full years - diff in calendar years) === 1 if last calendar year is not full
// If so, result must be decreased by 1 in absolute value
var isLastYearNotFull = compareAsc(dateLeft, dateRight, dirtyOptions) === -sign
return sign * (difference - isLastYearNotFull)
var result = sign * (difference - isLastYearNotFull)
// Prevent negative zero
return result === 0 ? 0 : result
}

0 comments on commit 0bf2133

Please sign in to comment.