Skip to content

Commit

Permalink
fix deprecated datetime timestamps calls (#16095)
Browse files Browse the repository at this point in the history
* fix deprecated datetime timestamps calls

* jwt date

* test
  • Loading branch information
memsharded committed May 7, 2024
1 parent a199aae commit 4368d7e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
7 changes: 4 additions & 3 deletions conans/server/crypto/jwt/jwt_manager.py
@@ -1,10 +1,10 @@
from datetime import datetime
from datetime import datetime, timezone
from calendar import timegm

import jwt


class JWTManager(object):
class JWTManager:
"""
Handles the JWT token generation and encryption.
"""
Expand All @@ -21,7 +21,8 @@ def get_token_for(self, profile_fields=None):
profile_fields = profile_fields or {}

if self.expire_time:
profile_fields["exp"] = timegm((datetime.utcnow() + self.expire_time).timetuple())
profile_fields["exp"] = timegm((datetime.now(timezone.utc)
+ self.expire_time).timetuple())

return jwt.encode(profile_fields, self.secret, algorithm="HS256")

Expand Down
13 changes: 11 additions & 2 deletions conans/test/unittests/client/util/time_test.py
Expand Up @@ -3,15 +3,16 @@

from dateutil.tz import tzutc

from conans.util.dates import from_timestamp_to_iso8601, _from_iso8601_to_datetime
from conans.util.dates import from_timestamp_to_iso8601, _from_iso8601_to_datetime, \
from_iso8601_to_timestamp


class TimeTest(unittest.TestCase):

def test_time_conversions(self):
timestamp = 1547138099
iso = from_timestamp_to_iso8601(timestamp)
self.assertEqual(iso, "2019-01-10T16:34:59Z")
self.assertEqual(iso, "2019-01-10T16:34:59+00:00")

dt = _from_iso8601_to_datetime(iso)
expected = datetime.datetime(year=2019, month=1, day=10, hour=16, minute=34, second=59,
Expand All @@ -29,3 +30,11 @@ def test_time_conversions(self):
expected = datetime.datetime(year=2019, month=5, day=14, hour=15, minute=52, second=28,
microsecond=383000, tzinfo=tzutc())
self.assertEqual(dt, expected)

artifactory_ret = "2024-01-23T08:39:53.776+0000"
ts = from_iso8601_to_timestamp(artifactory_ret)
assert ts == 1705999193.776

artifactory_ret = "2024-01-23T08:39:53.776+00:00"
ts = from_iso8601_to_timestamp(artifactory_ret)
assert ts == 1705999193.776
4 changes: 2 additions & 2 deletions conans/util/dates.py
Expand Up @@ -9,7 +9,7 @@

def from_timestamp_to_iso8601(timestamp):
# Used exclusively by conan_server to return the date in iso format (same as artifactory)
return "%sZ" % datetime.datetime.utcfromtimestamp(timestamp).isoformat()
return "%s" % datetime.datetime.fromtimestamp(timestamp, datetime.timezone.utc).isoformat()


def _from_iso8601_to_datetime(iso_str):
Expand All @@ -34,7 +34,7 @@ def revision_timestamp_now():
def timestamp_to_str(timestamp):
# used by ref.repr_humantime() to print human readable time
assert timestamp is not None
return datetime.datetime.utcfromtimestamp(int(timestamp)).strftime('%Y-%m-%d %H:%M:%S UTC')
return datetime.datetime.fromtimestamp(int(timestamp), datetime.timezone.utc).strftime('%Y-%m-%d %H:%M:%S UTC')


def timelimit(expression):
Expand Down

0 comments on commit 4368d7e

Please sign in to comment.