Skip to content Skip to sidebar Skip to footer

Why Does It Make A Difference If I Add 1 Hour To The Utc Time And Localize Or 1 Hour To The Local Time?

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 =

Solution 1:

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:

from datetime import datetime, timezone, timedelta
import pytz

# Timezone-aware object to start with
t11 = datetime(1918, 4, 15, 0, 0, tzinfo=timezone.utc).astimezone(pytz.timezone('Europe/Berlin'))

# Extract timezone information
tzinfo = t11.tzinfo

# Convert to UTC, add timedelta, convert to local timezone
t13 = (t11.astimezone(pytz.timezone('utc')) + timedelta(hours=1)).astimezone(tzinfo)

Another way to do it:

t14 = t11 + timedelta(hours=1)  # Invalid timezone!t14 = t14.astimezone(pytz.utc).astimezone(t14.tzinfo)  # Fix the timezone

Now I have:

t11:1918-04-15 01:00:00+01:00t13:1918-04-15 03:00:00+02:00# one hour more and +1h because of DST

Pendulum

The package pendulum is another way to fix it:

from pendulum import datetime
from datetime import timedelta
import pytz

t11 = datetime(1918, 4, 15, 0, 0).astimezone(pytz.timezone('Europe/Berlin'))
t12 = t11 + timedelta(hours=1)

t2 = datetime(1918, 4, 15, 1, 0).astimezone(pytz.timezone('Europe/Berlin'))

gives:

t11:1918-04-15T01:00:00+01:00t12:1918-04-15T03:00:00+02:00t2 :1918-04-15T03:00:00+02:00

Post a Comment for "Why Does It Make A Difference If I Add 1 Hour To The Utc Time And Localize Or 1 Hour To The Local Time?"