Reputation: 33605
For whatever reason, pytz doesn't seem to be changing the hour of the datetime object.
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
What do I need to do to fix this? I want to convert datetime.now() to a different datetime.
Upvotes: 0
Views: 353
Reputation: 34994
The reason for that behavior is because datetime.now() returns a naive time. It doesn't add/subtract any time from it, because it doesn't have a way to know how much to add/subtract. To get the current datetime in a different timezone using pytz, just pass the timezone object to datetime.now()
:
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]:
Upvotes: 1