question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Weird behavior of DateTime.diff with winter time change

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
icambroncommented, Nov 2, 2021

Another solution is

afterTimeChange.diff(beforeTimeChange, "hours").as("days")
0reactions
vinassefranchecommented, Nov 2, 2021

Thanks @icambron I’ll try your solutions and see what works best for us!

Read more comments on GitHub >

github_iconTop Results From Across the Web

DateTime::diff and end of Daylight Saving Time - Stack Overflow
I've found a strange behaviour of DateTime::diff() when compared date are in different times: spring time and winter time. Any ideas how to...
Read more >
Considerations on Daylight Saving Time and .NET - PI Square
We can change this behavior by specifying the Kind of the DateTime struct to UTC in the constructor. Basically, we are working only...
Read more >
DateTime add and subtract & daylight saving time
The combination of daylight saving times and DateTime.add() and DateTime.subtract() can cause unpredictable problems.
Read more >
A Tour of Timezones (& Troubles) in R | R-bloggers
UTC is a special standard that has no timezone, no daylight savings time, or any of the other weird things that make timezones...
Read more >
7 "Tick-bait" gotchas for Date-Times in .NET (C#) - Pepperoni
A DateTimeOffset is effectively a DateTime and a TimeSpan. The TimeSpan is the offset from UTC. I.e. subtracting the timespan from the date...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found