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

Remove use of utcnow #290

Merged
merged 4 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
7 changes: 5 additions & 2 deletions cachecontrol/caches/redis_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

from __future__ import division

from datetime import datetime
from datetime import datetime, timezone
from typing import TYPE_CHECKING, Optional, Union

from cachecontrol.cache import BaseCache
Expand All @@ -26,7 +26,10 @@ def set(
if not expires:
self.conn.set(key, value)
elif isinstance(expires, datetime):
delta = expires - datetime.utcnow()
now_utc = datetime.now(timezone.utc)
if expires.tzinfo is None:
now_utc = now_utc.replace(tzinfo=None)
delta = expires - now_utc
self.conn.setex(key, int(delta.total_seconds()), value)
else:
self.conn.setex(key, expires, value)
Expand Down
6 changes: 3 additions & 3 deletions cachecontrol/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import calendar
import time
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone
from email.utils import formatdate, parsedate, parsedate_tz
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional

Expand All @@ -15,7 +15,7 @@


def expire_after(delta: timedelta, date: Optional[datetime] = None) -> datetime:
date = date or datetime.utcnow()
date = date or datetime.now(timezone.utc)
return date + delta


Expand Down Expand Up @@ -67,7 +67,7 @@ def update_headers(self, response: "HTTPResponse") -> Dict[str, str]:

if "expires" not in response.headers:
date = parsedate(response.headers["date"])
expires = expire_after(timedelta(days=1), date=datetime(*date[:6]))
expires = expire_after(timedelta(days=1), date=datetime(*date[:6], tzinfo=timezone.utc))
frostming marked this conversation as resolved.
Show resolved Hide resolved
headers["expires"] = datetime_to_header(expires)
headers["cache-control"] = "public"
return headers
Expand Down
9 changes: 6 additions & 3 deletions tests/test_expires_heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

import calendar
import time
from datetime import datetime
from datetime import datetime, timezone
from email.utils import formatdate, parsedate
from pprint import pprint
from unittest.mock import Mock
Expand Down Expand Up @@ -157,7 +157,9 @@ def test_last_modified_is_used(self):
resp = DummyResponse(200, {"Date": self.now, "Last-Modified": self.week_ago})
modified = self.heuristic.update_headers(resp)
assert ["expires"] == list(modified.keys())
assert datetime(*parsedate(modified["expires"])[:6]) > datetime.now()

expected = datetime(*parsedate(modified["expires"])[:6], tzinfo=timezone.utc)
assert expected > datetime.now(timezone.utc)

def test_last_modified_is_not_used_when_cache_control_present(self):
resp = DummyResponse(
Expand Down Expand Up @@ -185,7 +187,8 @@ def test_last_modified_is_used_when_cache_control_public(self):
)
modified = self.heuristic.update_headers(resp)
assert ["expires"] == list(modified.keys())
assert datetime(*parsedate(modified["expires"])[:6]) > datetime.now()
expected = datetime(*parsedate(modified["expires"])[:6], tzinfo=timezone.utc)
assert expected > datetime.now(timezone.utc)

def test_warning_not_added_when_response_more_recent_than_24_hours(self):
resp = DummyResponse(200, {"Date": self.now, "Last-Modified": self.week_ago})
Expand Down
7 changes: 6 additions & 1 deletion tests/test_storage_redis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: Apache-2.0

from datetime import datetime
from datetime import datetime, timezone
from unittest.mock import Mock

from cachecontrol.caches import RedisCache
Expand All @@ -17,6 +17,11 @@ def test_set_expiration_datetime(self):
self.cache.set("foo", "bar", expires=datetime(2014, 2, 2))
assert self.conn.setex.called

def test_set_expiration_datetime_aware(self):
self.cache.set("foo", "bar",
expires=datetime(2014, 2, 2, tzinfo=timezone.utc))
assert self.conn.setex.called

def test_set_expiration_int(self):
self.cache.set("foo", "bar", expires=600)
assert self.conn.setex.called