aio-libs / aio-libs/aiohttp

Naive datetime is interpreted as UTC when set last_modified

未关闭
#5,304 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
bug
主要语言
Python
星标
16.5k
派生
2.4k
平均合并
17 小时 22 分钟
30 天内合并 PR
212

描述

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.

贡献指南

打开贡献指南

评估

这个 Issue 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。