Skip to content

Commit dfe9607

Browse files
authored
Fix "AttributeError" while using "ZoneInfo" (due to invalid "tzinfo.utcoffset()") (#1260)
1 parent 6dc3d3d commit dfe9607

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

loguru/_datetime.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ def _default_datetime_formatter(dt):
3636
)
3737

3838

39-
def _format_timezone(tzinfo, *, sep):
40-
offset = tzinfo.utcoffset(None).total_seconds()
39+
def _format_timezone(dt, *, sep):
40+
tzinfo = dt.tzinfo or timezone.utc
41+
offset = tzinfo.utcoffset(dt).total_seconds()
4142
sign = "+" if offset >= 0 else "-"
4243
(h, m), s = divmod(abs(offset // 60), 60), abs(offset) % 60
4344
z = "%s%02d%s%02d" % (sign, h, sep, m)
@@ -103,8 +104,8 @@ def _compile_format(spec):
103104
"SSSSS": ("%05d", lambda t, dt: dt.microsecond // 10),
104105
"SSSSSS": ("%06d", lambda t, dt: dt.microsecond),
105106
"A": ("%s", lambda t, dt: "AM" if t.tm_hour < 12 else "PM"),
106-
"Z": ("%s", lambda t, dt: _format_timezone(dt.tzinfo or timezone.utc, sep=":")),
107-
"ZZ": ("%s", lambda t, dt: _format_timezone(dt.tzinfo or timezone.utc, sep="")),
107+
"Z": ("%s", lambda t, dt: _format_timezone(dt, sep=":")),
108+
"ZZ": ("%s", lambda t, dt: _format_timezone(dt, sep="")),
108109
"zz": ("%s", lambda t, dt: (dt.tzinfo or timezone.utc).tzname(dt) or ""),
109110
"X": ("%d", lambda t, dt: dt.timestamp()),
110111
"x": ("%d", lambda t, dt: int(dt.timestamp() * 1000000 + dt.microsecond)),
@@ -135,7 +136,6 @@ def _compile_format(spec):
135136

136137

137138
class datetime(datetime_): # noqa: N801
138-
139139
def __format__(self, fmt):
140140
return _compile_format(fmt)(self)
141141

0 commit comments

Comments
 (0)