在蟒蛇中将山地标准时间转换为东部标准时间

Moh*_*hit 5 python timezone datetime

我有一个时间datetime对象..它有日期和时间..

所以例如

    d = (2011,11,1,8,11,22)  (24 hour time time format)
Run Code Online (Sandbox Code Playgroud)

但这张时间戳是在山地标准时间..(亚利桑那州.凤凰城)

现在我想在EST中转换这个时间...

现在这只是时间增量调整..

但是,这也是夏令时问题.

我想知道是否有一种内置的方法来处理夏令时调整时区.

Emm*_*l N 5

使用pytz进行时区转换.pytz考虑夏令时检查这个.你需要一个帮助函数,如:

def convert(dte, fromZone, toZone):
    fromZone, toZone = pytz.timezone(fromZone), pytz.timezone(toZone)
    return fromZone.localize(dte, is_dst=True).astimezone(toZone)
Run Code Online (Sandbox Code Playgroud)


Phi*_*lip 4

您正在寻找的库是pytz,特别是 localize() 方法。

Pytz 不在标准库中,但您可以通过 pip 或 easy_install 获取它。