新聞中心
你知道Python stuct_time 中模塊如何使用整理提供N多種嗎?以下的文章就是通過對Python stuct_time 中模塊如何操作時間的函數(shù)的相關(guān)代碼去體現(xiàn)其的實際應用方案,以下是文章的介紹。

一、Python stuct_time中模塊使用整理提供了各種操作時間的函數(shù)。一般有兩種表示的方式:
***種:以時間戳的方式(相對于1970.1.1 00:00:00 以秒計算的偏移量),時間戳是惟一
第二種:以數(shù)組的形式表示即(struct_time), 共有九個元素,分別表示,同一個時間戳的stuct_time會因為時區(qū)不同而不同。
時區(qū):夏令時區(qū)與UTC時區(qū)函數(shù)介紹:
- asctime()
- asctime([tuple]) -> string
將一個Python struct_time 轉(zhuǎn)換成字符串clock()該函數(shù)有兩個功能:在***次調(diào)用的時候,返回的是程序運行的實際時間;(返回整個程序的總運行時間值),以第二次之后的調(diào)用,返回的是自***次調(diào)用后,到這次調(diào)用的時間間隔。示例:
- 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)
線程推遲指定的時間運行,經(jīng)過測試,單位為秒
- ctime(seconds)
- ctime(seconds) -> string
將一個時間戳Python stuct_time(默認為當前時間)轉(zhuǎn)換成一個時間字符串.示例:
- 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個參數(shù)值)將一個時間戳轉(zhuǎn)換成一個UTC時區(qū)(0時區(qū))的struct_time,如果seconds參數(shù)未輸入,則以當前時間為轉(zhuǎn)換標準(數(shù)組類型的時間值)示例:
- if __name__ == '__main__':
- print time.gmtime()
時間元組輸出:
- (2010, 3, 4, 4, 58, 53, 3, 63, 0)
返回的就是一個數(shù)組類型的數(shù)據(jù)。其中的含義分別為:
- (tm_year, tm_mon, tm_day, tm_hour, tm_min,tm_sec, tm_wday, tm_yday, tm_isdst)
得到一個數(shù)組之后就可以這樣分割:print time.gmtime()[0] 得到***個元素值
【編輯推薦】
- Python socket編程在具體應用中前兩個步驟的介紹
- 用python pylint檢查相關(guān)東西的操作方案詳解
- Python入門的相對路徑和絕對路徑詳解
- Python環(huán)境的實際應用方案介紹與代碼詳解
- Python數(shù)據(jù)結(jié)構(gòu)創(chuàng)建的具體應用方案詳解
標題名稱:Pythonstuct_time中模塊如何操作時間函數(shù)的方案
文章起源:http://www.5511xx.com/article/dpdgodi.html


咨詢
建站咨詢
