Bug: Cookie Expires labels a non-UTC aware datetime as GMT without converting it
Expected Behaviour
A Cookie whose expires is a timezone-aware datetime should send that instant in GMT, as RFC 6265 requires for Expires. datetime(2026, 1, 1, 12, tzinfo=timezone(timedelta(hours=2))) is 10:00 UTC, so the header should read Expires=Thu, 01 Jan 2026 10:00:00 GMT.
Current Behaviour
Cookie._format_date formats the datetime with strftime and a hard-coded GMT suffix, without converting it to UTC first. The local wall-clock time is labelled GMT, so the cookie expires earlier or later by the UTC offset: Expires=Thu, 01 Jan 2026 12:00:00 GMT.
Code snippet
from datetime import datetime, timedelta, timezone
from aws_lambda_powertools.shared.cookies import Cookie
cookie = Cookie("a", "b", expires=datetime(2026, 1, 1, 12, tzinfo=timezone(timedelta(hours=2))))
print(str(cookie)) # a=b; Secure; Expires=Thu, 01 Jan 2026 12:00:00 GMT
Possible Solution
Convert an aware datetime to UTC before formatting (expires.astimezone(timezone.utc)); naive datetimes keep today's behaviour. I have this fix with a regression test in tests/unit/test_cookie_class.py (fails on develop, passes with the change) and can open a PR once this is triaged.
Steps to Reproduce
Run the snippet above on develop (5785377).
Powertools for AWS Lambda (Python) version
latest (3.37.0, develop)
AWS Lambda function runtime
3.13
Packaging format used
PyPi
Debugging logs
N/A
Thanks @LemonCondor, confirmed. _format_date stamps GMT on whatever wall-clock time it gets, so an aware datetime outside UTC is off by its offset. Converting aware values with astimezone(timezone.utc) and leaving naive ones alone is the right fix. It's yours, open the PR with the test. This goes into 3.40.0.
One more thing in _format_date, in case it fits the same fix: %a and %b follow the process LC_TIME, so with a German locale the header reads Expires=Fr, 30 Okt 2026 07:28:00 GMT, which browsers cannot parse. email.utils.format_datetime(value, usegmt=True) after the UTC conversion gives the English names regardless of locale. Same root cause as #8706 for feature flags.
@LemonCondor same question as the other one I asked today: do you still want this? It has been three weeks without a PR, and @anishmehta24 found a second problem in the same function, the %a/%b day and month names follow LC_TIME, so a German locale produces a header browsers can't parse. Both fixes belong in one PR: convert aware datetimes to UTC, and format with email.utils.format_datetime instead of strftime. Moving to 3.42.0. Wednesday is the cut, then it's open to anyone.
@LemonCondor @leandrodamascena the Wednesday cut has passed and no PR is linked here, so I opened #8876 with the fix. It converts aware datetimes to UTC and formats the date with email.utils.format_datetime, which also fixes the LC_TIME day and month names found earlier in this thread. It adds three tests in tests/unit/test_cookie_class.py.
@LemonCondor if your fix is ready, tell me and I'll close mine so yours can go ahead. Otherwise, @leandrodamascena, could you assign the issue to me?
Sorry for the late reply, @leandrodamascena. #8876 covers both problems, so please go ahead with it. Thanks @cleissonom for picking it up and @anishmehta24 for spotting the locale issue.
[!warning] This issue is now closed. Please be mindful that future comments are hard for our team to see. If you need more assistance, please either reopen the issue, or open a new issue referencing this one. If you wish to keep having a conversation with other community members under this issue feel free to do so.