aio-libs / aio-libs/aiohttp

Naive datetime is interpreted as UTC when set last_modified

Aperta
#5,304 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub
bug
Lingua principale
Python
Stelle
16.5k
Fork
2.4k
Merge medio
17h 22m
PR unite (30g)
212

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.