time包基于C語言的庫函數(libraryfunctions)。Python的解釋器通常是用C編寫的,Python的一些函數也會直接調用C語言的庫函數。
importtime
print(time.time())#wallclocktime,unit:second
print(time.clock())#processorclocktime,unit:second
time.sleep()可以將程序置于休眠狀態,直到某時間間隔之后再喚醒程序,讓程序繼續運行。
importtime
print('start')
time.sleep(10)#sleepfor10seconds
print('wakeup')
當我們需要定時地查看程序運行狀態時,就可以利用該方法。
time包還定義了struct_time對象。該對象實際上是將掛鐘時間轉換為年、月、日、時、分、秒……等日期信息,存儲在該對象的各個屬性中(tm_year,tm_mon,tm_mday...)。下面方法可以將掛鐘時間轉換為struct_time對象:
st=time.gmtime()#返回struct_time格式的UTC時間
st=time.localtime()#返回struct_time格式的當地時間,當地時區根據系統環境決定。
s=time.mktime(st)#將struct_time格式轉換成wallclocktime
以上內容為大家介紹了Python中的time包,希望對大家有所幫助,如果想要了解更多Python相關知識,請關注IT培訓機構:千鋒教育。