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

Naive datetime is interpreted as UTC when set last_modified #5304

Open
serhiy-storchaka opened this issue Dec 2, 2020 · 2 comments
Open
Labels

Comments

@serhiy-storchaka
Copy link
Contributor

When set the last_modified property of StreamResponse the naive datetime object is interpreted as UTC. But naive datetime objects are usually in local timezone. For example datetime.now() returns naive datetime objects in local timezone. It provokes bugs in user code.

For example:

$ cat test_last_modified2.py
from aiohttp.web import StreamResponse
from datetime import datetime, timezone

resp = StreamResponse()
dt = datetime.now(timezone.utc)
dt2 = datetime.now()
resp.last_modified = dt
print(resp.last_modified)
print(repr(resp.last_modified))
print(resp.headers["Last-Modified"])
resp.last_modified = dt2
print(resp.last_modified)
print(repr(resp.last_modified))
print(resp.headers["Last-Modified"])
$ python test_last_modified2.py
2020-12-02 10:06:47+00:00
datetime.datetime(2020, 12, 2, 10, 6, 47, tzinfo=datetime.timezone.utc)
Wed, 02 Dec 2020 10:06:47 GMT
2020-12-02 12:06:47+00:00
datetime.datetime(2020, 12, 2, 12, 6, 47, tzinfo=datetime.timezone.utc)
Wed, 02 Dec 2020 12:06:47 GMT

It would be better either raise an error if naive datetime object is passed or interpret it as local time.

@greshilov
Copy link
Contributor

I can pick up this two issues from Serhiy, since I worked recently with those parts of code in my ETag PR (#5298).

@asvetlov
Copy link
Member

asvetlov commented Dec 3, 2020

Happy to review.
@greshilov I know I should review yours #5298 for ETags.
Now sure if I can do it on workdays but don't worry please -- I'll find time for the review on the weekend at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants