Home > Back-end >  How do I convert between rfc-2822 time zones?
How do I convert between rfc-2822 time zones?

Time:10-08

I have datetimes in RFC 2822 format such as:

Wed, 06 Oct 2021 23:45:05 0000 (UTC)

and using email.utils to parse it.

But I would like to convert the zone to PDT (-0700) before parsing it.

How can that be done in Python 3.9 ?

CodePudding user response:

Technically you need to parse it before you can change it to pdt.

timestring = "Wed, 06 Oct 2021 23:45:05  0000 (UTC)"

import email, pytz
t = email.utils.parsedate_to_datetime(timestring)
pdttime = t.astimezone(pytz.timezone('US/PACIFIC'))
print(email.utils.format_datetime(pdttime))
  • Related