Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions commit_msg.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Replace deprecated datetime.utcfromtimestamp and utcnow

datetime.utcfromtimestamp() and datetime.utcnow() are deprecated
since Python 3.12 and scheduled for removal. This replaces:

- utcfromtimestamp() in prettyprint.py with
datetime.fromtimestamp(ts, tz=timezone.utc), stripping the
+00:00 offset so the output stays as before (ISO format + Z)

- utcnow() in a test expression with datetime.now(), since the
test only checks that the subtraction produces a timedelta

Fixes #517
10 changes: 4 additions & 6 deletions eliot/prettyprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import pprint
import argparse
from datetime import datetime
from datetime import datetime, timezone
from sys import stdin, stdout
from collections import OrderedDict
from json import dumps
Expand Down Expand Up @@ -44,16 +44,14 @@

def _render_timestamp(message: dict, local_timezone: bool) -> str:
"""Convert a message's timestamp to a string."""
# If we were returning or storing the datetime we'd want to use an
# explicit timezone instead of a naive datetime, but since we're
# just using it for formatting we needn't bother.
if local_timezone:
dt = datetime.fromtimestamp(message[TIMESTAMP_FIELD])
else:
dt = datetime.utcfromtimestamp(message[TIMESTAMP_FIELD])
dt = datetime.fromtimestamp(message[TIMESTAMP_FIELD], tz=timezone.utc)
result = dt.isoformat(sep="T")
if not local_timezone:
result += "Z"
# Strip the +00:00 suffix and use Z instead
result = result.replace("+00:00", "") + "Z"
return result


Expand Down
2 changes: 1 addition & 1 deletion eliot/tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def test_otherLocals(self):
built-ins.
"""
result = self.evaluateExpression(
"isinstance(datetime.utcnow() - datetime.utcnow(), timedelta)", {}
"isinstance(datetime.now() - datetime.now(), timedelta)", {}
)
self.assertEqual(result, True)

Expand Down