From 291bdf800a6dd1cf67d62e62586c6a76acc2c3cc Mon Sep 17 00:00:00 2001 From: Mark Dickinson Date: Mon, 11 Sep 2023 18:04:31 +0100 Subject: [PATCH] Fix use of the deprecated datetime.utcnow method (#1758) This PR replaces uses of the deprecated (as of Python 3.12) `datetime.utcnow` method. Note that this isn't quite a like-for-like change: there's a slight difference in the output format. Given that the event tracer is barely used in practice, I doubt this change matters much. ``` >>> from datetime import datetime, timezone >>> datetime.now(timezone.utc).isoformat(" ") '2023-09-11 16:31:35.534330+00:00' >>> datetime.utcnow().isoformat(" ") '2023-09-11 16:31:56.798474' ``` (cherry picked from commit 36a95d54083f234874579fa425e1793f63a2f043) --- traits/util/event_tracer.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/traits/util/event_tracer.py b/traits/util/event_tracer.py index 0769f315f..acb226c70 100644 --- a/traits/util/event_tracer.py +++ b/traits/util/event_tracer.py @@ -15,7 +15,7 @@ import os import threading from contextlib import contextmanager -from datetime import datetime +from datetime import datetime, timezone from traits import trait_notifiers @@ -228,7 +228,7 @@ def pre_tracer(self, obj, name, old, new, handler): """ indent = self.indent - time = datetime.utcnow().isoformat(" ") + time = datetime.now(timezone.utc).isoformat(" ") container = self.container container.record( ChangeMessageRecord( @@ -255,7 +255,7 @@ def post_tracer(self, obj, name, old, new, handler, exception=None): """ Record a string representation of the trait change return """ - time = datetime.utcnow().isoformat(" ") + time = datetime.now(timezone.utc).isoformat(" ") self.indent -= 1 indent = self.indent if exception: