python不正确的时区转换使用pytz

Posted

技术标签:

【中文标题】python不正确的时区转换使用pytz【英文标题】:python incorrect timezone conversion using pytz 【发布时间】:2011-07-02 11:12:25 【问题描述】:

我在 python 中编写了以下脚本,将日期时间从任何给定时区转换为 EST。

from datetime import datetime, timedelta  
from pytz import timezone  
import pytz  
utc = pytz.utc  

# Converts char representation of int to numeric representation '121'->121, '-1729'->-1729 
def toInt(ch):  
    ret = 0  
    minus = False  
    if ch[0] == '-':  
        ch = ch[1:]  
        minus = True  
    for c in ch:  
        ret = ret*10 + ord(c) - 48  
    if minus:  
        ret *= -1  
    return ret  

# Converts given datetime in tzone to EST. dt = 'yyyymmdd' and tm = 'hh:mm:ss' 
def convert2EST(dt, tm, tzone): 
    y = toInt(dt[0:4]) 
    m = toInt(dt[4:6]) 
    d = toInt(dt[6:8]) 
    hh = toInt(tm[0:2]) 
    mm = toInt(tm[3:5]) 
    ss = toInt(tm[6:8])

    # EST timezone and given timezone 
    est_tz = timezone('US/Eastern') 
    given_tz = timezone(tzone)

    fmt = '%Y-%m-%d %H:%M:%S %Z%z'

    # Initialize given datetime and convert it to local/given timezone 
    local = datetime(y, m, d, hh, mm, ss) 
    local_dt = given_tz.localize(local)


    est_dt = est_tz.normalize(local_dt.astimezone(est_tz)) 
    dt = est_dt.strftime(fmt) 
    print dt 
    return dt  

当我用 convert2EST('20110220', '11:00:00', 'America/Sao_Paulo')

输出为“2011-02-20 08:00:00 EST-0500”,但巴西的 DST 于 2 月 20 日结束,正确答案应为“2011-02-20 09:00:00 EST-0500”。

通过一些实验,我发现根据 pytz 巴西的 DST 在 2 月 27 日结束,这是不正确的。

pytz 是否包含错误数据或我遗漏了什么。任何帮助或 cmets 将不胜感激。

【问题讨论】:

您应该只使用内置的int 函数将您的字符串转换为整数。 和 strptime 转换为日期。 【参考方案1】:

首先稍微不那么疯狂的实现:

import datetime
import pytz

EST = pytz.timezone('US/Eastern')

def convert2EST(date, time, tzone):
    dt = datetime.datetime.strptime(date+time, '%Y%m%d%H:%M:%S')
    tz = pytz.timezone(tzone)
    dt = tz.localize(dt)
    return dt.astimezone(EST)

现在,我们尝试调用它:

>>> print convert2EST('20110220', '11:00:00', 'America/Sao_Paulo')
2011-02-20 09:00:00-05:00

如我们所见,我们得到了正确的答案。

更新:我明白了!

巴西在 2008 年更改了夏令时。目前尚不清楚之前的夏令时是什么,但您的数据可能已经过时了。

这可能不是 pytz 故障,因为 pytz 能够使用您的操作系统数据库。您可能需要更新操作系统。这就是(我猜)即使使用 2005 年的 pytz 也能得到正确答案的原因,它使用了我操作系统中的(更新的)数据。

【讨论】:

根据文档,pytz 使用奥尔森时区数据库,而不是操作系统时区。这可能只是升级到最新版本的 pytz 的问题。 pytz.sourceforge.net/#latest-versions @Mark Ransom:大多数 Unix 版本,包括所有 Linux 版本,以及我认为 OS X,使用 Olson 数据库。自从我阅读 pytz 源代码以来已经有一段时间了,但我有一个模糊的记忆,它在某些情况下使用操作系统数据库而不是包含的数据库。这几乎是唯一的可能性,除非他使用的是 8 岁的 pytz 版本。 你完全正确!我安装了旧版本的 pytz 并更新它解决了这个问题。谢谢伦纳特 太棒了。它一定很老了。 也许 pytz 自 2005 年以来已更改为在所有情况下都使用内置文件而不是操作系统?该文档似乎非常明确,尽管不能保证它是正确的。如果巴西在 2008 年发生变化,您只需要 3 年的副本。【参考方案2】:

似乎您已经回答了自己的问题。如果 pytz 说巴西的夏令时结束于 2 月 27 日,那就错了。巴西的 DST 以third Sunday of February 结束,除非该周日恰逢狂欢节期间;今年没有,所以 DST 不会延迟。

也就是说,您似乎在不必要地滚动自己的转换器。您应该查看time 模块,它可以简化gmt 和本地时间之间的转换等等。

【讨论】:

以上是关于python不正确的时区转换使用pytz的主要内容,如果未能解决你的问题,请参考以下文章

使用pytz的python时区转换问题

Pytz Python时区转换不起作用

pytz:为啥在时区之间转换时需要规范化?

使用 pytz 时区时 Python 日期时间不包括 DST

python模块-pytz时区转换

python 使用`pytz`将时间和日期对话转换为其他时区。