Python的time.time()返回本地或UTC时间戳吗?

本文翻译自:Does Python's time.time() return the local or UTC timestamp?

Python时间模块中的time.time()返回系统时间还是UTC时间?


#1楼

参考:https://stackoom.com/question/wHfL/Python的time-time-返回本地或UTC时间戳吗


#2楼

The time.time() function returns the number of seconds since the epoch, as seconds. time.time()函数返回自纪元以来的秒数,以秒为单位。 Note that the "epoch" is defined as the start of January 1st, 1970 in UTC. 请注意,“时代”定义为UTC的1970年1月1日开始。 So the epoch is defined in terms of UTC and establishes a global moment in time. 因此,以UTC定义时代,并确定全球时间。 No matter where you are "seconds past epoch" (time.time()) returns the same value at the same moment. 无论您身处“历时秒”(time.time())在哪里,都将在同一时刻返回相同的值。

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. ts变量是返回的时间(以秒为单位)。 I then converted it to a string using the datetime library making it a string that is human readable. 然后,我使用datetime库将其转换为字符串,使其成为易于阅读的字符串。


#3楼

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]) (问题的标题在过去是不同的,因此对此答案的介绍进行了更改,以阐明如何将其解释为时间。[2016年1月14日更新])

You can get the timestamp as a string using the .now() or .utcnow() of the datetime.datetime : 您可以使用datetime.datetime.now().utcnow()将时间戳记作为字符串获取:

>>> 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: now与预期的utcnow不同-否则它们的工作方式相同:

>>> 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: 如果要使用ISO格式,请使用对象的.isoformat()方法:

>>> 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

#4楼

Based on the answer from #squiguy, to get a true timestamp I would type cast it from float. 根据#squiguy的答案,要获得真实的时间戳,我会键入从float转换的时间戳。

>>> import time
>>> ts = int(time.time())
>>> print(ts)
1389177318

At least that's the concept. 至少那是概念。


#5楼

The answer could be neither or both. 答案可能不是两者皆有。

  • neither: time.time() returns approximately the number of seconds elapsed since the Epoch. 都不: time.time()大约返回自纪元以来经过的秒数。 The result doesn't depend on timezone so it is neither UTC nor local time. 结果不取决于时区,因此它既不是UTC也不是本地时间。 Here's POSIX defintion for "Seconds Since the Epoch" . 这是“自大纪元以来的第二个”POSIX定义

  • both: time.time() doesn't require your system's clock to be synchronized so it reflects its value (though it has nothing to do with local timezone). 两者: time.time()不需要同步系统时钟,因此可以反映其值(尽管它与本地时区无关)。 Different computers may get different results at the same time. 不同的计算机可能同时获得不同的结果。 On the other hand if your computer time is synchronized then it is easy to get UTC time from the timestamp (if we ignore leap seconds): 另一方面,如果您的计算机时间同步的,那么很容易从时间戳中获取UTC时间(如果我们忽略leap秒):

     from datetime import datetime utc_dt = datetime.utcfromtimestamp(timestamp) 

On how to get timestamps from UTC time in various Python versions, see How can I get a date converted to seconds since epoch according to UTC? 有关如何从各种Python版本的UTC时间获取时间戳的信息,请参见如何根据UTC将日期转换为自纪元以来的秒数?


#6楼

There is no such thing as an "epoch" in a specific timezone. 在特定的时区没有“时代”这样的东西。 The epoch is well-defined as a specific moment in time, so if you change the timezone, the time itself changes as well. 纪元已明确定义为特定时间,因此,如果您更改时区,则时间本身也会改变。 Specifically, this time is Jan 1 1970 00:00:00 UTC . 具体来说,这个时间是Jan 1 1970 00:00:00 UTC So time.time() returns the number of seconds since the epoch. 因此time.time()返回自纪元以来的秒数。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章