Skip to content

Commit

Permalink
Merge pull request #513 from selfisekai/utcnow
Browse files Browse the repository at this point in the history
replace datetime.utcnow() with datetime.now()
  • Loading branch information
mjurbanski-reef authored Oct 28, 2024
2 parents 32978f1 + 3a26aad commit 0a45fee
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
11 changes: 8 additions & 3 deletions b2sdk/_internal/b2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,20 @@ def post_request(self, method, url, headers, response):
# Convert the server time to a datetime object
try:
with setlocale("C"):
# "%Z" always creates naive datetimes, even though the timezone
# is specified. https://github.com/python/cpython/issues/76678
# Anyway, thankfully, HTTP/1.1 spec requires the string
# to always say "GMT", and provide UTC time.
# https://datatracker.ietf.org/doc/html/rfc2616#section-3.3.1
server_time = datetime.datetime.strptime(
server_date_str, '%a, %d %b %Y %H:%M:%S %Z'
)
server_date_str, '%a, %d %b %Y %H:%M:%S GMT'
).replace(tzinfo=datetime.timezone.utc)
except ValueError:
logger.exception('server returned date in an inappropriate format')
raise BadDateFormat(server_date_str)

# Get the local time
local_time = datetime.datetime.utcnow()
local_time = datetime.datetime.now(datetime.timezone.utc)

# Check the difference.
max_allowed = 10 * 60 # ten minutes, in seconds
Expand Down
1 change: 1 addition & 0 deletions changelog.d/+datetime.utcnow_deprecation.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed datetime.utcnow() deprecation warnings in Python 3.12.
6 changes: 3 additions & 3 deletions test/unit/b2http/test_b2http.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,22 +402,22 @@ def test_bad_month(self):
ClockSkewHook().post_request('POST', 'http://example.com', {}, response)

def test_no_skew(self):
now = datetime.datetime.utcnow()
now = datetime.datetime.now(datetime.timezone.utc)
now_str = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
response = MagicMock()
response.headers = {'Date': now_str}
ClockSkewHook().post_request('POST', 'http://example.com', {}, response)

def test_positive_skew(self):
now = datetime.datetime.utcnow() + datetime.timedelta(minutes=11)
now = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=11)
now_str = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
response = MagicMock()
response.headers = {'Date': now_str}
with self.assertRaises(ClockSkew):
ClockSkewHook().post_request('POST', 'http://example.com', {}, response)

def test_negative_skew(self):
now = datetime.datetime.utcnow() + datetime.timedelta(minutes=-11)
now = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(minutes=-11)
now_str = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
response = MagicMock()
response.headers = {'Date': now_str}
Expand Down

0 comments on commit 0a45fee

Please sign in to comment.