Python 的 time.time() 是否返回本地或 UTC 时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13890935/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Does Python's time.time() return the local or UTC timestamp?
提问by Saransh Mohapatra
Does time.time()in the Python time module return the system's time or the time in UTC?
是否time.time()Python的时间模块系统返回的时间或UTC时间?
采纳答案by squiguy
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.
该time.time()函数返回自纪元以来的秒数,以秒为单位。请注意,“纪元”被定义为 UTC 中 1970 年 1 月 1 日的开始。因此,纪元是根据 UTC 定义的,并建立了一个全球时刻。无论您身在何处,“时代过去的秒数”(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 tsvariable is the time returned in seconds. I then converted it to a string using the datetimelibrary making it a string that is human readable.
该ts变量是在几秒钟内返回的时间。然后我使用datetime库将其转换为字符串,使其成为人类可读的字符串。
回答by pepr
This is for the text form of a timestampthat 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-01-14更新])
You can get the timestamp as a string using the .now()or .utcnow()of the datetime.datetime:
您可以使用.now()或.utcnow()的字符串获取时间戳datetime.datetime:
>>> import datetime
>>> print datetime.datetime.utcnow()
2012-12-15 10:14:51.898000
The nowdiffers from utcnowas 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
回答by Rudi Strydom
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.
至少是这个概念。
回答by jfs
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 issynchronized 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)
两者都不是:
time.time()返回大约自 Epoch 以来经过的秒数。结果不依赖于时区,因此既不是 UTC 时间也不是本地时间。这是“自纪元以来的秒数”的POSIX 定义。both:
time.time()不需要你的系统时钟同步,所以它反映了它的价值(尽管它与本地时区无关)。不同的计算机可能同时得到不同的结果。另一方面,如果您的计算机时间是同步的,那么很容易从时间戳中获取 UTC 时间(如果我们忽略闰秒):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 将日期转换为自纪元以来的秒数?
回答by HyperNeutrino
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.
在特定时区没有“纪元”这样的东西。纪元被明确定义为特定时刻,因此如果您更改时区,时间本身也会发生变化。具体来说,这次是Jan 1 1970 00:00:00 UTC。所以time.time()返回自纪元以来的秒数。
回答by Natim
I eventually settled for:
我最终解决了:
>>> import time
>>> time.mktime(time.gmtime())
1509467455.0
回答by Ryabchenko Alexander
timestamp is always time in utc, but when you call datetime.datetime.fromtimestampit returns you time in your local timezone corresponding to this timestamp, so result depend of your locale.
时间戳始终是 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)
There exist nice library arrowwith different behaviour. In same case it returns you time object with UTC timezone.
存在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]>

