spencermountain / spacetime

A lightweight javascript timezone library
http://spacetime.how/
Other
3.98k stars 183 forks source link

Diff returning incorrect value #184

Closed alexkmerz closed 4 years ago

alexkmerz commented 4 years ago

I am currently experiencing an issue where the diff function is off by 1 in certain situations.

The diff in days between 2019-01-01 and 2020-04-10 is showing as 465 days, however, the diff in days between 2019-01-01 and 2020-04-17 is showing as 473 days.

Since the dates are only 7 days apart I would expect the value to return as 472.

Replication code below:

let lastyear = spacetime('2019-01-01', 'Pacific/Auckland');
let thisyear = spacetime('2020-01-01', 'Pacific/Auckland');

let pass = spacetime('2020-04-10', 'Pacific/Auckland');
let fail = spacetime('2020-04-17', 'Pacific/Auckland');

console.log('Last year to pass diff: ' + lastyear.diff(pass, 'days'));
console.log('Last year to fail diff: ' + lastyear.diff(fail, 'days'));
console.log('This year to pass diff: ' + thisyear.diff(pass, 'days'));
console.log('This year to fail diff: ' + thisyear.diff(fail, 'days'));

console.log('Last year to pass diff: ' + lastyear.diff(pass, 'days'));
console.log('Last year to pass + 1 week diff: ' + lastyear.diff(pass.add(1, 'week'), 'days'));
console.log('This year to pass diff: ' + thisyear.diff(pass, 'days'));
console.log('This year to pass + 1 week diff: ' + thisyear.diff(pass.add(1, 'week'), 'days'));`
spencermountain commented 4 years ago

whoa, good find @alexkmerz . thank you.

so Pacific/Auckland does have a DST change in that interval (04/07) - my spider-sense is saying this is the culprit. Will address this in the next release. Let me know if you find anything else. thanks

spencermountain commented 4 years ago

hey @alexkmerz I think this is fixed now, as of v6.6.0.

let lastyear = spacetime('2019-01-01', 'Pacific/Auckland')
let pass = spacetime('2020-04-10', 'Pacific/Auckland')
let fail = spacetime('2020-04-17', 'Pacific/Auckland')

console.log('Last year to pass diff: ' + lastyear.diff(pass, 'days'))
console.log('Last year to fail diff: ' + lastyear.diff(fail, 'days'))
// Last year to pass diff: 465
// Last year to fail diff: 472

let me know if you are still seeing anything funny. thank you!