Skip to main content
 首页 » 编程设计

python之时区错误 30分钟而不是1小时

2025年05月04日16jirigala

<分区>

有了这个,我现在已经挣扎了几个小时。该代码比我在此处提供的示例大得多,但它分解为:

我有一个天真的日期时间对象,我想将它转换为 UTC 时间,但这并没有按预期工作。

import datetime 
import pytz 
 
# Following is a naive datetime object, but we know the user meant 
# timezone Europe/Zurich 
 
zurich = datetime.datetime(2016, 1, 8, 7, 10) 
# datetime.datetime(2016, 1, 8, 7, 10) 
 
# So I'm now converting it to a datetime object which is aware of the 
# timezone 
 
zurich = zurich.replace(tzinfo=pytz.timezone('Europe/Zurich')) 
# datetime.datetime(2016, 1, 8, 7, 10, tzinfo=<DstTzInfo 'Europe/Zurich' BMT+0:30:00 STD>) 
 
# Let's convert to UTC 
 
zurich = zurich.astimezone(pytz.utc) 
# datetime.datetime(2016, 1, 8, 6, 40, tzinfo=<UTC>) 

苏黎世与 UTC 时间的时差为 +01:00(夏令时)或 +02:00(夏令时)。为什么 Python 认为它是 +00:30?!

非常感谢任何帮助(我已经开始拔头发了)。