regebro / tzlocal

A Python module that tries to figure out what your local timezone is
MIT License
184 stars 58 forks source link

[3.0] AttributeError: 'backports.zoneinfo.ZoneInfo' object has no attribute 'zone' [Python 3.8] #112

Closed dongryphon closed 2 years ago

dongryphon commented 2 years ago

We just got the 3.0 version and hit the above error. We are using python 3.8 and were previously using tzlocal 2.1.

Is this a regression or an API change? What is the correct way to extract the tz name using the backports module flavor?

Thanks!

regebro commented 2 years ago

It's an api change, in version 3 the returned objects are no longer pytz objects, but zoneinfo objects, zoneinfo being the new timezone support in the stdlib. This, and dropping Python 2 support, are the only changes from 2.1 to 3.0.

The time zone name is in the .key attribute of the ZoneInfo object. If tzlocal didn't find a time zone name, the name will be "local".

dongryphon commented 2 years ago

Thanks for the clarification. I'll close this then.

regebro commented 2 years ago

The new 4.0b3 release uses a shim that is backwards compatible with pytz, so .zone should work there, but with a deprecation warning. Using str(timezone) is the new way.

Also, there is a get_localzone_name() method if you really want a name, and not a zone.

regebro commented 2 years ago

4.0 is now released, so this should no longer be an issue, I hope.