Example Code
from datetime import datetime, timezone, timedelta
import pytz
t11 = datetime(1918, 4, 15, 0, 0, tzinfo=timezone.utc).astimezone(pytz.timezone('Europe/Berlin'))
t12 = t11 + timedelta(hours=1)
t2 = datetime(1918, 4, 15, 1, 0, tzinfo=timezone.utc).astimezone(pytz.timezone('Europe/Berlin'))
print(t12)
print(t2)
Observed
1918-04-15 02:00:00+01:00
1918-04-15 03:00:00+02:00
Expected
I expected both to be what I see for t2
. The crucial difference is t2.hour
vs t12.hour
. For a timezone-aware datetime object, I expected the hour to be the local hour.
Question
How can I change this behaviour? What is the reason for having it like this?
I will not accept the following, because it only explains how to do it right. It doesn't explain why adding timedelta doesn't work the expected way in the first place.
How to fix it
This answer suggests to take the following approach:
Another way to do it:
Now I have:
Pendulum
The package
pendulum
is another way to fix it:gives: