Weird behavior of DateTime.diff with winter time change
See original GitHub issueDescribe the bug
DateTime.diff(..., 'days')
returns a weird number of days when the two DateTime are before and after the winter time change. With a fixed time to 00:00:00Z
on both dates, the diff in days returned is not an integer but slightly less (30.95833333
instead of 31
for example)
What is strange is that asking for the diff in hours and dividing it by 24 returns the integer while I would have thought it would not.
Asking DateTime to parse the same dates as utc
fixes the issue, FYI.
To Reproduce You can run it in this fiddle
const DateTime = luxon.DateTime;
// weird
const beforeTimeChange = DateTime.fromISO('2021-10-15T00:00:00Z', {zone: 'Europe/Andorra'});
const afterTimeChange = DateTime.fromISO('2021-11-15T00:00:00Z', {zone: 'Europe/Andorra'});
console.log(afterTimeChange.diff(beforeTimeChange, 'days').days); // => 30.958333333333332
console.log(afterTimeChange.diff(beforeTimeChange, 'hours').hours / 24); // => 31
// ok
const beforeTimeChangeOK = DateTime.fromISO('2021-10-15T00:00:00Z', {zone: 'utc'});
const afterTimeChangeOK = DateTime.fromISO('2021-11-15T00:00:00Z', {zone: 'utc'});
console.log(afterTimeChangeOK.diff(beforeTimeChangeOK, 'days').days); // => 31
console.log(afterTimeChangeOK.diff(beforeTimeChangeOK, 'hours').hours / 24); // => 31
Actual vs Expected behavior I would expect the diff in days to be an integer and maybe the diff in hours to be one hour less than one it is today.
Desktop (please complete the following information):
- OS: Ubuntu 18.04
- Browser: Brave 1.31.87
- Luxon version: 2.0.2
- Your timezone: not relevant
Additional context
Not directly related to the bug and just giving thoughts but I would be nice to make it clear that the DateTime.diff
method takes into account the timezone and is not done on utc. Maybe it would be clearer to have the DateTime.diff
be in utc
and have something like a DateTime.localDiff
which makes the diff in the timezone.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5 (3 by maintainers)
Another solution is
Thanks @icambron I’ll try your solutions and see what works best for us!