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