python - 为什么我在 python 中看到时区异常

标签 python python-3.x pytz

<分区>

使用 Python3,我执行以下操作:

from datetime import datetime
from pytz import timezone
df = datetime(2018, 6, 1, hour=0, minute=0, tzinfo=timezone('US/Eastern'))
print(df)
2018-06-01 00:00:00-04:56

那么,为什么日期时间设置为 -4:56 的偏移量?不应该是-5:00吗?为什么要休息 4 分钟?

最佳答案

eastern_tz = timezone('US/Eastern')
eastern_time = eastern_tz.localize(datetime(2018, 6, 1, 0, 0))

这会给你正确的结果:

>>> print(eastern_time)
2018-06-01 00:00:00.000000-04:00

原因是python datetime 模块timezone 对象不直接兼容pytz timezone 对象。 pytz 文档说:

if you want to create local wallclock times you need to use the localize() method documented in this document

关于python - 为什么我在 python 中看到时区异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50972939/

相关文章:

python - 为什么 PST(美国/洛杉矶)时间转换为 UTC 时间晚 1 小时?

python - 如何查找列表中句子列表中每个单词的引理和频率计数?

Python 3 映射字典更新方法到其他字典列表

python - Pytorch:在 dataloader.dataset 上使用 torch.utils.random_split() 后,数据中缺少批量大小

python - 将时区缩写解析为 UTC

python - pytz:为什么这些不同的方法会给出不同的 UTC 偏移量?

python - 多态性或继承或任何其他建议?

python - 在 python 列表类型结构中合理存储 10 亿多个值

java - Java 还是 Python 更适合在 Google App Engines 上编写网页源代码检查 Web 服务?

python - 根据列表项第一个元素的邻接性合并列表项