Skip to content

Commit 272bc7b

Browse files
committed
fix: issue2551278 - datetime.datetime.utcnow deprecation.
Replace calls with equivalent that produces timezone aware dates rather than naive dates. Also some flake8 fixes for test/rest_common.py.
1 parent 5fcd56c commit 272bc7b

File tree

5 files changed

+123
-96
lines changed

5 files changed

+123
-96
lines changed

CHANGES.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ Fixed:
3434
and make *FieldStroage symbols available. Roundp uses its own
3535
cgitb.py and not the system cgitb.py. It looks like it's the
3636
precursor to the system cgitb.py. (John Rouillard)
37+
- issue2551278 - datetime.datetime.utcnow deprecation. Replace
38+
calls with equivalent that produces timezone aware dates rather than
39+
naive dates. (John Rouillard)
3740

3841
Features:
3942

roundup/anypy/datetime_.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# https://issues.roundup-tracker.org/issue2551278
2+
# datetime.utcnow deprecated
3+
try:
4+
from datetime import now, UTC
5+
6+
def utcnow():
7+
return now(UTC)
8+
except ImportError:
9+
import datetime
10+
11+
def utcnow():
12+
return datetime.datetime.utcnow()

roundup/date.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import datetime
2525
import re
2626

27+
from roundup.anypy.datetime_ import utcnow
28+
2729
try:
2830
import pytz
2931
except ImportError:
@@ -376,7 +378,7 @@ def __init__(self, spec='.', offset=0, add_granularity=False,
376378
def now(self):
377379
""" To be able to override for testing
378380
"""
379-
return datetime.datetime.utcnow()
381+
return utcnow()
380382

381383
def set(self, spec, offset=0, date_re=date_re,
382384
serialised_re=serialised_date_re, add_granularity=False):

roundup/rate_limit.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from datetime import timedelta, datetime
88

9+
from roundup.anypy.datetime_ import utcnow
10+
911

1012
class RateLimit: # pylint: disable=too-few-public-methods
1113
def __init__(self, count, period):
@@ -50,7 +52,7 @@ def update(self, key, limit, testonly=False):
5052
'''Determine if the item associated with the key should be
5153
rejected given the RateLimit limit.
5254
'''
53-
now = datetime.utcnow()
55+
now = utcnow()
5456
tat = max(self.get_tat(key), now)
5557
separation = (tat - now).total_seconds()
5658
max_interval = limit.period.total_seconds() - limit.inverse
@@ -88,7 +90,7 @@ def status(self, key, limit):
8890
)
8991

9092
# status of current limit as of now
91-
now = datetime.utcnow()
93+
now = utcnow()
9294

9395
current_count = int((limit.period - (tat - now)).total_seconds() /
9496
limit.inverse)

0 commit comments

Comments
 (0)