Skip to content

Commit 0a45fee

Browse files
Merge pull request #513 from selfisekai/utcnow
replace datetime.utcnow() with datetime.now()
2 parents 32978f1 + 3a26aad commit 0a45fee

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

b2sdk/_internal/b2http.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,20 @@ def post_request(self, method, url, headers, response):
145145
# Convert the server time to a datetime object
146146
try:
147147
with setlocale("C"):
148+
# "%Z" always creates naive datetimes, even though the timezone
149+
# is specified. https://github.com/python/cpython/issues/76678
150+
# Anyway, thankfully, HTTP/1.1 spec requires the string
151+
# to always say "GMT", and provide UTC time.
152+
# https://datatracker.ietf.org/doc/html/rfc2616#section-3.3.1
148153
server_time = datetime.datetime.strptime(
149-
server_date_str, '%a, %d %b %Y %H:%M:%S %Z'
150-
)
154+
server_date_str, '%a, %d %b %Y %H:%M:%S GMT'
155+
).replace(tzinfo=datetime.timezone.utc)
151156
except ValueError:
152157
logger.exception('server returned date in an inappropriate format')
153158
raise BadDateFormat(server_date_str)
154159

155160
# Get the local time
156-
local_time = datetime.datetime.utcnow()
161+
local_time = datetime.datetime.now(datetime.timezone.utc)
157162

158163
# Check the difference.
159164
max_allowed = 10 * 60 # ten minutes, in seconds
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed datetime.utcnow() deprecation warnings in Python 3.12.

test/unit/b2http/test_b2http.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -402,22 +402,22 @@ def test_bad_month(self):
402402
ClockSkewHook().post_request('POST', 'http://example.com', {}, response)
403403

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

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

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

0 commit comments

Comments
 (0)