Skip to content

Commit

Permalink
Fix deprecation warning for datetime.datetime.utcnow
Browse files Browse the repository at this point in the history
This fixes IdentityPython#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).
  • Loading branch information
kfrydel committed Nov 3, 2023
1 parent 74dc24d commit 60ad647
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/saml2/time_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import calendar
from datetime import datetime
from datetime import timedelta
from datetime import UTC
import re
import sys
import time
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion tests/test_41_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 60ad647

Please sign in to comment.