Skip to content

Commit 6949cba

Browse files
committed
simplify iso format to not use datetime
1 parent 9c8eda4 commit 6949cba

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Lib/_colorize.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class ANSIColors:
1717
BLUE = "\x1b[34m"
1818
CYAN = "\x1b[36m"
1919
GREEN = "\x1b[32m"
20+
GREY = "\x1b[90m"
2021
MAGENTA = "\x1b[35m"
2122
RED = "\x1b[31m"
2223
WHITE = "\x1b[37m" # more like LIGHT GRAY

Lib/test/test_traceback_timestamps.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def test_traceback_timestamps_flag_iso(self):
6565
result = script_helper.assert_python_ok("-X", "traceback_timestamps=iso", self.script_path)
6666
stderr = result.err.decode()
6767
self.assertIn("<@", stderr) # Timestamp should be present
68-
self.assertRegex(stderr, r"<@\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}") # ISO format
68+
# ISO format with Z suffix for UTC
69+
self.assertRegex(stderr, r"<@\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{6}Z>")
6970

7071
def test_traceback_timestamps_flag_value(self):
7172
"""Test that sys.flags.traceback_timestamps shows the right value"""
@@ -147,4 +148,4 @@ def test_traceback_timestamps_invalid_flag(self):
147148

148149

149150
if __name__ == "__main__":
150-
unittest.main()
151+
unittest.main()

Lib/traceback.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,13 @@ def _timestamp_formatter(ns):
192192
return f"<@{ns}ns>"
193193
case "iso":
194194
def _timestamp_formatter(ns):
195-
from datetime import datetime
196-
return f"<@{datetime.fromtimestamp(ns/1e9).isoformat()}>"
195+
# Less logic in a critical path than using datetime.
196+
from time import strftime, gmtime
197+
seconds = ns / 1e9
198+
# Use gmtime for UTC time
199+
timestr = strftime("%Y-%m-%dT%H:%M:%S", gmtime(seconds))
200+
fractional = f"{seconds - int(seconds):.6f}"[2:] # Get just the decimal part
201+
return f"<@{timestr}.{fractional}Z>" # Z suffix indicates UTC/Zulu time
197202
case _:
198203
raise ValueError(f"Invalid sys.flags.traceback_timestamp={_TIMESTAMP_FORMAT!r}")
199204

0 commit comments

Comments
 (0)