From 60ad647cd80fcae968c6ac7aedecfa92254f8c80 Mon Sep 17 00:00:00 2001 From: Kamil Frydel Date: Fri, 3 Nov 2023 17:19:10 +0100 Subject: [PATCH] Fix deprecation warning for datetime.datetime.utcnow This fixes #934. utcnow function, starting from Python 3.12 is deprecated: https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow Thus, this commit turns usages of utcnow() into now(UTC). --- src/saml2/time_util.py | 5 +++-- tests/test_41_response.py | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/saml2/time_util.py b/src/saml2/time_util.py index c3e5b1393..68705bae4 100644 --- a/src/saml2/time_util.py +++ b/src/saml2/time_util.py @@ -8,6 +8,7 @@ import calendar from datetime import datetime from datetime import timedelta +from datetime import UTC import re import sys import time @@ -175,7 +176,7 @@ def time_in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0 :return: UTC time """ delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks) - return datetime.utcnow() + delta + return datetime.now(UTC) + delta def time_a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0): @@ -185,7 +186,7 @@ def time_a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0, minutes= minutes[, hours[, weeks]]]]]]]) """ delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks) - return datetime.utcnow() - delta + return datetime.now(UTC) - delta def in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0, format=TIME_FORMAT): diff --git a/tests/test_41_response.py b/tests/test_41_response.py index 59f688885..b8d7e3953 100644 --- a/tests/test_41_response.py +++ b/tests/test_41_response.py @@ -127,7 +127,7 @@ def test_issuer_none(self): @patch("saml2.time_util.datetime") def test_false_sign(self, mock_datetime, caplog): caplog.set_level(logging.ERROR) - mock_datetime.utcnow = Mock(return_value=datetime.datetime(2016, 9, 4, 9, 59, 39)) + mock_datetime.now = Mock(return_value=datetime.datetime(2016, 9, 4, 9, 59, 39)) with open(FALSE_ASSERT_SIGNED) as fp: xml_response = fp.read()