淺談python中處理時間的模塊

咱們知道,Python提供了三種時間函數,時間模塊 time、基本時間日期模塊 datetime 和日曆模塊 Calendar。python

 

1、time模塊函數

 

一、在time模塊中,能夠用三種表現形式來表示時間,分別是時間戳、格式化時間字符串和結構化時間:spa

1).時間戳,經過time.time()得到code

>>> time.time() 1545027641.4434128

2).格式化時間字符串,經過相似於time.strftime("%Y-%m-%d %H:%M:%S")這樣的表達式得到orm

>>> time.strftime("%Y-%m-%d %H:%M:%S") '2018-12-17 14:22:49'

下面是python中時間日期格式化符號:對象

%a 本地星期名稱的簡寫(如星期四爲Thu) %A 本地星期名稱的全稱(如星期四爲Thursday) %b 本地月份名稱的簡寫(如八月份爲agu) %B 本地月份名稱的全稱(如八月份爲august) %c 本地相應的日期表示和時間表示 %d    一個月中的第幾天(01 - 31%f    微秒(範圍0.999999%H    一天中的第幾個小時(24小時制,00 - 23%I    第幾個小時(12小時制,0 - 11%j    一年中的第幾天(001 - 366%m    月份(01 - 12%M    分鐘數(00 - 59%p 本地am或者pm的標識符 %S    秒數(00 - 59%U    一年中的星期數。(00 - 53星期天是一個星期的開始。)第一個星期天之 前的全部天數都放在第0周。 %w    一個星期中的第幾天(0 - 6,0是星期天) %W    和%U基本相同,不一樣的是%W以星期一爲一個星期的開始。 %x 本地相應的日期表示 %X 本地相應的時間表示 %y    兩位數的年份表示(00-99%Y    四位數的年份表示(000-9999%z 與UTC時間的間隔(若是是本地時間,返回空字符串) %Z 時區的名字(若是是本地時間,返回空字符串) %%    %自己

3).結構化時間,或者叫時間元祖,經過time.localtime()得到,返回一個時間元祖blog

>>> time.localtime() time.struct_time(tm_year=2018, tm_mon=12, tm_mday=17, tm_hour=14, tm_min=36, tm_ sec=24, tm_wday=0, tm_yday=351, tm_isdst=0)

其中:接口

tm_year(年)                  2018 tm_mon(月) 1 到 12 tm_mday(日) 1 到 31 tm_hour(時) 0 到 23 tm_min(分) 0 到 59 tm_sec(秒) 0 到 61 (60或61 是閏秒) tm_wday(weekday) 0到6 (0是週一) tm_yday(一年的第幾天) 1 到 366 tm_isdst(是不是夏令時) -1, 0, 1, -1是決定是否爲夏令時的標誌

 

2.關於time模塊的一些基本使用方法:進程

1)time.sleep(t)  其中參數 t 指推遲執行的秒數字符串

2)time.time() 返回當前時間的時間戳

import time def f(): time.sleep(3) t1 = time.time() f() t2 = time.time() print(t2-t1) #3.001171588897705

3)time.localtime([ sec ]) 格式化時間戳爲本地的時間。 若是sec參數未輸入,則以當前時間爲轉換標準。

4)time.gmtime([ sec ]) 格式化時間戳爲格林威治標轉時間。 若是sec參數未輸入,則以當前時間爲轉換標準。

5)time.strftime(format[, t]) 其中參數format表示格式化字符串,可選參數是一個struct_time對象

6)time.strptime(string[,format])  是time.strftime()方法的逆向操做

>>> time.strftime("%Y-%m-%d %H:%M:%S") '2018-12-17 15:17:17'
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime()) '2018-12-17 07:17:42'
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(1545030000)) '2018-12-17 15:00:00'
time.strptime('2018-12-17', '%Y-%m-%d') time.struct_time(tm_year=2018, tm_mon=12, tm_mday=17, tm_hour=0, tm_min 0, tm_sec=0, tm_wday=0, tm_yday=351, tm_isdst=-1)

 

7)time.asctime([t]) 接受時間元組並返回一個可讀的形式爲"Tue Dec 11 18:07:14 2018"的字符串,其中 t -- 9個元素的元組或者經過函數 gmtime() 或 localtime() 返回的時間值。沒有傳參則默認爲time.localtime()。

>>> time.asctime() 'Mon Dec 17 15:20:51 2018'
>>> time.asctime(time.localtime()) 'Mon Dec 17 15:21:03 2018'
>>> time.asctime(time.localtime(1545030000)) 'Mon Dec 17 15:00:00 2018'
>>> time.asctime(time.gmtime()) 'Mon Dec 17 07:21:36 2018'
>>> time.asctime((2018, 12, 17, 15, 0, 0, 0, 351, 0)) 'Mon Dec 17 15:00:00 2018'

8)time.ctime([ sec ]) 把一個時間戳(按秒計算的浮點數)轉化爲time.asctime()的形式。 若是參數未給或者爲None的時候,將會默認time.time()爲參數。它的做用至關於 asctime(localtime(secs))。沒有返回值。

>>> time.ctime() 'Mon Dec 17 15:29:33 2018'
>>> time.ctime(1545030000) 'Mon Dec 17 15:00:00 2018'

9)time.clock() 用以浮點數計算的秒數返回當前的CPU時間。用來衡量不一樣程序的耗時。這個須要注意,在不一樣的系統上含義不一樣。在UNIX系統上,它返回的是"進程時間",它是用秒錶示的浮點數(時間戳)。而在WINDOWS中,第一次調用,返回的是進程運行的實際時間。而第二次以後的調用是自第一次調用之後到如今的運行時間。

import time def procedure(): time.sleep(3) t = time.clock() procedure() print (time.clock() - t, "seconds process time") #2.999830285124656 seconds process time

10)time.mktime(t) 執行與gmtime(), localtime()相反的操做,它接收struct_time對象做爲參數,返回用秒數來表示時間的浮點數。若是輸入的值不是一個合法的時間,將觸發 OverflowError 或 ValueError。其中 t -- 結構化的時間或者完整的9位元組元素。

 time.mktime(time.localtime()) 1545033603.0 time.mktime((2018, 12, 17, 16, 0, 0, 0, 351, 0)) 1545033600.0

 

二)datatime模塊

datatime模塊從新封裝了time模塊,提供更多接口,提供的類有:time,date,datetime,timedelta,tzinfo。

1.time類

time類表示時間值,屬性有hour minute  second microsecond tzinfo

>>> import datetime >>> t = datetime.time(11, 12, 13) >>> print(t) 11:12:13
>>> print(t.hour) 11
>>> print(t.minute) 12
>>> print(t.second) 13
>>> print(t.microsecond) 0 >>> print(t.tzinfo) None

 

2.date類

日期值用date 類表示。實例具備屬性year,month和 day。

>>> today = datetime.date.today() >>> print(today) 2018-12-17
>>> today.year 2018
>>> today.month 12
>>> today.day 17
>>> t = today.timetuple() >>> print(t) time.struct_time(tm_year=2018, tm_mon=12, tm_mday=17, tm_hour=0, tm_min=0, tm_se c=0, tm_wday=0, tm_yday=351, tm_isdst=-1)
>>> d1 = datetime.date(2018,10,1) >>> print(d1) 2018-10-01
>>> d2 = d1.replace(month=11) >>> print(d2) 2018-11-01

 

3.timedelta類

用於時間的加減

>>> print(datetime.timedelta(microseconds=1)) 0:00:00.000001
>>> print(datetime.timedelta(milliseconds=1)) 0:00:00.001000
>>> print(datetime.timedelta(seconds=1)) 0:00:01
>>> print(datetime.timedelta(minutes=1)) 0:01:00
>>> print(datetime.timedelta(hours=1)) 1:00:00
>>> print(datetime.timedelta(days=1)) 1 day, 0:00:00
>>> print(datetime.timedelta(weeks=1)) 7 days, 0:00:00
>>> today = datetime.date.today() >>> print(today) 2018-12-17
>>> one_day = datetime.timedelta(days=1) >>> print(one_day) 1 day, 0:00:00
>>> yesterday = today - one_day >>> print(yesterday) 2018-12-16
>>> tomorrow = today + one_day >>> print(tomorrow) 2018-12-18
>>> print(tomorrow - yesterday) 2 days, 0:00:00
>>> print(yesterday - tomorrow) -2 days, 0:00:00

 

4.datetimetime類

datetime至關於date和time結合起來。其屬性有year, month, day, hour , minute , second , microsecond , tzinfo

>>> today = datetime.datetime.today() >>> print(today) 2018-12-17 21:43:12.993074
>>> t = today.strftime("%a %b %d %H:%M:%S %Y") >>> print(t) Mon Dec 17 21:43:12 2018

 

5.tzinfo類

tzinfo類表示時區,但因爲是抽象類,不能直接實現

 

3、calendar

calendar模塊有很普遍的方法用來處理年曆和月曆

>>> import calendar >>> cal = calendar.month(2018, 12) >>> print(cal) December 2018 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

基本方法:

#設置每週以週三開始算
>>> calendar.setfirstweekday(2)
#判斷是不是閏年
>>> print(calendar.isleap(2018))
False
#返回某月的第一天是星期幾和這個月的天數 >>> print(calendar.monthrange(2018, 12))
(5, 31)
#返回某月每一週的列表集合 >>> print(calendar.monthcalendar(2018, 12))
[[0, 0, 0, 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, 0]]
>>> calendar.setfirstweekday(0)
>>> print(calendar.monthcalendar(2018, 12))
[[0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0, 0]]
相關文章
相關標籤/搜索