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

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

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


回答 0

time.time()函数返回自纪元以来的秒数,以秒为单位。请注意,“时代”定义为UTC的1970年1月1日开始。因此,以UTC定义时代,并确定全球时间。无论您身在何处,“ time.time()”都会在同一时刻返回相同的值。

这是我在计算机上运行的一些示例输出,也将其转换为字符串。

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

ts变量是在几秒钟内返回的时间。然后,我使用datetime库将其转换为字符串,从而使其成为人类可读的字符串。

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.


回答 1

这是用于时间戳记文本形式可以在文本文件中使用。(问题的标题在过去是不同的,因此对该答案的介绍进行了更改,以阐明如何将其解释为时间。[2016年1月14日更新])

您可以使用.now().utcnow()来将时间戳记作为字符串获取datetime.datetime

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

now不同之处utcnow与预期的不同-否则它们以相同的方式工作:

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

您可以将时间戳显式呈现给字符串:

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

或者,您甚至可以更明确地以自己喜欢的方式格式化时间戳记:

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

如果要使用ISO格式,请使用.isoformat()对象的方法:

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

您可以在变量中使用这些变量来进行计算和打印,而无需进行转换。

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

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

回答 2

根据#squiguy的答案,要获得真实的时间戳,我会键入从float转换的时间戳。

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

至少那是概念。

Based on the answer from #squiguy, to get a true timestamp I would type cast it from float.

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

At least that’s the concept.


回答 3

答案可能不是两者皆有。

  • 都不:time.time()返回距新纪元已过去的秒数。结果不取决于时区,因此它既不是UTC也不是本地时间。这是“自大纪元以来的第二个”POSIX定义

  • 两者:time.time()不需要同步系统时钟,因此它可以反映其值(尽管它与本地时区无关)。不同的计算机可能同时获得不同的结果。另一方面,如果您的计算机时间同步的,那么很容易从时间戳中获取UTC时间(如果我们忽略leap秒):

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

有关如何从各种Python版本的UTC时间获取时间戳的信息,请参见如何根据UTC将日期转换为自纪元以来的秒数?

The answer could be neither or both.

  • neither: time.time() returns approximately the number of seconds elapsed since the Epoch. The result doesn’t depend on timezone so it is neither UTC nor local time. Here’s POSIX defintion for “Seconds Since the Epoch”.

  • 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). 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):

    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?


回答 4

我最终选择了:

>>> import time
>>> time.mktime(time.gmtime())
1509467455.0

I eventually settled for:

>>> import time
>>> time.mktime(time.gmtime())
1509467455.0

回答 5

在特定的时区没有“时代”这样的东西。纪元已明确定义为特定时间,因此,如果您更改时区,则时间本身也会改变。具体来说,这次是Jan 1 1970 00:00:00 UTC。因此time.time()返回自纪元以来的秒数。

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. So time.time() returns the number of seconds since the epoch.


回答 6

时间戳始终是utc中的时间,但是当您调用datetime.datetime.fromtimestamp 它时,它会返回与该时间戳相对应的本地时区中的时间,因此结果取决于您的语言环境。

>>> import time, datetime

>>> time.time()
1564494136.0434234

>>> datetime.datetime.now()
datetime.datetime(2019, 7, 30, 16, 42, 3, 899179)
>>> datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2019, 7, 30, 16, 43, 12, 4610)

存在arrow具有不同行为的漂亮的库。在相同情况下,它会返回带有UTC时区的时间对象。

>>> import arrow
>>> arrow.now()
<Arrow [2019-07-30T16:43:27.868760+03:00]>
>>> arrow.get(time.time())
<Arrow [2019-07-30T13:43:56.565342+00:00]>

timestamp is always time in utc, but when you call datetime.datetime.fromtimestamp it returns you time in your local timezone corresponding to this timestamp, so result depend of your locale.

>>> import time, datetime

>>> time.time()
1564494136.0434234

>>> datetime.datetime.now()
datetime.datetime(2019, 7, 30, 16, 42, 3, 899179)
>>> datetime.datetime.fromtimestamp(time.time())
datetime.datetime(2019, 7, 30, 16, 43, 12, 4610)

There exist nice library arrow with different behaviour. In same case it returns you time object with UTC timezone.

>>> import arrow
>>> arrow.now()
<Arrow [2019-07-30T16:43:27.868760+03:00]>
>>> arrow.get(time.time())
<Arrow [2019-07-30T13:43:56.565342+00:00]>

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。