Skip to content

Commit 92e5dca

Browse files
committed
Replace deprecated datetime.utcnow() by datetime.now(timezone.utc)
The `utcnow` function has been deprecated starting from Python 3.12: https://docs.python.org/3/library/datetime.html#datetime.datetime.utcnow Thus replace `datetime.datetime.utcnow()` by `datetime.datetime.now(datetime.timezone.utc)`. Also replace `pytz` with standard Python. Fixes: IdentityPython#934
1 parent 0252ec9 commit 92e5dca

File tree

7 files changed

+9
-13
lines changed

7 files changed

+9
-13
lines changed

pyproject.toml

-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ importlib-resources = {python = "<3.9", version = "*"}
4444
paste = {optional = true, version = "*"}
4545
pyopenssl = "<24.3.0"
4646
python-dateutil = "*"
47-
pytz = "*"
4847
"repoze.who" = {optional = true, version = "*"}
4948
requests = "^2"
5049
xmlschema = "^2"
@@ -68,7 +67,6 @@ ipdb = "^0.13.9"
6867
mypy = "^1.0.0"
6968
types-pyopenssl = "^23.0.0.3"
7069
types-python-dateutil = "^2.8.19.6"
71-
types-pytz = "^2022.7.1.0"
7270
types-setuptools = "^67.2.0.1"
7371
types-six = "^1.16.21.4"
7472
types-requests = "^2.28.11.12"

src/saml2/cert.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from OpenSSL import crypto
99
import dateutil.parser
10-
import pytz
1110

1211
import saml2.cryptography.pki
1312

@@ -278,7 +277,7 @@ def verify_chain(self, cert_chain_str_list, cert_str):
278277

279278
def certificate_not_valid_yet(self, cert):
280279
starts_to_be_valid = dateutil.parser.parse(cert.get_notBefore())
281-
now = pytz.UTC.localize(datetime.datetime.utcnow())
280+
now = datetime.datetime.now(datetime.timezone.utc)
282281
if starts_to_be_valid < now:
283282
return False
284283
return True

src/saml2/mongo_store.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ def store(self, value, **kwargs):
205205
doc.update(kwargs)
206206
# Add timestamp to all documents to allow external garbage collecting
207207
if "created_at" not in doc:
208-
doc["created_at"] = datetime.datetime.utcnow()
208+
doc["created_at"] = datetime.datetime.now(datetime.timezone.utc)
209209
_ = self.db.insert_one(doc)
210210

211211
def get(self, value=None, **kwargs):

src/saml2/sigver.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
from urllib import parse
3030

3131
from OpenSSL import crypto
32-
import pytz
3332

3433
from saml2 import ExtensionElement
3534
from saml2 import SamlBase
@@ -387,7 +386,7 @@ def active_cert(key):
387386
except AttributeError:
388387
return False
389388

390-
now = pytz.UTC.localize(datetime.datetime.utcnow())
389+
now = datetime.datetime.now(datetime.timezone.utc)
391390
valid_from = dateutil.parser.parse(cert.get_notBefore())
392391
valid_to = dateutil.parser.parse(cert.get_notAfter())
393392
active = not cert.has_expired() and valid_from <= now < valid_to

src/saml2/time_util.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"""
77

88
import calendar
9-
from datetime import datetime
9+
from datetime import datetime, timezone
1010
from datetime import timedelta
1111
import re
1212
import sys
@@ -175,7 +175,7 @@ def time_in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0
175175
:return: UTC time
176176
"""
177177
delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
178-
return datetime.utcnow() + delta
178+
return datetime.now(timezone.utc) + delta
179179

180180

181181
def time_a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0):
@@ -185,7 +185,7 @@ def time_a_while_ago(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=
185185
minutes[, hours[, weeks]]]]]]])
186186
"""
187187
delta = timedelta(days, seconds, microseconds, milliseconds, minutes, hours, weeks)
188-
return datetime.utcnow() - delta
188+
return datetime.now(timezone.utc) - delta
189189

190190

191191
def in_a_while(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0, format=TIME_FORMAT):

tests/test_41_response.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_issuer_none(self):
127127
@patch("saml2.time_util.datetime")
128128
def test_false_sign(self, mock_datetime, caplog):
129129
caplog.set_level(logging.ERROR)
130-
mock_datetime.utcnow = Mock(return_value=datetime.datetime(2016, 9, 4, 9, 59, 39))
130+
mock_datetime.now = Mock(return_value=datetime.datetime(2016, 9, 4, 9, 59, 39, tzinfo=datetime.timezone.utc))
131131
with open(FALSE_ASSERT_SIGNED) as fp:
132132
xml_response = fp.read()
133133

tests/test_44_authnresp.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env python
22
from contextlib import closing
3-
from datetime import datetime
3+
from datetime import datetime, timezone
44

55
from dateutil import parser
66
from pathutils import dotname
@@ -131,7 +131,7 @@ def test_verify_w_authn(self):
131131
assert len(authn_info) == 1
132132
assert authn_info[0][0] == INTERNETPROTOCOLPASSWORD
133133
assert authn_info[0][1] == ["http://www.example.com/login"]
134-
now = datetime.utcnow()
134+
now = datetime.now(timezone.utc)
135135
dt = parser.parse(authn_info[0][2])
136136
assert now.year == dt.year and now.month == dt.month and now.day == dt.day
137137
session_info = self.ar.session_info()

0 commit comments

Comments
 (0)