Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting naive datetime object to UTC timezone [duplicate]

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

like image 792
user8225639 Avatar asked Feb 15 '26 22:02

user8225639


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

like image 116
orestisf Avatar answered Feb 17 '26 11:02

orestisf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!