I have a dictionary that contains times, set up like
{ '2018-06-22': { 24: { 24: { 'Team1': 'Nigeria',
'Team2': 'Iceland',
'Time': '18:00',
'Timezone': 'UTC+ ... }}
How can I take the Time and change from whatever zone it's in (UTC+2
, UTC
, UTC+3
, etc) and change to, say, USA Chicago (UTC-5)?
I've tried using the solution here but get 1900-01-01 10:00:00-05:00
. The date is fine, I can remove that. I am not sure why the time though seems to be a range? I was expecting 24 hour format output.
from datetime import datetime
from dateutil import tz
def update_timezone(time, old_zone, new_zone):
"""
Takes an old timezone and converts to the new one
"""
from_zone = tz.gettz(old_zone)
to_zone = tz.gettz(new_zone)
utc = datetime.strptime(time, "%H:%M")
utc = utc.replace(tzinfo=from_zone)
central = utc.astimezone(to_zone)
return central
print(update_timezone("18:00", "UTC+3","UTC-5"))
Output:
1900-01-01 10:00:00-05:00
Desired output:
11:00