Skip to content

Commit

Permalink
fix: issue2551278 - datetime.datetime.utcnow deprecation.
Browse files Browse the repository at this point in the history
Replace calls with equivalent that produces timezone aware dates
rather than naive dates.

Also some flake8 fixes for test/rest_common.py.
  • Loading branch information
rouilj committed Jul 25, 2023
1 parent 5fcd56c commit 272bc7b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 96 deletions.
3 changes: 3 additions & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ Fixed:
and make *FieldStroage symbols available. Roundp uses its own
cgitb.py and not the system cgitb.py. It looks like it's the
precursor to the system cgitb.py. (John Rouillard)
- issue2551278 - datetime.datetime.utcnow deprecation. Replace
calls with equivalent that produces timezone aware dates rather than
naive dates. (John Rouillard)

Features:

Expand Down
12 changes: 12 additions & 0 deletions roundup/anypy/datetime_.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# https://issues.roundup-tracker.org/issue2551278
# datetime.utcnow deprecated
try:
from datetime import now, UTC

def utcnow():
return now(UTC)
except ImportError:
import datetime

def utcnow():
return datetime.datetime.utcnow()
4 changes: 3 additions & 1 deletion roundup/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import datetime
import re

from roundup.anypy.datetime_ import utcnow

try:
import pytz
except ImportError:
Expand Down Expand Up @@ -376,7 +378,7 @@ def __init__(self, spec='.', offset=0, add_granularity=False,
def now(self):
""" To be able to override for testing
"""
return datetime.datetime.utcnow()
return utcnow()

def set(self, spec, offset=0, date_re=date_re,
serialised_re=serialised_date_re, add_granularity=False):
Expand Down
6 changes: 4 additions & 2 deletions roundup/rate_limit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from datetime import timedelta, datetime

from roundup.anypy.datetime_ import utcnow


class RateLimit: # pylint: disable=too-few-public-methods
def __init__(self, count, period):
Expand Down Expand Up @@ -50,7 +52,7 @@ def update(self, key, limit, testonly=False):
'''Determine if the item associated with the key should be
rejected given the RateLimit limit.
'''
now = datetime.utcnow()
now = utcnow()
tat = max(self.get_tat(key), now)
separation = (tat - now).total_seconds()
max_interval = limit.period.total_seconds() - limit.inverse
Expand Down Expand Up @@ -88,7 +90,7 @@ def status(self, key, limit):
)

# status of current limit as of now
now = datetime.utcnow()
now = utcnow()

current_count = int((limit.period - (tat - now)).total_seconds() /
limit.inverse)
Expand Down
Loading

0 comments on commit 272bc7b

Please sign in to comment.