【问题标题】:Python pytz issue with DSTDST 的 Python pytz 问题
【发布时间】:2014-01-17 21:27:02
【问题描述】:

我正在使用命令

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

【问题讨论】:

    标签: python datetime timezone pytz


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2019-04-06
      • 2016-06-08
      • 2012-07-05
      • 1970-01-01
      • 2012-11-30
      • 2012-11-01
      • 2014-08-17
      • 2012-08-30
      • 2015-06-13
      相关资源
      最近更新 更多