Skip to content

Commit 6d355d3

Browse files
authored
Replace dateutil.tz with ZoneInfo and tzlocal for better cross-platform compatibility. (#40)
1 parent 70ca462 commit 6d355d3

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ description = "Logging/encoding/decoding using CLP's IR stream format"
1313
readme = "README.md"
1414
requires-python = ">=3.7"
1515
dependencies = [
16+
"backports.zoneinfo >= 0.2.1; python_version < '3.9'",
1617
"clp-ffi-py >= 0.0.11",
17-
"python-dateutil >= 2.7.0",
1818
"typing-extensions >= 3.7.4",
19+
"tzlocal == 5.1; python_version < '3.8'",
20+
"tzlocal >= 5.2; python_version >= '3.8'",
1921
"zstandard >= 0.18.0",
2022
]
2123
classifiers = [

src/clp_logging/handlers.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import sys
55
import time
66
from abc import ABCMeta, abstractmethod
7-
from datetime import tzinfo
87
from math import floor
98
from pathlib import Path
109
from queue import Empty, Queue
@@ -13,7 +12,7 @@
1312
from types import FrameType
1413
from typing import Callable, ClassVar, Dict, IO, Optional, Tuple, Union
1514

16-
import dateutil.tz
15+
import tzlocal
1716
from clp_ffi_py.ir import FourByteEncoder
1817
from zstandard import FLUSH_FRAME, ZstdCompressionWriter, ZstdCompressor
1918

@@ -59,12 +58,11 @@ def _init_timeinfo(fmt: Optional[str], tz: Optional[str]) -> Tuple[str, str]:
5958
if not fmt:
6059
fmt = "yyyy-MM-d H:m:s.A"
6160
if not tz:
62-
tzf: Optional[tzinfo] = dateutil.tz.gettz()
63-
if tzf:
64-
tzp: Path = Path.resolve(Path(tzf._filename)) # type: ignore
65-
tz = "/".join([tzp.parent.name, tzp.name])
66-
else:
61+
try:
62+
tz = tzlocal.get_localzone_name()
63+
except Exception:
6764
tz = "UTC"
65+
6866
return fmt, tz
6967

7068

src/clp_logging/readers.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from abc import ABCMeta, abstractmethod
2-
from datetime import datetime, tzinfo
2+
from datetime import datetime
33
from pathlib import Path
44
from sys import stderr
55
from types import TracebackType
66
from typing import IO, Iterator, List, Match, Optional, Tuple, Type, Union
77

8-
import dateutil.tz
98
from clp_ffi_py.ir import FourByteEncoder
109
from zstandard import ZstdDecompressionReader, ZstdDecompressor
1110

@@ -31,6 +30,13 @@
3130
VAR_COMPACT_ENCODING,
3231
)
3332

33+
try:
34+
from zoneinfo import ZoneInfo # type: ignore[import-not-found, unused-ignore]
35+
except ImportError:
36+
from backports.zoneinfo import ( # type: ignore[import-not-found, no-redef, unused-ignore]
37+
ZoneInfo,
38+
)
39+
3440

3541
class Log:
3642
"""
@@ -61,7 +67,7 @@ def __init__(self) -> None:
6167
def __str__(self) -> str:
6268
return self.formatted_msg
6369

64-
def _decode(self, timestamp_format: Optional[str], timezone: Optional[tzinfo]) -> int:
70+
def _decode(self, timestamp_format: Optional[str], timezone: Optional[ZoneInfo]) -> int:
6571
"""
6672
Populate the `variables`, `msg`, and `formatted_msg` fields by decoding
6773
the encoded `encoded_logtype and `encoded_variables`.
@@ -148,7 +154,7 @@ def __init__(self, timestamp_format: Optional[str], chunk_size: int) -> None:
148154
self.metadata: Optional[Metadata] = None
149155
self.last_timestamp_ms: int
150156
self.timestamp_format: Optional[str] = timestamp_format
151-
self.timezone: Optional[tzinfo]
157+
self.timezone: Optional[ZoneInfo]
152158
self.pos: int
153159

154160
def read_preamble(self) -> int:
@@ -186,7 +192,7 @@ def read_preamble(self) -> int:
186192
# We do not use the timestamp pattern from the preamble as it may
187193
# be from other languages and therefore incompatible.
188194
# self.timestamp_format = self.metadata[METADATA_TIMESTAMP_PATTERN_KEY]
189-
self.timezone = dateutil.tz.gettz(self.metadata[METADATA_TZ_ID_KEY])
195+
self.timezone = ZoneInfo(self.metadata[METADATA_TZ_ID_KEY])
190196
return self.pos
191197

192198
@abstractmethod

0 commit comments

Comments
 (0)