Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 337
Saransh Mohapatra Avatar asked Dec 15 '12 09:12

Saransh Mohapatra


People also ask

Does Python time time return UTC?

The time. time() function returns the number of seconds since the epoch, as seconds. Note that the "epoch" is defined as the start of January 1st, 1970 in UTC. So the epoch is defined in terms of UTC and establishes a global moment in time.

What does time time () return in Python?

The time() function returns the number of seconds passed since epoch. For Unix system, January 1, 1970, 00:00:00 at UTC is epoch (the point where time begins).

How does time time work Python?

Python's time module provides a function for getting local time from the number of seconds elapsed since the epoch called localtime() . Notice that tm_isdst=0 . Since DST matters with local time, tm_isdst will change between 0 and 1 depending on whether or not DST is applicable for the given time.

How do I show UTC time in Python?

Getting the UTC timestampUse the datetime. datetime. now() to get the current date and time. Then use tzinfo class to convert our datetime to UTC.


2 Answers

The time.time() function returns the number of seconds since the epoch, as seconds. Note that the "epoch" is defined as the start of January 1st, 1970 in UTC. So the epoch is defined in terms of UTC and establishes a global moment in time. No matter where you are "seconds past epoch" (time.time()) returns the same value at the same moment.

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.

like image 58
squiguy Avatar answered Oct 13 '22 12:10

squiguy


This is for the text form of a timestamp that can be used in your text files. (The title of the question was different in the past, so the introduction to this answer was changed to clarify how it could be interpreted as the time. [updated 2016-01-14])

You can get the timestamp as a string using the .now() or .utcnow() of the datetime.datetime:

>>> import datetime >>> print datetime.datetime.utcnow() 2012-12-15 10:14:51.898000 

The now differs from utcnow as expected -- otherwise they work the same way:

>>> print datetime.datetime.now() 2012-12-15 11:15:09.205000 

You can render the timestamp to the string explicitly:

>>> str(datetime.datetime.now()) '2012-12-15 11:15:24.984000' 

Or you can be even more explicit to format the timestamp the way you like:

>>> datetime.datetime.now().strftime("%A, %d. %B %Y %I:%M%p") 'Saturday, 15. December 2012 11:19AM' 

If you want the ISO format, use the .isoformat() method of the object:

>>> datetime.datetime.now().isoformat() '2013-11-18T08:18:31.809000' 

You can use these in variables for calculations and printing without conversions.

>>> ts = datetime.datetime.now() >>> tf = datetime.datetime.now() >>> te = tf - ts >>> print ts 2015-04-21 12:02:19.209915 >>> print tf 2015-04-21 12:02:30.449895 >>> print te 0:00:11.239980 
like image 43
pepr Avatar answered Oct 13 '22 13:10

pepr