Repository navigation
Description
Expected Behaviour
The Expires attribute of a Cookie is the instant given in expires, written in GMT. Two datetime values that are the same instant produce the same Expires attribute, whatever timezone they are expressed in.
Current Behaviour
Cookie.__str__ formats expires with strftime("%a, %d %b %Y %H:%M:%S GMT") (_format_date in aws_lambda_powertools/shared/cookies.py). The GMT suffix is a literal and the datetime is never converted, so a timezone-aware datetime that is not in UTC has its local wall-clock time labelled as GMT.
For 12:00 UTC expressed as 07:00 at UTC-5, the cookie says 07:00:00 GMT, so the browser drops it five hours early. With a positive offset it lives longer than intended.
s=v; Expires=Thu, 01 Jan 2026 12:00:00 GMT; Secure
s=v; Expires=Thu, 01 Jan 2026 07:00:00 GMT; Secure
Both lines come from the same instant.
Code snippet
from datetime import datetime, timedelta, timezone
from aws_lambda_powertools.shared.cookies import Cookie
utc = datetime(2026, 1, 1, 12, 0, tzinfo=timezone.utc)
local = utc.astimezone(timezone(timedelta(hours=-5)))
assert utc == local # the same instant
print(Cookie("s", "v", expires=utc))
print(Cookie("s", "v", expires=local))Possible Solution
In _format_date, convert an aware datetime to UTC before formatting:
if timestamp.utcoffset() is not None:
timestamp = timestamp.astimezone(timezone.utc)A naive datetime has no offset to convert from and would keep being written as is, so nothing changes for it or for a datetime already in UTC.
I have this change ready with two tests in tests/unit/test_cookie_class.py (an aware UTC datetime, and an aware non-UTC datetime that fails on develop and passes with the change). I'm happy to open the PR once this is triaged.
Steps to Reproduce
- Install
aws-lambda-powertools. - Run the snippet above.
- The two printed cookies have different
Expiresvalues, although both datetimes are the same instant. The second should also readThu, 01 Jan 2026 12:00:00 GMT.
Did you run the reproduction yourself?
Yes, I ran it and saw the failure
Run locally against develop (be21802) on Python 3.13, macOS, not inside a Lambda function. An AI coding assistant was used to find and analyse this.
Powertools for AWS Lambda (Python) version
3.41.0 (develop at be21802)
AWS Lambda function runtime
3.13
Packaging format used
PyPi
Debugging logs
No response
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsComing soon
Activity
boring-cyborg commentedon Nov 23, 2026
Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out the #python channel on our Powertools for AWS Lambda Discord: Invite link
powertools-for-aws-oss-automation commentedon Nov 23, 2026
Thanks for opening your first issue here! We'll come back to you as soon as we can.
In the meantime, check out our GitHub Discussions: Join the conversation
leandrodamascena commentedon Nov 25, 2026
Thanks @omkarbandikatte, this is the same bug as #8671:
_format_datestampedGMTon the wall-clock time of an aware datetime. The fix landed in #8876 and goes out in 3.42.0: aware datetimes are converted to UTC first, naive ones still mean UTC. Closing as a duplicate.powertools-for-aws-oss-automation commentedon Nov 25, 2026
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.