python-3.xparsingpython-datetimetimezone-offset

Python strptime parse timezone format "GMT+-H"


I'm using python 3.7 and trying to figure out the correct format to get this code to work

dt = datetime.strptime("4 January 2022, 22:03 GMT-5", "%-d %b %Y, %H:%M %Zz")

The above line always fails. Is there something I can do to get it to parse? I am assuming its failing on the "GMT-5 part"

Edit: Adding context, the input string is scraped from a website so I need to find a way to turn it into a python datetime object so by code can understand when the event took place. Im not really sure how I could "In Code" change the input to match the required format of strptime


Solution

  • %z parsing directive won't parse an hour-only UTC offset (docs: requires ±HHMM[SS[.ffffff]] form). But you can derive a timezone object from a timedelta and set it like

    from datetime import datetime, timedelta, timezone
    
    s = "4 January 2022, 22:03 GMT-5"
    
    parts = s.split('GMT')
    
    dt = (datetime.strptime(parts[0].strip(), "%d %B %Y, %H:%M") # parse to datetime w/o offset
              .replace(tzinfo=timezone(timedelta(hours=int(parts[1]))))) # add UTC offset
    
    print(dt)
    # 2022-01-04 22:03:00-05:00