Skip to content

Commit 6085900

Browse files
[8.0.x] fix: avoid rounding microsecond to 1_000_000 (#11863)
Co-authored-by: Dương Quốc Khánh <[email protected]>
1 parent 3b41c65 commit 6085900

File tree

2 files changed

+3
-1
lines changed

2 files changed

+3
-1
lines changed

Diff for: changelog/11861.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Avoid microsecond exceeds ``1_000_000`` when using ``log-date-format`` with ``%f`` specifier, which might cause the test suite to crash.

Diff for: src/_pytest/logging.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ def formatTime(self, record: LogRecord, datefmt: Optional[str] = None) -> str:
7171
tz = timezone(timedelta(seconds=ct.tm_gmtoff), ct.tm_zone)
7272
# Construct `datetime.datetime` object from `struct_time`
7373
# and msecs information from `record`
74-
dt = datetime(*ct[0:6], microsecond=round(record.msecs * 1000), tzinfo=tz)
74+
# Using int() instead of round() to avoid it exceeding 1_000_000 and causing a ValueError (#11861).
75+
dt = datetime(*ct[0:6], microsecond=int(record.msecs * 1000), tzinfo=tz)
7576
return dt.strftime(datefmt)
7677
# Use `logging.Formatter` for non-microsecond formats
7778
return super().formatTime(record, datefmt)

0 commit comments

Comments
 (0)