Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace to-be-deprecated utcnow() datetime calls with aware now() calls #475

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/webob/cookies.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import binascii
from collections.abc import MutableMapping
from datetime import date, datetime, timedelta
from datetime import date, datetime, timedelta, timezone
import hashlib
import hmac
import json
Expand Down Expand Up @@ -239,10 +239,13 @@ def serialize_cookie_date(v):
v = timedelta(seconds=v)

if isinstance(v, timedelta):
v = datetime.utcnow() + v
v = datetime.now(tz=timezone.utc) + v

if isinstance(v, (datetime, date)):
if isinstance(v, datetime):
v = v.astimezone(timezone.utc).timetuple()
elif isinstance(v, date):
v = v.timetuple()

r = time.strftime("%%s, %d-%%s-%Y %H:%M:%S GMT", v)

return bytes_(r % (weekdays[v[6]], months[v[1]]), "ascii")
Expand Down
10 changes: 6 additions & 4 deletions src/webob/response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from base64 import b64encode
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from hashlib import md5
import re
import struct
Expand Down Expand Up @@ -1246,6 +1246,8 @@ def _cache_expires(self, seconds=0, **kw):
seconds = timedelta_to_seconds(seconds)
cache_control = self.cache_control

utcnow = datetime.now(tz=timezone.utc).replace(tzinfo=None)

if seconds is None:
pass
elif not seconds:
Expand All @@ -1259,15 +1261,15 @@ def _cache_expires(self, seconds=0, **kw):
cache_control.max_age = 0
cache_control.post_check = 0
cache_control.pre_check = 0
self.expires = datetime.utcnow()
self.expires = utcnow

if "last-modified" not in self.headers:
self.last_modified = datetime.utcnow()
self.last_modified = utcnow
self.pragma = "no-cache"
else:
cache_control.properties.clear()
cache_control.max_age = seconds
self.expires = datetime.utcnow() + timedelta(seconds=seconds)
self.expires = utcnow + timedelta(seconds=seconds)
self.pragma = None

for name, value in kw.items():
Expand Down
Loading