Python pytz不更新时区更改的小时数

Python pytz不更新时区更改的小时数,python,Python,不管出于什么原因,pytz似乎没有改变datetime对象的小时数 from pytz import timezone from datetime import datetime eastern = timezone('US/Eastern').localize(datetime.now()).hour central = timezone('US/central').localize(datetime.now()).hour assert eastern != central # Asse

不管出于什么原因,pytz似乎没有改变datetime对象的小时数

from pytz import timezone
from datetime import datetime

eastern = timezone('US/Eastern').localize(datetime.now()).hour
central = timezone('US/central').localize(datetime.now()).hour

assert eastern != central # AssertionError

我需要做什么来解决这个问题?我想将datetime.now()转换为另一个datetime。

这种行为的原因是datetime.now()返回一个原始时间。它在任何时候都不会加/减,因为它无法知道要加/减多少。要使用pytz获取不同时区中的当前日期时间,只需将时区对象传递给
datetime.now()


在您的示例中,这不是时区“更改”,因为datetime.now()不是以时区开头的。谢谢。哇!真是一团糟。“因为它没有办法知道加/减多少”我真的认为它有。。。请尝试时区('US/Eastern')。本地化(datetime.now())。strftime('%H')。我对Python中的datetime处理不太满意。@fny您正在对一个已经本地化的对象调用
.strftime()
timezone('US/Eastern')。localize(datetime.now())
本质上只是将一个时区分配给一个没有时区的datetime对象。那么为什么
timezone('US/Eastern')。localize(datetime.now()).hour
版本没有给出正确的小时?
In [32]: eastern = datetime.now(timezone('US/Eastern')).hour                                                                               

In [33]: central = datetime.now(timezone('US/Central')).hour                                                                               

In [34]: eastern                                                                                                                           
Out[34]: 18

In [35]: central                                                                                                                           
Out[35]: 17

In [36]: assert central != eastern                                                                                                         

In [37]: