Converting naive datetime object to UTC timezone

Viewed 219

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..

1 Answers

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())

Related