Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix differenceInDays across DST (#533) #1630

Merged
merged 3 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 4 additions & 12 deletions src/differenceInDays/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import toDate from '../toDate/index.js'
import differenceInCalendarDays from '../differenceInCalendarDays/index.js'
import compareAsc from '../compareAsc/index.js'
import requiredArgs from '../_lib/requiredArgs/index.js'

/**
Expand Down Expand Up @@ -42,15 +40,9 @@ export default function differenceInDays(dirtyDateLeft, dirtyDateRight) {
var dateLeft = toDate(dirtyDateLeft)
var dateRight = toDate(dirtyDateRight)

var sign = compareAsc(dateLeft, dateRight)
var difference = Math.abs(differenceInCalendarDays(dateLeft, dateRight))
var result = (dateLeft - dateRight) / 86400000
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move 86400000 into a var MILLISECONDS_IN_DAY

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dkozickis moved


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) === -sign
var result = sign * (difference - isLastDayNotFull)
// Prevent negative zero
return result === 0 ? 0 : result
// round towards zero
if (result > 0) return Math.floor(result)
return Math.ceil(result)
}
10 changes: 10 additions & 0 deletions src/differenceInDays/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ describe('differenceInDays', function() {
assert(result === 0)
})

it('ignores any localtime differences with DST', function() {
const a = new Date('2019-10-05T15:00:00.000Z') // for TZ=Australia/Sydney, this is 1 hour before DST
const b = new Date('2019-10-06T14:00:00.000Z') // for TZ=Australia/Sydney, this is 1 day after DST
const c = new Date('2019-10-07T14:00:00.000Z') // for TZ=Australia/Sydney, this is 2 days after DST

assert(differenceInDays(c, b) === 1)
assert(differenceInDays(b, a) === 0) // 23 hours < 1 day
assert(differenceInDays(c, a) === 1) // 47 hours < 2 days
})

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