From aadd12cb08bede15946bc790f6005bbe120c17bc Mon Sep 17 00:00:00 2001 From: LuK1337 Date: Sat, 16 Sep 2023 09:36:49 +0200 Subject: [PATCH] Use non-deprecated API for obtaining UTC time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit DeprecationWarning: datetime.datetime.utcnow() is deprecated and scheduled for removal in a future version. Use timezone-aware objects to represent datetimes in UTC: datetime.datetime.now(datetime.UTC). Change-Id: Ia2c46fb87c544d98cc2dd68a829f67d4770b479c Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/386615 Tested-by: Łukasz Patron Reviewed-by: Mike Frysinger Reviewed-by: Łukasz Patron Commit-Queue: Mike Frysinger --- git_config.py | 4 ++-- git_trace2_event_log_base.py | 6 +++--- repo | 4 ++-- tests/test_git_superproject.py | 4 +++- tests/test_git_trace2_event_log.py | 4 +++- 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/git_config.py b/git_config.py index 8c2eb6e9..c19f7662 100644 --- a/git_config.py +++ b/git_config.py @@ -795,8 +795,8 @@ class SyncAnalysisState: to be logged. """ self._config = config - now = datetime.datetime.utcnow() - self._Set("main.synctime", now.isoformat(timespec="microseconds") + "Z") + now = datetime.datetime.now(datetime.timezone.utc) + self._Set("main.synctime", now.isoformat(timespec="microseconds")) self._Set("main.version", "1") self._Set("sys.argv", sys.argv) for key, value in superproject_logging_data.items(): diff --git a/git_trace2_event_log_base.py b/git_trace2_event_log_base.py index a111668a..d986b240 100644 --- a/git_trace2_event_log_base.py +++ b/git_trace2_event_log_base.py @@ -71,7 +71,7 @@ class BaseEventLog(object): if env is None: env = os.environ - self.start = datetime.datetime.utcnow() + self.start = datetime.datetime.now(datetime.timezone.utc) # Save both our sid component and the complete sid. # We use our sid component (self._sid) as the unique filename prefix and @@ -126,7 +126,7 @@ class BaseEventLog(object): "event": event_name, "sid": self._full_sid, "thread": threading.current_thread().name, - "time": datetime.datetime.utcnow().isoformat() + "Z", + "time": datetime.datetime.now(datetime.timezone.utc).isoformat(), } def StartEvent(self): @@ -147,7 +147,7 @@ class BaseEventLog(object): if result is None: result = 0 exit_event["code"] = result - time_delta = datetime.datetime.utcnow() - self.start + time_delta = datetime.datetime.now(datetime.timezone.utc) - self.start exit_event["t_abs"] = time_delta.total_seconds() self._log.append(exit_event) diff --git a/repo b/repo index d2a4df2e..7f24ff1e 100755 --- a/repo +++ b/repo @@ -173,7 +173,7 @@ if not REPO_REV: BUG_URL = "https://issues.gerritcodereview.com/issues/new?component=1370071" # increment this whenever we make important changes to this script -VERSION = (2, 36) +VERSION = (2, 37) # increment this if the MAINTAINER_KEYS block is modified KEYRING_VERSION = (2, 3) @@ -838,7 +838,7 @@ def SetGitTrace2ParentSid(env=None): KEY = "GIT_TRACE2_PARENT_SID" - now = datetime.datetime.utcnow() + now = datetime.datetime.now(datetime.timezone.utc) value = "repo-%s-P%08x" % (now.strftime("%Y%m%dT%H%M%SZ"), os.getpid()) # If it's already set, then append ourselves. diff --git a/tests/test_git_superproject.py b/tests/test_git_superproject.py index dc46ec65..600b069f 100644 --- a/tests/test_git_superproject.py +++ b/tests/test_git_superproject.py @@ -108,7 +108,9 @@ class SuperprojectTestCase(unittest.TestCase): self.assertRegex(log_entry["sid"], self.FULL_SID_REGEX) else: self.assertRegex(log_entry["sid"], self.SELF_SID_REGEX) - self.assertRegex(log_entry["time"], r"^\d+-\d+-\d+T\d+:\d+:\d+\.\d+Z$") + self.assertRegex( + log_entry["time"], r"^\d+-\d+-\d+T\d+:\d+:\d+\.\d+\+00:00$" + ) def readLog(self, log_path): """Helper function to read log data into a list.""" diff --git a/tests/test_git_trace2_event_log.py b/tests/test_git_trace2_event_log.py index 9d4f24c3..d8e963dd 100644 --- a/tests/test_git_trace2_event_log.py +++ b/tests/test_git_trace2_event_log.py @@ -90,7 +90,9 @@ class EventLogTestCase(unittest.TestCase): self.assertRegex(log_entry["sid"], self.FULL_SID_REGEX) else: self.assertRegex(log_entry["sid"], self.SELF_SID_REGEX) - self.assertRegex(log_entry["time"], r"^\d+-\d+-\d+T\d+:\d+:\d+\.\d+Z$") + self.assertRegex( + log_entry["time"], r"^\d+-\d+-\d+T\d+:\d+:\d+\.\d+\+00:00$" + ) def readLog(self, log_path): """Helper function to read log data into a list."""