Python stuct_time中模塊如何操作時(shí)間函數(shù)的方案
你知道Python stuct_time 中模塊如何使用整理提供N多種嗎?以下的文章就是通過(guò)對(duì)Python stuct_time 中模塊如何操作時(shí)間的函數(shù)的相關(guān)代碼去體現(xiàn)其的實(shí)際應(yīng)用方案,以下是文章的介紹。
一、Python stuct_time中模塊使用整理提供了各種操作時(shí)間的函數(shù)。一般有兩種表示的方式:
***種:以時(shí)間戳的方式(相對(duì)于1970.1.1 00:00:00 以秒計(jì)算的偏移量),時(shí)間戳是惟一
第二種:以數(shù)組的形式表示即(struct_time), 共有九個(gè)元素,分別表示,同一個(gè)時(shí)間戳的stuct_time會(huì)因?yàn)闀r(shí)區(qū)不同而不同。
時(shí)區(qū):夏令時(shí)區(qū)與UTC時(shí)區(qū)函數(shù)介紹:
- asctime()
- asctime([tuple]) -> string
將一個(gè)Python struct_time 轉(zhuǎn)換成字符串clock()該函數(shù)有兩個(gè)功能:在***次調(diào)用的時(shí)候,返回的是程序運(yùn)行的實(shí)際時(shí)間;(返回整個(gè)程序的總運(yùn)行時(shí)間值),以第二次之后的調(diào)用,返回的是自***次調(diào)用后,到這次調(diào)用的時(shí)間間隔。示例:
- import time
- if __name__ == '__main__':
- time.sleep(1)
- print "clock1:%s" % time.clock()
- time.sleep(1)
- print "clock1:%s" % time.clock()
- time.sleep(1)
- print "clock1:%s" % time.clock()
輸出:
- clock1:3.07301626324e-006
- clock1:0.997576228264
- clock1:1.99513653272
- 1.1.3 sleep(seconds)
線程推遲指定的時(shí)間運(yùn)行,經(jīng)過(guò)測(cè)試,單位為秒
- ctime(seconds)
- ctime(seconds) -> string
將一個(gè)時(shí)間戳Python stuct_time(默認(rèn)為當(dāng)前時(shí)間)轉(zhuǎn)換成一個(gè)時(shí)間字符串.示例:
- if __name__ == '__main__':
- print time.ctime()
輸出:
- Thu Mar 04 12:55:03 2010
- 1.1.5 gmtime(...)
- gmtime([seconds]) -> (tm_year, tm_mon, tm_day,
tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)
9個(gè)參數(shù)值)將一個(gè)時(shí)間戳轉(zhuǎn)換成一個(gè)UTC時(shí)區(qū)(0時(shí)區(qū))的struct_time,如果seconds參數(shù)未輸入,則以當(dāng)前時(shí)間為轉(zhuǎn)換標(biāo)準(zhǔn)(數(shù)組類型的時(shí)間值)示例:
- if __name__ == '__main__':
- print time.gmtime()
時(shí)間元組輸出:
- (2010, 3, 4, 4, 58, 53, 3, 63, 0)
返回的就是一個(gè)數(shù)組類型的數(shù)據(jù)。其中的含義分別為:
- (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)
得到一個(gè)數(shù)組之后就可以這樣分割:print time.gmtime()[0] 得到***個(gè)元素值
【編輯推薦】
- Python socket編程在具體應(yīng)用中前兩個(gè)步驟的介紹
- 用python pylint檢查相關(guān)東西的操作方案詳解
- Python入門的相對(duì)路徑和絕對(duì)路徑詳解
- Python環(huán)境的實(shí)際應(yīng)用方案介紹與代碼詳解
- Python數(shù)據(jù)結(jié)構(gòu)創(chuàng)建的具體應(yīng)用方案詳解