Skip to content
Closed
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
2 changes: 1 addition & 1 deletion cachecontrol/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def expire_after(delta: timedelta, date: datetime | None = None) -> datetime:


def datetime_to_header(dt: datetime) -> str:
return formatdate(calendar.timegm(dt.timetuple()))
return formatdate(calendar.timegm(dt.utctimetuple()))


class BaseHeuristic:
Expand Down
27 changes: 26 additions & 1 deletion 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, timezone
from datetime import datetime, timedelta, timezone
from email.utils import formatdate, parsedate
from pprint import pprint
from unittest.mock import Mock
Expand All @@ -19,10 +19,35 @@
LastModified,
OneDayCache,
)
from cachecontrol.heuristics import (
datetime_to_header as heuristic_datetime_to_header,
)

from .utils import DummyResponse


class TestDatetimeToHeader:
def test_positive_timezone_offset(self):
value = datetime(
2026, 10, 20, 1, tzinfo=timezone(timedelta(hours=5, minutes=30))
)
assert heuristic_datetime_to_header(value) == "Mon, 19 Oct 2026 19:30:00 -0000"

def test_negative_timezone_offset(self):
value = datetime(
2026, 10, 20, 23, tzinfo=timezone(timedelta(hours=-3, minutes=-30))
)
assert heuristic_datetime_to_header(value) == "Wed, 21 Oct 2026 02:30:00 -0000"

def test_utc(self):
value = datetime(2026, 10, 20, 12, tzinfo=timezone.utc)
assert heuristic_datetime_to_header(value) == "Tue, 20 Oct 2026 12:00:00 -0000"

def test_naive_remains_utc(self):
value = datetime(2026, 10, 20, 12)
assert heuristic_datetime_to_header(value) == "Tue, 20 Oct 2026 12:00:00 -0000"


class TestHeuristicWithoutWarning:
def setup_method(self):
class NoopHeuristic(BaseHeuristic):
Expand Down