After going through (parts of) the documentation and several threads here, I am attempting to make some simple code to get a particular time of today converted to a different timezone. Please note that I am not interested in the current time, but rather some specific time conversion (eg. 16:00:00 of today in EST converted to CET, for instance).
To that end, I have come up with the following:
from datetime import datetime
from pytz import timezone
def get_time(hour, minute, second=0, from_dt="US/Eastern", to_dt="Europe/Amsterdam"):
# Get a datetime of today in Europe/Amsterdam from XX:XX:XX in US/Eastern
today = datetime.today()
dt = datetime(year=today.year, month=today.month, day=today.day, hour=hour, minute=minute, second=second, tzinfo=timezone(from_dt))
return dt.astimezone(tz=timezone(to_dt)).strftime("%H:%M:%S")
print(get_time(16,0))
print(get_time(16,0,from_dt="Europe/Amsterdam",to_dt="US/Eastern"))
However, to my surprise this results in the very confusing:
>>> print(get_time(16,0))
22:56:00
>>> print(get_time(16,0,from_dt="Europe/Amsterdam",to_dt="US/Eastern"))
11:42:00
I don't understand what's going on: I would expect the result to be an exact hour (ie. 00 minutes), and a consistent conversion (it shows +7 one way, -4 the other way).