arrow icon indicating copy to clipboard operation
arrow copied to clipboard

Error in datediff calculation

Open Gravitar64 opened this issue 3 years ago • 3 comments
trafficstars

If i calculate the time difference between 2 datetime objects, the result is incorrect. Here an example:

import arrow start = arrow.get('28.04.2022 14:00','DD.MM.YYYY HH:mm' ) end = arrow.now() print(f'Start-Datetime: {start}') print(f'End-Datetime : {end}') print(f'Time-Difference: {end-start}')

Start-Datetime: 2022-04-28T14:00:00+00:00 End-Datetime : 2022-04-28T16:17:06.144073+02:00 Time-Difference: 0:17:06.144073

The correct Time-Difference should be 2:17:06......

System Info

  • 🖥 Windows 10 21H1:
  • 🐍 Python 3.9.9:
  • 🏹 Arrow 1.2.1:

Gravitar64 avatar Apr 28 '22 14:04 Gravitar64

seems to be correct, the timezones differ by 2 hours, which makes up in the resulting time difference.

try specifying the timezones explicitly for now(), as it uses the local timezone, or switch to using utcnow().

krisfremen avatar Apr 28 '22 14:04 krisfremen

Hi,

ok, I replaced the tzinfo for start to "Europe/Berlin". But that has no effect

start = arrow.now()
start.replace(tzinfo='Europe/Berlin')
end = arrow.get('30.04.2022 13:00','DD.MM.YYYY HH:mm')
print(f'Start-Datetime: {start}')
print(f'End-Datetime : {end}')
print(f'Time-Difference: {end-start}')

Start-Datetime: 2022-04-30T12:22:02.977434+02:00 End-Datetime : 2022-04-30T13:00:00+00:00 Time-Difference: 2:37:57.022566

The correct Time-Difference should be 00:37:57......

Gravitar64 avatar Apr 30 '22 10:04 Gravitar64

@Gravitar64 start and end are still in different timezones, you need to specify it for both of them.

systemcatch avatar May 19 '22 10:05 systemcatch