0

I'm trying to calculate time difference between "now" and new years eve... for example. When I set the timezone info to "Europe/Rome" there is an error of 10min in calculation. Without the timezone info the error is 1h.

What am I doing wrong???

Here is an example code:

from datetime import datetime
import pytz

now   = datetime.now()
nowtz = datetime.now(tz=pytz.timezone("Europe/Rome"))

fut   = datetime(2022,12,31,23,59,59)
futtz = datetime(2022,12,31,23,59,59,tzinfo=pytz.timezone("Europe/Rome"))

delta = fut - now
deltatz = futtz - nowtz

print("Without timezone:")
print("Now: " + now.strftime("%Y/%m/%d %H:%M:%S"))
print("Tar: " + fut.strftime("%Y/%m/%d %H:%M:%S"))
print("Dif: " + str(delta))
print("")
print("With timezone:")
print("Now: " + nowtz.strftime("%Y/%m/%d %H:%M:%S"))
print("Tar: " + futtz.strftime("%Y/%m/%d %H:%M:%S"))
print("Dif: " + str(deltatz))

and an output:

Without timezone:
Now: 2021/10/05 14:12:09
Tar: 2022/12/31 23:59:59
Dif: 452 days, 9:47:49.933575

With timezone:
Now: 2021/10/05 14:12:09
Tar: 2022/12/31 23:59:59
Dif: 452 days, 10:57:49.908281

What is the correct way to calculate the time difference in python?

For a reference value I used: https://www.timeanddate.com/counters/newyear.html?p0=215

rv_normal
  • 80
  • 9
  • note: with Python 3.9 you can avoid the "localize" trap with `zoneinfo`, [see e.g. here](https://stackoverflow.com/a/63628816/10197418) – FObersteiner Oct 05 '21 at 12:43

1 Answers1

0

For whoever else stumbles across this problem, here is a solution:

from datetime import datetime
import pytz

tzone = pytz.timezone("Europe/Rome")

now   = datetime.now().astimezone()
nowtz = datetime.now(tz=tzone)

fut   = datetime(2021,12,31,23,59,59).astimezone()
futtz = tzone.localize(datetime(2021,12,31,23,59,59), is_dst=None)

delta = fut - now
deltatz = futtz - nowtz

print("Without timezone:")
print("Now: " + now.strftime("%Y/%m/%d %H:%M:%S"))
print("Tar: " + fut.strftime("%Y/%m/%d %H:%M:%S"))
print("Dif: " + str(delta))
print("")
print("With timezone:")
print("Now: " + nowtz.strftime("%Y/%m/%d %H:%M:%S"))
print("Tar: " + futtz.strftime("%Y/%m/%d %H:%M:%S"))
print("Dif: " + str(deltatz))

With output:

Without timezone:
Now: 2021/10/05 14:22:27
Tar: 2021/12/31 23:59:59
Dif: 87 days, 10:37:31.187800

With timezone:
Now: 2021/10/05 14:22:27
Tar: 2021/12/31 23:59:59
Dif: 87 days, 10:37:31.187783
rv_normal
  • 80
  • 9
  • Please write 2 lines on what was the issue and how you managed to fix as well. will help future readers, :) . – Equinox Oct 05 '21 at 12:27
  • @venky__ the point here is to use `localize` correctly. This has been asked many times before here on SO (the dupe I linked is just one example); [the docs](http://pytz.sourceforge.net/#localized-times-and-date-arithmetic) are clear about it, it's just not intuitive I guess. – FObersteiner Oct 05 '21 at 12:46
  • 1
    Yes, it's a dupe. `localize` is the answer. Although, it's clearly not obvious... based on number of ppl with the same issue. At least I learned to check/test my code even when it's simple. – rv_normal Oct 05 '21 at 12:49