Skip to content

Commit

Permalink
Replace deprecated datetime.utcfromtimestamp, datetime.utcnow
Browse files Browse the repository at this point in the history
https://docs.python.org/3/whatsnew/3.12.html#deprecated
“`datetime.datetime`’s `utcnow()` and `utcfromtimestamp()` are
deprecated and will be removed in a future version. Instead, use
timezone-aware objects to represent datetimes in UTC: respectively,
call `now()` and `fromtimestamp()` with the `tz` parameter set to
`datetime.UTC`.”

Signed-off-by: Anders Kaseorg <[email protected]>
  • Loading branch information
andersk committed Oct 22, 2024
1 parent 0320fd4 commit 8d13647
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 13 deletions.
21 changes: 10 additions & 11 deletions src/onelogin/saml2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
import base64
import warnings
from copy import deepcopy
import calendar
from datetime import datetime
from datetime import datetime, timezone
from hashlib import sha1, sha256, sha384, sha512
from isodate import parse_duration as duration_parser
import re
Expand Down Expand Up @@ -389,7 +388,7 @@ def parse_time_to_SAML(time):
:return: SAML2 timestamp.
:rtype: string
"""
data = datetime.utcfromtimestamp(float(time))
data = datetime.fromtimestamp(float(time), timezone.utc)
return data.strftime(OneLogin_Saml2_Utils.TIME_FORMAT)

@staticmethod
Expand All @@ -405,25 +404,25 @@ def parse_SAML_to_time(timestr):
:rtype: int
"""
try:
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT)
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT).replace(tzinfo=timezone.utc)
except ValueError:
try:
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT_2)
data = datetime.strptime(timestr, OneLogin_Saml2_Utils.TIME_FORMAT_2).replace(tzinfo=timezone.utc)
except ValueError:
elem = OneLogin_Saml2_Utils.TIME_FORMAT_WITH_FRAGMENT.match(timestr)
if not elem:
raise Exception("time data %s does not match format %s" % (timestr, r"yyyy-mm-ddThh:mm:ss(\.s+)?Z"))
data = datetime.strptime(elem.groups()[0] + "Z", OneLogin_Saml2_Utils.TIME_FORMAT)
data = datetime.strptime(elem.groups()[0] + "Z", OneLogin_Saml2_Utils.TIME_FORMAT).replace(tzinfo=timezone.utc)

return calendar.timegm(data.utctimetuple())
return int(data.timestamp())

@staticmethod
def now():
"""
:return: unix timestamp of actual time.
:rtype: int
"""
return calendar.timegm(datetime.utcnow().utctimetuple())
return int(datetime.now(timezone.utc).timestamp())

@staticmethod
def parse_duration(duration, timestamp=None):
Expand All @@ -445,10 +444,10 @@ def parse_duration(duration, timestamp=None):

timedelta = duration_parser(duration)
if timestamp is None:
data = datetime.utcnow() + timedelta
data = datetime.now(timezone.utc) + timedelta
else:
data = datetime.utcfromtimestamp(timestamp) + timedelta
return calendar.timegm(data.utctimetuple())
data = datetime.fromtimestamp(timestamp, timezone.utc) + timedelta
return int(data.timestamp())

@staticmethod
def get_expire_time(cache_duration=None, valid_until=None):
Expand Down
4 changes: 2 additions & 2 deletions tests/src/OneLogin/saml2_tests/metadata_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import json
from os.path import dirname, join, exists
from time import strftime
from datetime import datetime
from datetime import datetime, timezone
import unittest

from onelogin.saml2 import compat
Expand Down Expand Up @@ -97,7 +97,7 @@ def testBuilder(self):
self.assertNotIn("cacheDuration", metadata5)
self.assertIn('validUntil="2014-10-01T11:04:29Z"', metadata5)

datetime_value = datetime.now()
datetime_value = datetime.now(timezone.utc)
metadata6 = OneLogin_Saml2_Metadata.builder(sp_data, security["authnRequestsSigned"], security["wantAssertionsSigned"], datetime_value, "P1Y", contacts, organization)
self.assertIsNotNone(metadata5)
self.assertIn("<md:SPSSODescriptor", metadata6)
Expand Down

0 comments on commit 8d13647

Please sign in to comment.