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