Home > Software design >  Python strptime parse timezone format "GMT -H"
Python strptime parse timezone format "GMT -H"

Time:01-05

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

CodePudding user response:

You're using the wrong format for the month and invalid text for the UTC offset (it has to be four digits, as described in the documentation):

>>> datetime.datetime.strptime("4 January 2022, 22:03 GMT-0500", "%d %B %Y, %H:%M %Z%z")
datetime.datetime(2022, 1, 4, 22, 3, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=68400), 'GMT'))

CodePudding user response:

%z parsing directive won't parse an hour-only UTC offset, 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
  •  Tags:  
  • Related