datetime.now()
doesn't appear to have timezone info attached. I want the current time in UTC. What do I do?
>>> from datetime import datetime
>>> y = datetime.now()
>>> y
datetime.datetime(2014, 3, 11, 11, 18, 33, 598489)
At first I thought datetime.utcnow()
was the obvious solution, but then I discovered that it doesn't have timezone info attached either (WTF?).
>>> from datetime import datetime
>>> y = datetime.utcnow()
>>> y
datetime.datetime(2014, 3, 11, 16, 19, 40, 238810)
In Python 3:
In Python 2.x there is no timezone object, but you can write your own:
Then you can do
datetime.now(timezone.utc)
just like in Python 3.Use
datetime.utcnow()
for the current time in UTC.Adapting from this answer, here is how to make the object timezone-aware.
I use pytz
Then use the following bit of code