I’m using the following to get utc datetime:
import datetime
import time
from pytz import timezone
now_utc = datetime.datetime.now(timezone('UTC'))
now = time.time()
print now_utc.time(), now
>> 04:51:39.337515 1332823899.34
I need to convert the format of now_utc.time() to look like the output of time.time().
How do I do that? Or, how to I get time.time() in utc?
time.mktime(now_utc.timetuple())Updating the answer, since it was completely wrong, but mistake I made can be useful for others.
time.mktimedoes not retain the timezone, using the tuple as a local time, so this:would rather work.
But, to make it short,
time.time(), as explicitly stated in the spec, returns an UTC timestamp, so just use it.(Also, you don’t need pytz to get an UTC datetime,
datetime.utcnow()does the same)