Skip to content

Commit

Permalink
Fix missing seconds when formatting timezone offset (#961)
Browse files Browse the repository at this point in the history
  • Loading branch information
Delgan committed Aug 31, 2023
1 parent 2465fca commit de646c6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
- Fix file possibly rotating too early or too late when re-starting an application around midnight (`#894 <https://github.com/Delgan/loguru/issues/894>`_).
- Fix inverted ``"<hide>"`` and ``"<strike>"`` color tags (`#943 <https://github.com/Delgan/loguru/pull/943>`_, thanks `@tunaflsh <https://github.com/tunaflsh>`_).
- Fix possible errors raised by logging non-picklable ``Exception`` instances while using ``enqueue=True`` (`#342 <https://github.com/Delgan/loguru/issues/342>`_, thanks `@ncoudene <https://github.com/ncoudene>`_).
- Fix missing seconds and microseconds when formatting timezone offset that requires such accuracy (`#961 <https://github.com/Delgan/loguru/issues/961>`_).
- Raise ``ValueError`` if an attempt to use nanosecond precision for time formatting is detected (`#855 <https://github.com/Delgan/loguru/issues/855>`_).


Expand Down
6 changes: 3 additions & 3 deletions loguru/_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __format__(self, spec):
tzinfo = dt.tzinfo or timezone(timedelta(seconds=0))
offset = tzinfo.utcoffset(dt).total_seconds()
sign = ("-", "+")[offset >= 0]
h, m = divmod(abs(offset // 60), 60)
(h, m), s = divmod(abs(offset // 60), 60), abs(offset) % 60

rep = {
"YYYY": "%04d" % year,
Expand Down Expand Up @@ -69,8 +69,8 @@ def __format__(self, spec):
"SSSSS": "%05d" % (microsecond // 10),
"SSSSSS": "%06d" % microsecond,
"A": ("AM", "PM")[hour // 12],
"Z": "%s%02d:%02d" % (sign, h, m),
"ZZ": "%s%02d%02d" % (sign, h, m),
"Z": "%s%02d:%02d%s" % (sign, h, m, (":%09.06f" % s)[: 11 if s % 1 else 3] * (s > 0)),
"ZZ": "%s%02d%02d%s" % (sign, h, m, ("%09.06f" % s)[: 10 if s % 1 else 2] * (s > 0)),
"zz": tzinfo.tzname(dt) or "",
"X": "%d" % timestamp,
"x": "%d" % (int(timestamp) * 1000000 + microsecond),
Expand Down
20 changes: 20 additions & 0 deletions tests/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,26 @@ def test_formatting(writer, freeze_time, time_format, date, timezone, expected):
assert result == expected + "\n"


@pytest.mark.parametrize(
"time_format, offset, expected",
[
("%Y-%m-%d %H-%M-%S %f %Z %z", 7230.099, "2018-06-09 01-02-03 000000 ABC +020030.099000"),
("YYYY-MM-DD HH-mm-ss zz Z ZZ", 6543, "2018-06-09 01-02-03 ABC +01:49:03 +014903"),
("HH-mm-ss zz Z ZZ", -12345.06702, "01-02-03 ABC -03:26:45.067020 -032645.067020"),
],
)
@pytest.mark.skipif(sys.version_info < (3, 7), reason="Offset must be a whole number of minutes")
def test_formatting_timezone_offset_down_to_the_second(
writer, freeze_time, time_format, offset, expected
):
date = datetime.datetime(2018, 6, 9, 1, 2, 3)
with freeze_time(date, ("ABC", offset)):
logger.add(writer, format="{time:%s}" % time_format)
logger.debug("Test")
result = writer.read()
assert result == expected + "\n"


def test_locale_formatting(writer, freeze_time):
dt = datetime.datetime(2011, 1, 1, 22, 22, 22, 0)
with freeze_time(dt):
Expand Down

0 comments on commit de646c6

Please sign in to comment.