Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Two python datetime objects having the same timezone information are printed differently

I’d like to convert timezone of a Python’s datetime object, from US/Eastern to UTC.

What I did was first making datetime object of US/Eastern timezone, converting it to UTC timezone, and converting it back to the US/Eastern timezone. It is expected the first and last US/Eastern timezone datetime objects are identical. But it turned out that the two are printed differently.

What am I missing here?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Code:

from datetime import datetime
import pytz

tz_local = pytz.timezone('US/Eastern')
tz_utc = pytz.utc

datestring = '20210701'
timestring = '04:00:00'
hour, minute, sec = timestring.split(':')
hour, minute, sec = list(map(int, [hour, minute, sec]))

# Make naive datetime object from raw strings
date_naive = datetime.strptime(datestring, '%Y%m%d')
time_naive = date_naive.replace(hour=hour, minute=minute, second=sec)

# Add local timezone information US/Eastern
time_local = time_naive.replace(tzinfo=tz_local)

# Convert to UTC timezone
time_utc = time_local.astimezone(tz_utc)

# Revert to US/Eastern Timezone
time_local_rev = time_utc.astimezone(tz_local)

print(time_local.strftime('%Y-%m-%d %H:%M:%S %Z%z'))
print(time_local_rev.strftime('%Y-%m-%d %H:%M:%S %Z%z'))

Outputs:

2021-07-01 04:00:00 LMT-0456
2021-07-01 04:56:00 EDT-0400

Solution

As @MrFuppes noted, using .localize method instead of .replace solved the issue as follows

# Add local timezone information US/Eastern
time_local = tz_local.localize(time_naive)

Generated

2021-07-01 04:00:00 EDT-0400
2021-07-01 04:00:00 EDT-0400

>Solution :

If you can use Python 3.9 or higher, use the built-in zoneinfo library to avoid the "localize-trap". EX:

from datetime import datetime
from zoneinfo import ZoneInfo

tz_local = ZoneInfo('US/Eastern')
tz_utc = ZoneInfo('UTC')

datestring = '20210701'
timestring = '04:00:00'

# make a datetime object and set the time zone with replace:
dt_local = datetime.strptime(datestring+timestring, "%Y%m%d%H:%M:%S").replace(tzinfo=tz_local)

dt_utc = dt_local.astimezone(tz_utc)

print(dt_local)
# 2021-07-01 04:00:00-04:00
print(dt_utc)
# 2021-07-01 08:00:00+00:00
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading