pytz 的奇怪时区问题 [英] Weird timezone issue with pytz

查看:54
本文介绍了pytz 的奇怪时区问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<预><代码>>>>进口pytz>>>pytz.timezone('亚洲/香港')<DstTzInfo 'Asia/Hong_Kong' LMT+7:37:00 STD>

7 小时 37 分钟的偏移?这有点奇怪,有人遇到过同样的问题吗?

事实上,我的行为有所不同

导入pytz从日期时间导入日期时间hk = pytz.timezone('亚洲/香港')dt1 = 日期时间(2012,1,1,tzinfo=hk)dt2 = hk.localize(datetime(2012,1,1))如果 dt1 >DT2:打印为什么?"

解决方案

时区和偏移量多年来一直在变化.pytz 创建时区对象时传递的默认区域名称和偏移量是该区域最早可用的名称,有时它们看起来有点奇怪.当您使用 localize 将区域附加到日期时,将替换正确的区域名称和偏移量.仅使用 datetime 构造函数将区域附加到日期并不能使其正确调整.

>>> import pytz
>>> pytz.timezone('Asia/Hong_Kong')
<DstTzInfo 'Asia/Hong_Kong' LMT+7:37:00 STD>

A seven hour and 37 minute offset? This is a little strange, does anyone experience the same issue?

In fact I'm getting different behavior between

import pytz
from datetime import datetime
hk = pytz.timezone('Asia/Hong_Kong')

dt1 = datetime(2012,1,1,tzinfo=hk)
dt2 = hk.localize(datetime(2012,1,1))
if dt1 > dt2:
   print "Why?"

解决方案

Time zones and offsets change over the years. The default zone name and offset delivered when pytz creates a timezone object are the earliest ones available for that zone, and sometimes they can seem kind of strange. When you use localize to attach the zone to a date, the proper zone name and offset are substituted. Simply using the datetime constructor to attach the zone to the date doesn't allow it to adjust properly.

这篇关于pytz 的奇怪时区问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆