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