简体   繁体   English

DST的python pytz问题

[英]Python pytz issue with DST

I am using the command 我正在使用命令

datetime.datetime.fromtimestamp(int(1364691600)).replace(tzinfo=pytz.utc).astimezone(pytz.timezone("Europe/London"))

This returns 这返回

datetime.datetime(2013, 3, 31, 3, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>)

Surely this should return 当然应该返回

datetime.datetime(2013, 3, 31, 2, 0, tzinfo=<DstTzInfo 'Europe/London' BST+1:00:00 DST>)

The reason I think this is because when BST switches it is 1 hour yet here it is doing 2 hours 我认为这是因为当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>)

fromtimestamp(tz=None) uses your local timezone and your local timezone is not utc and therefore it is incorrect to call .replace(tzinf=pytz.utc) on the result. fromtimestamp(tz=None)使用您的本地时区,而本地时区不是utc,因此在结果上调用.replace(tzinf=pytz.utc)是不正确的。

Pass the timezone directly instead: 直接传递时区:

>>>> 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>)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM