我正在使用命令
datetime.datetime.fromtimestamp(int(1364691600)).replace(tzinfo=pytz.utc).astimezone(pytz.timezone("Europe/London"))
返回
datetime.datetime(2013, 3, 31, 3, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>)
当然应该返回
datetime.datetime(2013, 3, 31, 2, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>)
我认为这是因为当BST切换时它是1小时但是这里它正在做2小时
>>> datetime.datetime.fromtimestamp(int(1364691599)).replace(tzinfo=pytz.utc).astimezone(pytz.timezone("Europe/London"))
datetime.datetime(2013, 3, 31, 0, 59, 59, tzinfo=<DstTzInfo 'Europe/London' GMT0:00:00 STD>)
答案 0 :(得分:2)
fromtimestamp(tz=None)
使用您当地的时区,而您当地的时区不是utc,因此在结果上调用.replace(tzinf=pytz.utc)
是不正确的。
直接传递时区:
>>>> from datetime import datetime
>>> import pytz # $ pip install pytz
>>> datetime.fromtimestamp(1364691600, pytz.timezone("Europe/London"))
datetime.datetime(2013, 3, 31, 2, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>)