3
votes

I'm bemused by the fact that in Python datetime.datetime.utcnow() returns a timezone-naive datetime object. I would expect it to be timezone-aware with UTC as its timezone, but whatever. In order to convert this to a timezone-aware datetime object I would do the following:

import pytz
from datetime import datetime

us_pacific = pytz.timezone('US/Pacific')
utc_now = datetime.utcnow()
pacific_now = us_pacific.fromutc(utc_now)

What I'd like to know now is how to go in the opposite direction. Given a timezone-aware datetime object, I'd like to get back to the corresponding timezone-naive UTC datetime object.

1

1 Answers

5
votes
# First convert back to UTC
utc_time = pacific_now.astimezone(pytz.utc)

# Remove the timezone attribute to make it timezone-naive
utc_time = utc_time.replace(tzinfo=None)