-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Replace dateutil.tz with ZoneInfo and tzlocal for better cross-platform compatibility. #40
Conversation
@@ -186,7 +192,7 @@ def read_preamble(self) -> int: | |||
# We do not use the timestamp pattern from the preamble as it may | |||
# be from other languages and therefore incompatible. | |||
# self.timestamp_format = self.metadata[METADATA_TIMESTAMP_PATTERN_KEY] | |||
self.timezone = dateutil.tz.gettz(self.metadata[METADATA_TZ_ID_KEY]) | |||
self.timezone = ZoneInfo(self.metadata[METADATA_TZ_ID_KEY]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The class ZoneInfo
is an implementation of tzinfo
abstracted class, shall we narrow down the type of self.timezone
to ZoneInfo
instead?
This means we should also update the type annotation in Log._decode
pyproject.toml
Outdated
"typing-extensions >= 3.7.4", | ||
"tzlocal >= 5.2", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
btw, according to the document, this lib needs Python >= 3.8. We need to support Python3.7.
PyPI: https://pypi.org/project/tzlocal/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's drop it to 5.1 then. That seems to be the last version that supports Python 3.7.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/clp_logging/handlers.py
Outdated
tz = "/".join([tzp.parent.name, tzp.name]) | ||
else: | ||
tz = "UTC" | ||
tz = tzlocal.get_localzone_name() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
from the official doc I didn't see examples w.r.t. error handling. But from the implementation details it seems like it could raise an exception or return None (according to their comments). Shall we add a try block to catch any exceptions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree. In fact when I was looking for a Python 3.7 compatible version of the lib, I found this from their docs:
tzlocal 4.0 also adds an official function get_localzone_name() to get only the timezone name, instead of a timezone object. On unix, it can raise an error if you don’t have a timezone name configured, where get_localzone() will succeed, so only use that if you need the timezone name.
Co-authored-by: Lin Zhihao <[email protected]>
Can you resolve the conflict and try the latest workflow? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commit msg suggestion:
Replace dateutil.tz with ZoneInfo and tzlocal for better cross-platform compatibility. (#40)
References
It was found on Windows that running below snippet would raise an exception of
AttributeError
.Example code:
Exception:
Further analysis found attribute
_filename
does not exist on the object returned bydateutil.tz.gettz()
because the Windows implementation is different than the Linux one, where the_filename
attribute does exist.Description
clp_logging/readers.py
to get time zone information with Python's built-in zoneinfo.ZoneInfo (with a fallback tobackports.zoneinfo.ZoneInfo
when the Python version is below 3.9).clp_logging/handlers.py
to get IANA current time zone name with third-party tzlocal library.backports.zoneinfo
to project requirements inpyproject.toml
for getting local time zone from IANA names.tzlocal
to project requirements inpyproject.toml
for getting local time zone name.Validation performed
Run this snippet on Windows without observing any errors:
Output: