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()返回自紀元以來的秒數。

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