0

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

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • 1
    `1900-01-01 10:00:00-05:00` is not a range, it means 1900 Jan 1, 10:00, 5 hours offset from UTC. – abarnert Jun 22 '18 at 21:34
  • @abarnert - OH! Wow. So it seems to be working correctly then. Whoops! Thanks for pointing that out. (I'll delete this post shortly) – BruceWayne Jun 22 '18 at 21:36
  • 1
    Also, don't convert the `datetime` to a string and then try to strip stuff off of the result; just keep it around as a `datetime`, and then use `strftime` to format it however you want for printout. Like `dt.strftime('%H:%S')`. Or, if you're using `str.format` or f-strings, you can even just throw the strftime format into the format spec: `print(f'The time is {dt:%H:HS}')`. – abarnert Jun 22 '18 at 21:41
  • @abarnert if you want to put both comments in an answer, I'll mark it. Or, should I just delete the question? – BruceWayne Jun 22 '18 at 22:49
  • 1
    Answered… but I think maybe this is just a dup of the question you already linked to in your question? Let's give it time and see if anyone else votes to close. – abarnert Jun 22 '18 at 23:07

1 Answers1

1

The date is fine, I can remove that.

Better than converting to a string and then trying to munge that, just keep it as a datetime object until you need a string, and then use the strftime method to format it however you want. For example:

>>> dt.strftime('%H:%M')
10:00

Or, if you're using f-strings or str.format, you can even put that directly in the format spec for a datetime object:

>>> print(f'The time sponsored by Accurist is {dt:%H:%M}, precisely.')
The time sponsored by Accurist is 10:00, precisely.

I am not sure why the time though seems to be a range?

Actually, it's not. The default str format for displaying datetime objects is based on ISO 8601.1 For local times that know their time zone's UTC offset at the time in question, it's included as a +02:00 or -05:00 on the end.

I was expecting 24 hour format output.

That's already the default for the str output.

But, more importantly, that's what you always get when you ask strftime for %H. (If you want 12 hour, that's %I.)


1. But not with default settings for all of the options, like T as the time separator. If you want that, you have to call the isoformat method.

abarnert
  • 354,177
  • 51
  • 601
  • 671