Saransh Mohapatra Saransh Mohapatra - 3 months ago 11
Python Question

Does Python's time.time() return the local or UTC timestamp?

Does

time.time()
in the Python time module return the system's time or the time in UTC?

Answer

The time.time() function returns the number of seconds since the epoch as seconds in UTC.

Here is some sample output I ran on my computer, converting it to a string as well.

Python 2.7.3 (default, Apr 24 2012, 00:00:54) 
[GCC 4.7.0 20120414 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import time
>>> ts = time.time()
>>> print ts
1355563265.81
>>> import datetime
>>> st = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
>>> print st
2012-12-15 01:21:05
>>>

The ts variable is the time returned in seconds. I then converted it to a string using the datetime library making it a string that is human readable.

Comments