Skip to content

Commit

Permalink
[8.0.x] fix: avoid rounding microsecond to 1_000_000 (#11863)
Browse files Browse the repository at this point in the history
Co-authored-by: Dương Quốc Khánh <[email protected]>
  • Loading branch information
github-actions[bot] and dqkqd authored Jan 27, 2024
1 parent 3b41c65 commit 6085900
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog/11861.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Avoid microsecond exceeds ``1_000_000`` when using ``log-date-format`` with ``%f`` specifier, which might cause the test suite to crash.
3 changes: 2 additions & 1 deletion src/_pytest/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ def formatTime(self, record: LogRecord, datefmt: Optional[str] = None) -> str:
tz = timezone(timedelta(seconds=ct.tm_gmtoff), ct.tm_zone)
# Construct `datetime.datetime` object from `struct_time`
# and msecs information from `record`
dt = datetime(*ct[0:6], microsecond=round(record.msecs * 1000), tzinfo=tz)
# Using int() instead of round() to avoid it exceeding 1_000_000 and causing a ValueError (#11861).
dt = datetime(*ct[0:6], microsecond=int(record.msecs * 1000), tzinfo=tz)
return dt.strftime(datefmt)
# Use `logging.Formatter` for non-microsecond formats
return super().formatTime(record, datefmt)
Expand Down

0 comments on commit 6085900

Please sign in to comment.