Python中的時間函式?


Python 提供了使用“time”模組以多種方式讀取、表示和重置時間資訊的庫。日期、時間和日期時間在 Python 中是物件,因此無論何時對它們進行任何操作,實際上操作的是物件,而不是字串或時間戳。

在本節中,我們將討論“time”模組,它允許我們處理時間上的各種操作。

time 模組遵循“紀元”約定,該約定指的是時間開始的點。在 Unix 系統中,“紀元”時間從 1970 年 1 月 1 日凌晨 12:00 開始,持續到 2038 年。

要在您的系統上確定紀元時間值,只需鍵入以下程式碼 -

>>> import time
>>> time.gmtime(0)

輸出

time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0,
tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

Python中的Tick?

Tick 指的是一個時間間隔,它是一個浮點數,以秒為單位測量。有時我們得到的時間是夏令時 (DST),在夏令時期間,時鐘向前移動 1 小時,秋季再向後移動。

Python 時間模組中最常用的函式 -

1. time.time() 函式

time() 是 time 模組的主要函式。它測量自紀元以來的秒數,作為一個浮點值。

語法

time.time()

演示上述函式的程式

import time
print("Number of seconds elapsed since the epoch are : ", time.time())

輸出

Number of seconds elapsed since the epoch are : 1553262407.0398576

我們可以使用 python time 函式來計算兩點之間經過的掛鐘時間。

以下是計算掛鐘時間的程式

import time
start = time.time()
print("Time elapsed on working...")
time.sleep(0.9)
end = time.time()
print("Time consumed in working: ",end - start)

輸出

Time elapsed on working...
Time consumed in working: 0.9219651222229004

2. time.clock() 函式

time.clock() 函式返回處理器時間。它用於效能測試/基準測試。

語法

time.clock()

clock() 函式返回程式消耗的正確時間,它比其對應的函式更準確。

讓我們編寫一個使用上述兩個時間函式(上面討論的)來區分它們的程式

import time
template = 'time()# {:0.2f}, clock()# {:0.2f}'
print(template.format(time.time(), time.clock()))
for i in range(5, 0, -1):
   print('---Sleeping for: ', i, 'sec.')
time.sleep(i)
print(template.format(time.time(), time.clock())
)

輸出

time()# 1553263728.08, clock()# 0.00
---Sleeping for: 5 sec.
time()# 1553263733.14, clock()# 5.06
---Sleeping for: 4 sec.
time()# 1553263737.25, clock()# 9.17
---Sleeping for: 3 sec.
time()# 1553263740.30, clock()# 12.22
---Sleeping for: 2 sec.
time()# 1553263742.36, clock()# 14.28
---Sleeping for: 1 sec.
time()# 1553263743.42, clock()# 15.34

3. time.ctime() 函式

time.time() 函式以“自紀元以來的秒數”作為輸入,並根據本地時間將其轉換為人類可讀的字串值。如果未傳遞引數,則返回當前時間。

import time
print('The current local time is :', time.ctime())
newtime = time.time() + 60
print('60 secs from now :', time.ctime(newtime))

輸出

The current local time is : Fri Mar 22 19:43:11 2019
60 secs from now : Fri Mar 22 19:44:11 2019

4. time.sleep() 函式

time.sleep() 函式將當前執行緒的執行暫停指定的秒數。傳遞浮點值作為輸入以獲得更精確的睡眠時間。

sleep() 函式可用於需要等待檔案完成關閉或讓資料庫提交發生的情況。

import time
# using ctime() to display present time
print ("Time starts from : ",end="")
print (time.ctime())
# using sleep() to suspend execution
print ('Waiting for 5 sec.')
time.sleep(5)
# using ctime() to show present time
print ("Time ends at : ",end="")
print (time.ctime())

輸出

Time starts from : Fri Mar 22 20:00:00 2019
Waiting for 5 sec.
Time ends at : Fri Mar 22 20:00:05 2019

5. time.struct_time 類

time.struct_time 是 time 模組中唯一存在的 資料結構。它具有命名的元組介面,可以透過索引或屬性名稱訪問。

語法

time.struct_time

當您需要訪問日期的特定欄位時,此類很有用。

此類提供許多函式,例如 localtime()、gmtime() 並返回 struct_time 物件。

import time
print(' Current local time:', time.ctime())
t = time.localtime()
print('Day of month:', t.tm_mday)
print('Day of week :', t.tm_wday)
print('Day of year :', t.tm_yday)

輸出

Current local time: Fri Mar 22 20:10:25 2019
Day of month: 22
Day of week : 4
Day of year : 81

6. time.strftime() 函式

此函式在第二個引數中採用元組或 struct_time,並根據第一個引數中指定的格式將其轉換為字串。

語法

time.strftime()

以下是實現 time.strftime() 函式的程式 -

import time
now = time.localtime(time.time())
print("Current date time is: ",time.asctime(now))
print(time.strftime("%y/%m/%d %H:%M", now))
print(time.strftime("%a %b %d", now))
print(time.strftime("%c", now))
print(time.strftime("%I %p", now))
print(time.strftime("%Y-%m-%d %H:%M:%S %Z", now))

輸出

Current date time is: Fri Mar 22 20:13:43 2019
19/03/22 20:13
Fri Mar 22
Fri Mar 22 20:13:43 2019
08 PM
2019-03-22 20:13:43 India Standard Time

檢查 Python 中的時區

有兩個時間屬性可以提供時區資訊 -

1. time.timezone

它以 UTC 格式返回本地(非夏令時)時區的偏移量。

>>> time.timezone
-19800

2. time.tzname – 它返回一個包含本地非夏令時和夏令時區的元組。

>>> time.tzname
('India Standard Time', 'India Daylight Time')

更新於:2019年7月30日

14K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.