aio-libs / aio-libs/aiohttp

Naive datetime is interpreted as UTC when set last_modified

Ouverte
#5,304 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
bug
Langage dominant
Python
Étoiles
16.5k
Forks
2.4k
Merge moyen
17 h 22 min
PR mergées (30 j)
212

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.