3

Is there any way to parse a string like

"Wed Aug 01 01:58:30 GMT 2020"

where GMT can be any possible timezone and then to convert the result of the parsed time to UTC timezone?

When I do

>>> datetime.strptime('Wed Aug 01 01:58:30 GMT 2020', '%a %b %d %H:%M:%S %Z %Y')
datetime.datetime(2020, 8, 1, 1, 58, 30)

I am not getting any timezone information (tzinfo of the returning objet is None). So I am unfortunately already stuck there in the process..

  • I'd look into [pytz](http://pytz.sourceforge.net) and [this previous question](https://stackoverflow.com/questions/79797) – Tim Stack Jul 05 '20 at 10:25
  • an option with only the `datetime` module would be `datetime.strptime('Wed Aug 01 01:58:30 GMT 2020'.replace('GMT', 'Z'), '%a %b %d %H:%M:%S %z %Y')` - %z parses 'Z' character to UTC tzinfo. – FObersteiner Jul 07 '20 at 06:52

1 Answers1

1

You can use the parse function from dateutil:

from dateutil.parser import parse
parse('Wed Aug 01 01:58:30 GMT 2020')

output: datetime.datetime(2020, 8, 1, 1, 58, 30, tzinfo=tzutc())

orestisf
  • 1,396
  • 1
  • 15
  • 30