Python time模块中的time.time()返回的是系统时间还是UTC时间?
time.time()函数的作用是:以浮点数的形式返回自epoch开始的秒数。请注意,“epoch”定义为UTC时间1970年1月1日的开始。因此,epoch是根据UTC来定义的,并建立了一个全局时间点。无论你在地球上的什么地方,"seconds past epoch" (time.time())在同一时刻返回相同的值。
下面是我在计算机上运行的一些示例输出,并将其转换为字符串。
>>> import time
>>> ts = time.time()
>>> ts
1355563265.81
>>> import datetime
>>> datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
'2012-12-15 01:21:05'
>>>
ts变量是以秒为单位返回的时间。然后,我使用datetime库将其转换为人类可读的字符串。
这是针对可以在文本文件中使用的时间戳的文本形式。(这个问题的标题在过去是不同的,所以这个答案的介绍被改变了,以澄清它如何解释为时间。[2016-01-14]更新)
你可以使用datetime.datetime的.now()或.utcnow()来获取时间戳字符串:
>>> 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
根据来自#squiguy的答案,为了获得一个真实的时间戳,我将从float类型转换它。
>>> import time
>>> ts = int(time.time())
>>> print(ts)
1389177318
至少这是一个概念。
答案可能是两者都不是,也可能都是。
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)
有关如何在各种Python版本中从UTC时间获取时间戳,请参阅如何根据UTC获取从epoch开始转换为秒的日期?
在特定的时区里没有所谓的“epoch”。epoch被定义为一个特定的时间点,因此如果您改变了时区,时间本身也会改变。具体来说,这个时间是1970年1月1日00:00:00 UTC。因此,time.time()返回自epoch以来的秒数。
时间戳总是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)
存在不同行为的漂亮库箭头。在同样的情况下,它返回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]>
要使用datetime库获取本地时间戳,请使用Python 3.x
#wanted format: year-month-day hour:minute:seconds
from datetime import datetime
# get time now
dt = datetime.now()
# format it to a string
timeStamp = dt.strftime('%Y-%m-%d %H:%M:%S')
# print it to screen
print(timeStamp)
Time.time()返回Unix时间戳。 您可以使用datetime库获取本地时间或UTC时间。
import datetime
local_time = datetime.datetime.now()
print(local_time.strftime('%Y%m%d %H%M%S'))
utc_time = datetime.datetime.utcnow()
print(utc_time.strftime('%Y%m%d %H%M%S'))