bux bux - 27 days ago 20
Python Question

Misunderstood timezone conversion

I'm converting a datetime into an other timezone (from Europe/Paris to America/Guadeloupe). But result is not what i expect:

import pytz
import datetime

tzinfo = pytz.timezone('Europe/Paris')
datetime_with_timezone = datetime.datetime(
2000, 1, 1, 0, 0, 0, tzinfo=tzinfo
)

print(datetime_with_timezone)
new_tzinfo = pytz.timezone('America/Guadeloupe')
print(datetime_with_timezone.astimezone(new_tzinfo))


Produce:

2000-01-01 00:00:00+00:09
1999-12-31 19:51:00-04:00


Why
datetime_with_timezone
contains
+00:09
?

Answer

This seems to be a known issue, as according to the pytz documentation:

Unfortunately using the tzinfo argument of the standard datetime constructors ‘’does not work’’ with pytz for many timezones.

The documentation provides further advice that

The preferred way of dealing with times is to always work in UTC, converting to localtime only when generating output to be read by humans.

Comments