python time模塊

Python---time模塊使用詳解

time.sleep() --- 使程序暫停數(shù)秒。
調(diào)用:time.sleep(seconds), 傳入秒數(shù), 程序運(yùn)行到這個(gè)函數(shù)時(shí)暫停一段時(shí)間。

time模塊

time模塊中時(shí)間表現(xiàn)的格式主要有三種:

  • a、timestamp時(shí)間戳,時(shí)間戳表示的是從1970年1月1日00:00:00開始按秒計(jì)算的偏移量
  • b、struct_time時(shí)間元組,共有九個(gè)元素組。
  • c、format time 格式化時(shí)間,已格式化的結(jié)構(gòu)使時(shí)間更具可讀性。包括自定義格式和固定格式。

時(shí)間格式轉(zhuǎn)換圖:

timestamp和struct_time之間的相互轉(zhuǎn)換:

  1. time.localtime()獲取當(dāng)?shù)貢r(shí)間即struct_time。
import time
print(time.localtime())
*************************************************************
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=22, tm_hour=14, tm_min=21, tm_sec=41, tm_wday=3, tm_yday=234, tm_isdst=0)
  1. time.gmtime(sec):函數(shù)將一個(gè)時(shí)間戳轉(zhuǎn)換為UTC時(shí)區(qū)(0時(shí)區(qū))的struct_time,可選的參數(shù)sec表示從1970-1-1以來的秒數(shù)。其默認(rèn)值為time.time(),函數(shù)返回time.struct_time類型的對象。(struct_time是在time模塊中定義的表示時(shí)間的對象)。
import time
s = time.gmtime(time.time())
print(s)
************************************************************************
time.struct_time(tm_year=2019, tm_mon=8, tm_mday=22, tm_hour=8, tm_min=44, tm_sec=26, tm_wday=3, tm_yday=234, tm_isdst=0)
  1. time.mktime(struct_time):返回時(shí)間戳
import time
s = time.mktime(time.localtime())
print(s)

時(shí)間元祖和格式化時(shí)間之間的轉(zhuǎn)換

  1. time.strftime():時(shí)間轉(zhuǎn)換字符串
  • time.strftime(format,dt)
    dt為具體時(shí)間,datetime和date格式均可,甚至可以為時(shí)間數(shù)組struct_time
  • dt.strftime(format)
    format可以取"%Y-%m-%d %H:%M:%S"的任意子串,來決定顯示年月日時(shí)分秒,dt默認(rèn)本地時(shí)間。
import time
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
time.sleep(2)
print(time.strftime('%Y-%m-%d %H:%M:%S'))
******************************************************************
2019-08-22 14:28:24
2019-08-22 14:28:26

那怎么把格式化時(shí)間轉(zhuǎn)換成struct_time時(shí)間呢?

  1. time.strptime():根據(jù)指定的格式把一個(gè)格式化時(shí)間轉(zhuǎn)換為時(shí)間元組。

time.strptime(string[, format])

  • string -- 這是其中將根據(jù)給定的格式解析字符串格式的時(shí)間。
  • format -- 這是將用于解析該給定的字符串的指令。
    格式參數(shù)使用相同的指令使用strftime();它默認(rèn)為“%a %b %d %H:%M:%S %Y”相匹配的ctime()所返回的格式。
import time
s = time.strptime(time.strftime('%M%m%d%H%M%S',time.localtime()),'%Y%m%d%H%M%S')
print(s)
*********************************************************************
time.struct_time(tm_year=1108, tm_mon=2, tm_mday=21, tm_hour=7, tm_min=11, tm_sec=5, tm_wday=4, tm_yday=52, tm_isdst=-1)

轉(zhuǎn)換成可讀形式的字符串時(shí)間:

  1. time asctime() 函數(shù)接受時(shí)間元組并返回一個(gè)可讀的形式為"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18時(shí)07分14秒)的24個(gè)字符的字符串。
  • time.asctime([t]))
    t -- 9個(gè)元素的元組或者通過函數(shù) gmtime() 或 localtime() 返回的時(shí)間值。
    返回值,返回一個(gè)可讀的形式為"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18時(shí)07分14秒)的24個(gè)字符的字符串。
import time
s = time.asctime(time.localtime())
print(s)
*************************************************************************
Thu Aug 22 17:21:26 2019
  1. time.ctime():函數(shù)接受時(shí)間戳并返回一個(gè)可讀的形式為"Tue Dec 11 18:07:14 2008"(2008年12月11日 周二18時(shí)07分14秒)的24個(gè)字符的字符串。
import time
s = time.ctime(time.time())
print(s)
***************************************************************************
Thu Aug 22 17:23:34 2019
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容