What's the Deal with Python's Timezone Handling: pytz vs. zoneinfo?

June 5, 2025 5 answers
Asked by lat45N_grid
I recently stumbled upon an issue while working with Python's datetime library. I was using `pytz` to set the timezone directly with `tzinfo`, and it ended up applying the old Local Mean Time (LMT), which made my datetime shift by 6 minutes unexpectedly. Here's a quick example: ```python from datetime import datetime import pytz dt_lmt = datetime(2021, 3, 25, 19, 0, tzinfo=pytz.timezone('Asia/Shangai')) print(dt_lmt.utcoffset()) # This shows an offset of 8:06:00 ``` However, when I switched to using `zoneinfo`, everything worked out perfectly: ```python from datetime import datetime from zoneinfo import ZoneInfo dt = datetime(2021, 3, 25, 19, 0, tzinfo=ZoneInfo("Asia/Shangai")) print(dt) # Outputs 2021-03-25 19:00:00+08:00 print(dt.utcoffset()) # Correctly shows 8:00:00 ``` This experience has made me wonder if I should just stick to using the standard library's `zoneinfo` from now on. How do other developers view the comparison between `pytz` and `zoneinfo`?

Answers

cinnamon roll debate
You're definitely not alone! I find working with the datetime library to be a bit of a headache too. Often feels like I need to write 10 different test cases just to get basic functionality working right, especially with timezones and differences in datetimes.

neat enough: Yeah, datetime can be tricky! It's one of those libraries that requires a lot of reading to really get the hang of it. It's so much easier in languages that have better datetime support.

graph+traversal: Same here! I think it's just a matter of getting used to it and figuring out what works best for your needs.

Reply to this answer
ground cover
Totally get where you're coming from! This is exactly why the `pytz` documentation recommends using `localize` instead of setting `tzinfo` directly. With Python 3.9 and up, `zoneinfo` is the way to go since it's now included with the standard library. You won't really miss `pytz` unless you're maintaining old code. Just stick to `zoneinfo` for cleaner handling!

focaccia.dough: Exactly! If you're in that version range, there's no reason to stay with `pytz`. It's always better to keep your codebase clean and modern.

Reply to this answer
Zinnia
Been there too! Just to highlight, the `pytz` docs already hint that for Python 3.9 and later, you should use `zoneinfo`. It really doesnโ€™t offer any more benefits than backward compatibility, and you might run into issues with timezone data past 2038. If you have to use it, make sure to read up on how to use `localize` and `normalize` properly.
Reply to this answer
thesis pending
Looks like there's a misunderstanding with `pytz` usage here. When you want to set time zone info, you should use `pytz.timezone('').localize(datetime.datetime())`. This way, you get accurate timezone handling without those weird offsets.
Reply to this answer
snorkel mask fit!
I'm just about ready to give up on `pytz` entirely. If you're working with Python 3.9+, embrace `zoneinfo`. Itโ€™s a lot simpler and more reliable for handling timezones without those quirks like LMT.
Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions