python時間time模塊處理大全
前言
在平常的代碼中,我們常常需要與時間打交道。在Python中,與時間處理有關(guān)的模塊就包括:time,datetime以及calendar。這篇文章,主要講解time模塊。
在開始之前,首先要說明這幾點:
在Python中,通常有這幾種方式來表示時間:時間戳 (給機器看的)、格式化的時間字符串(給人看的) 、struct_time元組機構(gòu)化時間(計算用的)。 UTC(Coordinated Universal Time,世界協(xié)調(diào)時)亦即格林威治天文時間,世界標準時間。在中國為UTC+8。DST(Daylight Saving Time)即夏令時。 時間戳(timestamp)的方式:通常來說,時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量。我們運行“type(time.time())”,返回的是float類型。返回時間戳方式的函數(shù)主要有time(),clock()等。 元組(struct_time)方式:struct_time元組共有9個元素,返回struct_time的函數(shù)主要有g(shù)mtime(),localtime(),strptime()。時間格式(時間戳)
time.time()函數(shù)獲取的是Unix時間戳。
返回的是一個float類型的數(shù)值。
import timet1 = time.time()print(t1, type(t1))
睡眠 time.sleep
time.sleep(2) # 睡眠2秒
格式化時間
import timeprint(time.strftime(’%Y-%m-%d %H:%M:%S’))2019-11-27 20:49:36%yprint(time.strftime(’%y-%m-%d %H:%M:%S’))19-11-27 20:50:27%c print(time.strftime(’%c’))Wed Nov 27 20:51:20 2019 格式 含義 備注 %a 本地(locale)簡化星期名稱 %A 本地完整星期名稱 %b 本地簡化月份名稱 %B 本地完整月份名稱 %c 本地相應(yīng)的日期和時間表示 %d 一個月中的第幾天(01 - 31) %H 一天中的第幾個小時(24小時制,00 - 23) %I 第幾個小時(12小時制,01 - 12) %j 一年中的第幾天(001 - 366) %m 月份(01 - 12) %M 分鐘數(shù)(00 - 59) %p 本地am或者pm的相應(yīng)符 一 %S 秒(01 - 61) 二 %U 一年中的星期數(shù)。(00 - 53星期天是一個星期的開始。)第一個星期天之前的所有天數(shù)都放在第0周。 三 %w 一個星期中的第幾天(0 - 6,0是星期天) 三 %W 和%U基本相同,不同的是%W以星期一為一個星期的開始。 %x 本地相應(yīng)日期 %X 本地相應(yīng)時間 %y 去掉世紀的年份(00 - 99) %Y 完整的年份 %Z 時區(qū)的名字(如果不存在為空字符) %% ‘%’字符 “%p”只有與“%I”配合使用才有效果。 文檔中強調(diào)確實是0 - 61,而不是59,閏年秒占兩秒(汗一個)。 當使用strptime()函數(shù)時,只有當在這年中的周數(shù)和天數(shù)被確定的時候%U和%W才會被計算。
結(jié)構(gòu)化時間(struct_time)
#結(jié)構(gòu)化時間#localtimestruct_time = time.localtime()print(struct_time) #time.struct_time(tm_year=2020, tm_mon=10, tm_mday=8, tm_hour=15, tm_min=7, tm_sec=53, tm_wday=0, tm_yday=281, tm_isdst=0)print(struct_time.tm_year) #2018#gmtimestruct_time1 = time.gmtime()print(struct_time1) #time.struct_time(tm_year=2020, tm_mon=10, tm_mday=8, tm_hour=15, tm_min=7, tm_sec=53, tm_wday=0, tm_yday=281, tm_isdst=0)print(struct_time1.tm_year) #2020
索引(Index) 屬性(Attribute) 值(Values) 0 tm_year(年) 比如2011 1 tm_mon(月) 1 - 12 2 tm_mday(日) 1 - 31 3 tm_hour(時) 0 - 23 4 tm_min(分) 0 - 59 5 tm_sec(秒) 0 - 61 6 tm_wday(weekday) 0 - 6(0表示周日) 7 tm_yday(一年中的第幾天) 1 - 366 8 tm_isdst(是否是夏令時) 默認為-1
轉(zhuǎn)換
它們之間的轉(zhuǎn)化如圖所示:
要了解更多,請參考time模塊的官方文檔 http://docs.python.org/library/time.html。
結(jié)構(gòu)化時間轉(zhuǎn)時間戳mktime
t = time.time()print(t) #1538982828.2859974print(time.localtime(t)) #time.struct_time(tm_year=2018, tm_mon=10, tm_mday=8, tm_hour=15, tm_min=13, tm_sec=3, tm_wday=0, tm_yday=281, tm_isdst=0)print(time.gmtime(t)) #time.struct_time(tm_year=2018, tm_mon=10, tm_mday=8, tm_hour=7, tm_min=13, tm_sec=3, tm_wday=0, tm_yday=281, tm_isdst=0)print(time.mktime(time.localtime())) #1538982958.0
結(jié)構(gòu)化時間轉(zhuǎn)字符串strftime
print(time.strftime(’%m/%d/%y %H:%M:%S’,time.localtime(3000000000))) #01/24/65 13:20:00
字符串轉(zhuǎn)結(jié)構(gòu)化時間strptime
t = time.time()t = time.strptime(’2000-12.31’,’%Y-%m.%d’)print(t) #time.struct_time(tm_year=2000, tm_mon=12, tm_mday=31, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=366, tm_isdst=-1)print(time.strftime(’%m/%d/%y %H:%M:%S’,time.localtime(3000000000))) #01/24/65 13:20:00
總結(jié)
asctime([tuple])
將時間元組(默認為本地時間)格式轉(zhuǎn)換為字符串形式。接受一個時間元組,其默認值為localtime()返回值
clock()
返回當前程序的cpu執(zhí)行時間。unix系統(tǒng)始終返回全部運行時間;而windows從第二次開始都是以第一次調(diào)用此函數(shù)時的時間戳作為基準,而不是程序開始時間為基準。不接受參數(shù)。
ctime(seconds)
將時間戳轉(zhuǎn)換為字符串。接受一個時間戳,其默認值為當前時間戳。等價于asctime(localtime(seconds))
print(time.asctime()) # Mon Oct 8 15:21:02 2018print(time.asctime(time.localtime())) # Mon Oct 8 15:21:02 2018print(time.asctime(time.localtime(2000000000))) # Wed May 18 11:33:20 2033print(time.ctime()) # Mon Oct 8 15:21:02 2018print(time.ctime(2000000000)) # Wed May 18 11:33:20 2033
gmtime([seconds])
將時間戳轉(zhuǎn)換為UTC時間元組格式。接受一個浮點型時間戳參數(shù),其默認值為當前時間戳。
localtime([seconds])
將時間戳轉(zhuǎn)換為本地時間元組格式。接受一個浮點型時間戳參數(shù),其默認值為當前時間戳。
mktime(tuple)
將本地時間元組轉(zhuǎn)換為時間戳。接受一個時間元組,必選。
sleep(seconds)
延遲一個時間段,接受整型、浮點型。
time()
返回當前時間戳,浮點數(shù)形式。不接受參數(shù)
tzset()
改變本地時區(qū)。
strptime(string, format)
將指定格式的時間字符串解析為時間元組,strftime()的逆向過程。接受字符串,時間格式2個參數(shù),都是必選。示例:
time.strptime(’2015-08-05 22:08:06’, ’%Y-%m-%d %X’)time.struct_time(tm_year=2015, tm_mon=8, tm_mday=5, tm_hour=22, tm_min=8, tm_sec=6
strftime(format[, tuple])
將時間元組以指定的格式轉(zhuǎn)換為字符串形式。接受字符串格式化串、時間元組。時間元組為可選,默認為localtime()。示例:
time.strftime('%Y-%m-%d %X', time.localtime())’2011-05-05 16:37:06’
好了,到此這篇關(guān)于python時間time模塊處理大全的文章就介紹到這了,更多相關(guān)python時間time模塊處理內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. IntelliJ IDEA設(shè)置自動提示功能快捷鍵的方法2. IntelliJ IDEA恢復(fù)刪除文件的方法3. CSS3中Transition屬性詳解以及示例分享4. 解決AJAX返回狀態(tài)200沒有調(diào)用success的問題5. jsp 實現(xiàn)的簡易mvc模式示例6. layui Ajax請求給下拉框賦值的實例7. 詳談ajax返回數(shù)據(jù)成功 卻進入error的方法8. IntelliJ IDEA 2020最新激活碼(親測有效,可激活至 2089 年)9. uniapp+.net core實現(xiàn)微信小程序獲取手機號功能10. 《CSS3實戰(zhàn)》筆記--漸變設(shè)計(二)
