pytz:_utcoffset为伊朗提供了一个错误的值



正确值:

>>> pytz.timezone('Asia/Tehran').utcoffset(datetime(2013, 1, 1)).total_seconds()/3600.0
3.5
>>> pytz.timezone('Asia/Tehran').utcoffset(datetime(2013, 1, 1)).total_seconds()
12600.0

错误值:

>>> pytz.timezone('Asia/Tehran')._utcoffset.total_seconds()/3600.0
3.433333333333333
>>> pytz.timezone('Asia/Tehran')._utcoffset.total_seconds()
12360.0

我想知道utcoffset()方法中是否使用了_utcoffset属性,为什么该方法在属性错误的情况下工作
看起来还是个bug
如果将Asia/Tehran替换为Iran ,则不会发生任何变化

>>> print pytz.VERSION
2012c

操作系统:Linux Mint 15(Olivia)
使用Python 2.7

让我们看看这里发生了什么:

>>> tz = pytz.timezone('Asia/Tehran')
>>> tz
<DstTzInfo 'Asia/Tehran' LMT+3:26:00 STD>

这意味着时区以LMT表示,即太阳时间。这就是为什么你会看到12360的utccoffset——这里没有错误,它只是使用不同的参考计算的。

现在,如果你这样做:

>>> d = tz.localize(datetime(2013, 1, 1))
>>> d
datetime.datetime(2013, 1, 1, 0, 0, tzinfo=<DstTzInfo 'Asia/Tehran' IRST+3:30:00 STD>)
>>> d.utcoffset()
datetime.timedelta(0, 12600)

localize方法使表示切换到该日期和地点使用的正确时区,即utccoffset为12600秒的IRST。

这正是tzinfo对象的utcoffset方法所做的——它定位给定的日期时间对象并返回其utccoffset。

同样,如果你现在这样做:

>>> d = datetime.now(tz)
>>> d
datetime.datetime(2013, 8, 15, 20, 46, 4, 705896, tzinfo=<DstTzInfo 'Asia/Tehran' IRDT+4:30:00 DST>)
>>> d.utcoffset()
datetime.timedelta(0, 16200)

您将获得以IRDT表示的日期时间,因为当前夏令时在该时区生效。

相关内容

最新更新