Python 3 教程
Python 程序能用很多方式處理日期和時(shí)間,轉(zhuǎn)換日期格式是一個(gè)常見(jiàn)的功能。
Python 提供了一個(gè) time 和 calendar 模塊可以用于格式化日期和時(shí)間。
時(shí)間間隔是以秒為單位的浮點(diǎn)小數(shù)。
每個(gè)時(shí)間戳都以自從 1970 年 1 月 1 日午夜(歷元)經(jīng)過(guò)了多長(zhǎng)時(shí)間來(lái)表示。
Python 的 time 模塊下有很多函數(shù)可以轉(zhuǎn)換常見(jiàn)日期格式。如函數(shù) time.time() 用于獲取當(dāng)前時(shí)間戳, 如下實(shí)例:
以上實(shí)例輸出結(jié)果:
當(dāng)前時(shí)間戳為: 1459996086.7115328
時(shí)間戳單位最適于做日期運(yùn)算。但是1970年之前的日期就無(wú)法以此表示了。太遙遠(yuǎn)的日期也不行,UNIX和Windows只支持到2038年。
很多Python函數(shù)用一個(gè)元組裝起來(lái)的9組數(shù)字處理時(shí)間:
序號(hào) | 字段 | 值 |
---|---|---|
0 | 4位數(shù)年 | 2008 |
1 | 月 | 1 到 12 |
2 | 日 | 1到31 |
3 | 小時(shí) | 0到23 |
4 | 分鐘 | 0到59 |
5 | 秒 | 0到61 (60或61 是閏秒) |
6 | 一周的第幾日 | 0到6 (0是周一) |
7 | 一年的第幾日 | 1到366 (儒略歷) |
8 | 夏令時(shí) | -1, 0, 1, -1是決定是否為夏令時(shí)的旗幟 |
上述也就是struct_time元組。這種結(jié)構(gòu)具有如下屬性:
序號(hào) | 屬性 | 值 |
---|---|---|
0 | tm_year | 2008 |
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 (60或61 是閏秒) |
6 | tm_wday | 0到6 (0是周一) |
7 | tm_yday | 一年中的第幾天,1 到 366 |
8 | tm_isdst | 是否為夏令時(shí),值有:1(夏令時(shí))、0(不是夏令時(shí))、-1(未知),默認(rèn) -1 |
從返回浮點(diǎn)數(shù)的時(shí)間戳方式向時(shí)間元組轉(zhuǎn)換,只要將浮點(diǎn)數(shù)傳遞給如localtime之類(lèi)的函數(shù)。
#!/usr/bin/python3 import time localtime = time.localtime(time.time()) print ("本地時(shí)間為 :", localtime)
以上實(shí)例輸出結(jié)果:
本地時(shí)間為 : time.struct_time(tm_year=2016, tm_mon=4, tm_mday=7, tm_hour=10, tm_min=28, tm_sec=49, tm_wday=3, tm_yday=98, tm_isdst=0)
你可以根據(jù)需求選取各種格式,但是最簡(jiǎn)單的獲取可讀的時(shí)間模式的函數(shù)是asctime():
#!/usr/bin/python3 import time localtime = time.asctime( time.localtime(time.time()) ) print ("本地時(shí)間為 :", localtime)
以上實(shí)例輸出結(jié)果:
本地時(shí)間為 : Thu Apr 7 10:29:13 2016
我們可以使用 time 模塊的 strftime 方法來(lái)格式化日期,:
time.strftime(format[, t])
#!/usr/bin/python3 import time # 格式化成2016-03-20 11:45:39形式 print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) # 格式化成Sat Mar 28 22:24:24 2016形式 print (time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())) # 將格式字符串轉(zhuǎn)換為時(shí)間戳 a = "Sat Mar 28 22:24:24 2016" print (time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y")))
以上實(shí)例輸出結(jié)果:
2016-04-07 10:29:46 Thu Apr 07 10:29:46 2016 1459175064.0
python中時(shí)間日期格式化符號(hào):
Calendar模塊有很廣泛的方法用來(lái)處理年歷和月歷,例如打印某月的月歷:
#!/usr/bin/python3 import calendar cal = calendar.month(2016, 1) print ("以下輸出2016年1月份的日歷:") print (cal)
以上實(shí)例輸出結(jié)果:
以下輸出2016年1月份的日歷: January 2016 Mo Tu We Th Fr Sa Su 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
Time 模塊包含了以下內(nèi)置函數(shù),既有時(shí)間處理的,也有轉(zhuǎn)換時(shí)間格式的:
序號(hào) | 函數(shù)及描述 | 實(shí)例 |
---|---|---|
1 | time.altzone 返回格林威治西部的夏令時(shí)地區(qū)的偏移秒數(shù)。如果該地區(qū)在格林威治東部會(huì)返回負(fù)值(如西歐,包括英國(guó))。對(duì)夏令時(shí)啟用地區(qū)才能使用。 |
以下實(shí)例展示了 altzone()函數(shù)的使用方法: >>> import time >>> print ("time.altzone %d " % time.altzone) time.altzone -28800 |
2 | time.asctime([tupletime]) 接受時(shí)間元組并返回一個(gè)可讀的形式為"Tue Dec 11 18:07:14 2008"(2008年12月11日?周二18時(shí)07分14秒)的24個(gè)字符的字符串。 |
以下實(shí)例展示了 asctime()函數(shù)的使用方法: >>> import time >>> t = time.localtime() >>> print ("time.asctime(t): %s " % time.asctime(t)) time.asctime(t): Thu Apr 7 10:36:20 2016 |
3 | time.clock() 用以浮點(diǎn)數(shù)計(jì)算的秒數(shù)返回當(dāng)前的CPU時(shí)間。用來(lái)衡量不同程序的耗時(shí),比time.time()更有用。 |
由于該方法依賴(lài)操作系統(tǒng),在 Python 3.3 以后不被推薦,而在 3.8 版本中被移除,需使用下列兩個(gè)函數(shù)替代。 time.perf_counter() # 返回系統(tǒng)運(yùn)行時(shí)間 time.process_time() # 返回進(jìn)程運(yùn)行時(shí)間 |
4 | time.ctime([secs]) 作用相當(dāng)于asctime(localtime(secs)),未給參數(shù)相當(dāng)于asctime() |
以下實(shí)例展示了 ctime()函數(shù)的使用方法: >>> import time >>> print ("time.ctime() : %s" % time.ctime()) time.ctime() : Thu Apr 7 10:51:58 2016 |
5 | time.gmtime([secs]) 接收時(shí)間戳(1970紀(jì)元后經(jīng)過(guò)的浮點(diǎn)秒數(shù))并返回格林威治天文時(shí)間下的時(shí)間元組t。注:t.tm_isdst始終為0 |
以下實(shí)例展示了 gmtime()函數(shù)的使用方法: >>> import time >>> print ("gmtime :", time.gmtime(1455508609.34375)) gmtime : time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=3, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0) |
6 | time.localtime([secs] 接收時(shí)間戳(1970紀(jì)元后經(jīng)過(guò)的浮點(diǎn)秒數(shù))并返回當(dāng)?shù)貢r(shí)間下的時(shí)間元組t(t.tm_isdst可取0或1,取決于當(dāng)?shù)禺?dāng)時(shí)是不是夏令時(shí))。 |
以下實(shí)例展示了 localtime()函數(shù)的使用方法: >>> import time >>> print ("localtime(): ", time.localtime(1455508609.34375)) localtime(): time.struct_time(tm_year=2016, tm_mon=2, tm_mday=15, tm_hour=11, tm_min=56, tm_sec=49, tm_wday=0, tm_yday=46, tm_isdst=0) |
7 | time.mktime(tupletime) 接受時(shí)間元組并返回時(shí)間戳(1970紀(jì)元后經(jīng)過(guò)的浮點(diǎn)秒數(shù))。 | 實(shí)例 |
8 | time.sleep(secs) 推遲調(diào)用線程的運(yùn)行,secs指秒數(shù)。 |
以下實(shí)例展示了 sleep()函數(shù)的使用方法: #!/usr/bin/python3 import time print ("Start : %s" % time.ctime()) time.sleep( 5 ) print ("End : %s" % time.ctime()) |
9 | time.strftime(fmt[,tupletime]) 接收以時(shí)間元組,并返回以可讀字符串表示的當(dāng)?shù)貢r(shí)間,格式由fmt決定。 |
以下實(shí)例展示了 strftime()函數(shù)的使用方法: >>> import time >>> print (time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())) 2016-04-07 11:18:05 |
10 | time.strptime(str,fmt='%a %b %d %H:%M:%S %Y') 根據(jù)fmt的格式把一個(gè)時(shí)間字符串解析為時(shí)間元組。 |
以下實(shí)例展示了 strptime()函數(shù)的使用方法: >>> import time >>> struct_time = time.strptime("30 Nov 00", "%d %b %y") >>> print ("返回元組: ", struct_time) 返回元組: time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1) |
11 | time.time( ) 返回當(dāng)前時(shí)間的時(shí)間戳(1970紀(jì)元后經(jīng)過(guò)的浮點(diǎn)秒數(shù))。 |
以下實(shí)例展示了 time()函數(shù)的使用方法: >>> import time >>> print(time.time()) 1459999336.1963577 |
12 | time.tzset() 根據(jù)環(huán)境變量TZ重新初始化時(shí)間相關(guān)設(shè)置。 | 實(shí)例 |
13 |
time.perf_counter() 返回計(jì)時(shí)器的精準(zhǔn)時(shí)間(系統(tǒng)的運(yùn)行時(shí)間),包含整個(gè)系統(tǒng)的睡眠時(shí)間。由于返回值的基準(zhǔn)點(diǎn)是未定義的,所以,只有連續(xù)調(diào)用的結(jié)果之間的差才是有效的。 | 實(shí)例 |
14 |
time.process_time() 返回當(dāng)前進(jìn)程執(zhí)行 CPU 的時(shí)間總和,不包含睡眠時(shí)間。由于返回值的基準(zhǔn)點(diǎn)是未定義的,所以,只有連續(xù)調(diào)用的結(jié)果之間的差才是有效的。 | ? |
Time模塊包含了以下2個(gè)非常重要的屬性:
序號(hào) | 屬性及描述 |
---|---|
1 | time.timezone 屬性time.timezone是當(dāng)?shù)貢r(shí)區(qū)(未啟動(dòng)夏令時(shí))距離格林威治的偏移秒數(shù)(>0,美洲;<=0大部分歐洲,亞洲,非洲)。 |
2 | time.tzname 屬性time.tzname包含一對(duì)根據(jù)情況的不同而不同的字符串,分別是帶夏令時(shí)的本地時(shí)區(qū)名稱(chēng),和不帶的。 |
此模塊的函數(shù)都是日歷相關(guān)的,例如打印某月的字符月歷。
星期一是默認(rèn)的每周第一天,星期天是默認(rèn)的最后一天。更改設(shè)置需調(diào)用calendar.setfirstweekday()函數(shù)。模塊包含了以下內(nèi)置函數(shù):
序號(hào) | 函數(shù)及描述 |
---|---|
1 | calendar.calendar(year,w=2,l=1,c=6) 返回一個(gè)多行字符串格式的year年年歷,3個(gè)月一行,間隔距離為c。 每日寬度間隔為w字符。每行長(zhǎng)度為21* W+18+2* C。l是每星期行數(shù)。 |
2 | calendar.firstweekday( ) 返回當(dāng)前每周起始日期的設(shè)置。默認(rèn)情況下,首次載入caendar模塊時(shí)返回0,即星期一。 |
3 | calendar.isleap(year) 是閏年返回 True,否則為 false。 >>> import calendar >>> print(calendar.isleap(2000)) True >>> print(calendar.isleap(1900)) False |
4 | calendar.leapdays(y1,y2) 返回在Y1,Y2兩年之間的閏年總數(shù)。 |
5 | calendar.month(year,month,w=2,l=1) 返回一個(gè)多行字符串格式的year年month月日歷,兩行標(biāo)題,一周一行。每日寬度間隔為w字符。每行的長(zhǎng)度為7* w+6。l是每星期的行數(shù)。 |
6 | calendar.monthcalendar(year,month) 返回一個(gè)整數(shù)的單層嵌套列表。每個(gè)子列表裝載代表一個(gè)星期的整數(shù)。Year年month月外的日期都設(shè)為0;范圍內(nèi)的日子都由該月第幾日表示,從1開(kāi)始。 |
7 | calendar.monthrange(year,month) 返回兩個(gè)整數(shù)。第一個(gè)是該月的星期幾,第二個(gè)是該月有幾天。星期幾是從0(星期一)到 6(星期日)。 >>> import calendar >>> calendar.monthrange(2014, 11) (5, 30) (5, 30)解釋?zhuān)? 表示 2014 年 11 月份的第一天是周六,30 表示 2014 年 11 月份總共有 30 天。 |
8 | calendar.prcal(year,w=2,l=1,c=6) 相當(dāng)于 print calendar.calendar(year,w,l,c). |
9 | calendar.prmonth(year,month,w=2,l=1) 相當(dāng)于 print calendar.calendar(year,w,l,c)。 |
10 | calendar.setfirstweekday(weekday) 設(shè)置每周的起始日期碼。0(星期一)到6(星期日)。 |
11 | calendar.timegm(tupletime) 和time.gmtime相反:接受一個(gè)時(shí)間元組形式,返回該時(shí)刻的時(shí)間戳(1970紀(jì)元后經(jīng)過(guò)的浮點(diǎn)秒數(shù))。 |
12 | calendar.weekday(year,month,day) 返回給定日期的日期碼。0(星期一)到6(星期日)。月份為 1(一月) 到 12(12月)。 |
在Python中,其他處理日期和時(shí)間的模塊還有: