summaryrefslogtreecommitdiffstats
path: root/Lib/logging
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2024-06-24 06:50:39 (GMT)
committerGitHub <noreply@github.com>2024-06-24 06:50:39 (GMT)
commit1500a23f33f5a6d052ff1ef6383d9839928b8ff1 (patch)
treee5339de97e05b9e425aac47af6c5fd388c9ca2cc /Lib/logging
parent02df6795743ee4ee26a07986edbb5e22ae9fec8b (diff)
downloadcpython-1500a23f33f5a6d052ff1ef6383d9839928b8ff1.zip
cpython-1500a23f33f5a6d052ff1ef6383d9839928b8ff1.tar.gz
cpython-1500a23f33f5a6d052ff1ef6383d9839928b8ff1.tar.bz2
gh-120683: Fix an error in logging.LogRecord timestamp (GH-120709)
The integer part of the timestamp can be rounded up, while the millisecond calculation truncates, causing the log timestamp to be wrong by up to 999 ms (affected roughly 1 in 8 million timestamps).
Diffstat (limited to 'Lib/logging')
-rw-r--r--Lib/logging/__init__.py5
1 files changed, 4 insertions, 1 deletions
diff --git a/Lib/logging/__init__.py b/Lib/logging/__init__.py
index 174b37c..3f41442 100644
--- a/Lib/logging/__init__.py
+++ b/Lib/logging/__init__.py
@@ -340,11 +340,14 @@ class LogRecord(object):
self.lineno = lineno
self.funcName = func
self.created = ct / 1e9 # ns to float seconds
-
# Get the number of whole milliseconds (0-999) in the fractional part of seconds.
# Eg: 1_677_903_920_999_998_503 ns --> 999_998_503 ns--> 999 ms
# Convert to float by adding 0.0 for historical reasons. See gh-89047
self.msecs = (ct % 1_000_000_000) // 1_000_000 + 0.0
+ if self.msecs == 999.0 and int(self.created) != ct // 1_000_000_000:
+ # ns -> sec conversion can round up, e.g:
+ # 1_677_903_920_999_999_900 ns --> 1_677_903_921.0 sec
+ self.msecs = 0.0
self.relativeCreated = (ct - _startTime) / 1e6
if logThreads: