在日常的代碼中,咱們常常要與時間打交道。在python中,與時間處理有關的模塊就包括:time和datetime,下面分別來介紹:
在開始以前,首先要說明有如下幾種方式來表示時間:python
- 1.時間戳
- 2.格式化的時間字符串(時間對象)
- 3.元組(struct_time)共九個元素,因爲python的time模塊實現主要調用C庫,因此各個平臺可能不一樣
幾個定義
UTC(Coordinated Universal Time,世界協調時)亦即格林威治天文時間,世界標準時間。在中國爲UTC+8。DST(Daylight Saving Time)即夏令時。函數
時間戳(timestamp)的方式:一般來講,時間戳表示的是從1970年1月1日00:00:00開始按秒計算的偏移量,咱們運行type(time.time())
,返回的就是float類型。線程
元組(tuple_time)方式:struct_time元組共有9個元素,返回 struct_time的函數主要有gmtime()
,localtime()
,strptime()
,下面列出這種方式元組中的幾個元素:
| 索引(Index) | 屬性Attribute | 值(Values) |
| :--------: | :-----: | :----: |
| 0 | tm_year(年) | 好比2011 |
| 1 | tm_mon(月 | 1 - 12 |
| 2 | tm_day(日) | 1-31 |
| 3 | tm_hour(時) | 0-23 |
| 4 | tm_min(分) | 0-59 |
| 5 | tm_sec(秒) | 0-59 |
| 6 | tm_wday(星期) | 0 - 6(0表示週日) |
| 7 | tm_yday(一年中的第幾天) | 1-366 |
| 8 | tm_isdst(是不是夏令時) | 默認爲1 |code
time.localtime([sesc])
:將一個時間戳轉換爲當前時區的struct_time。sesc參數未提供,則以當前時間爲準orm
In [2]: import time In [3]: time.localtime() Out[3]: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=11, tm_min=28, tm_sec=4, tm_wday=2, tm_yday=129, tm_isdst=0)
time.gmtime([sesc])
:與time.localtime()
方法相似,gmtime()
方法是將一個時間戳轉換成爲UTC時區的struct_time對象
In [5]: time.gmtime(1012931) # 裏面跟的是時間戳,若是不跟的話就和localtime()方法同樣了 Out[5]: time.struct_time(tm_year=1970, tm_mon=1, tm_mday=12, tm_hour=17, tm_min=22, tm_sec=11, tm_wday=0, tm_yday=12, tm_isdst=0)
time.time()
:返回當前時間的時間戳索引
In [6]: time.time() Out[6]: 1525836757.4961157 # 那麼當咱們拿到這個時間戳,就能夠經過time.gmtime()方法轉換成爲時間對象了 In [7]: time.gmtime(1525836757.4961157) Out[7]: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=3, tm_min=32, tm_sec=37, tm_wday=2, tm_yday=129, tm_isdst=0)
time.sleep([sesc])
:線程推遲指定的時間運行。單位爲秒接口
In [8]: time.sleep(2)
time.asctime([t])
:把一個表示時間的元組或者時間對象(struct_time)表示爲這種形式:Wed May 9 11:42:49 2018字符串
In [11]: time.asctime() Out[11]: 'Wed May 9 11:42:49 2018'
time.ctime([sesc])
:把一個時間戳(按秒計算的浮點數)轉換爲time.asctime()的形式form
In [15]: time.ctime(13131313131) # 支持自定義輸入時間戳 Out[15]: 'Wed Feb 12 02:58:51 2386' # 若是沒有給參數或者爲None的話,默認參數就是time.time()。它的做用至關因而time.asctime(time.localtime()) In [14]: time.ctime() Out[14]: 'Wed May 9 11:45:48 2018' # 它的做用至關因而time.asctime(time.localtime()) In [16]: time.asctime(time.localtime()) Out[16]: 'Wed May 9 11:49:09 2018'
time.srtftime(format,[,t])
:把一個表明時間的元組或者時間對象(struct_time)(好比由time.localtime()和time.gmtime()返回的)轉化爲格式化的時間字符串,若是t沒有指定,將傳入time.localtime()
# t沒有指定 In [17]: time.strftime("%Y-%m-%d %H:%M:%S") Out[17]: '2018-05-09 11:56:02' # t指定 In [3]: time.localtime() Out[3]: time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=11, tm_min=57, tm_sec=50, tm_wday=2, tm_yday=129, tm_isdst=0) In [4]: a = time.localtime() In [5]: time.strftime("%Y-%m-%d",a) Out[5]: '2018-05-09'
time.strptime()
:把一個格式化的時間字符串轉化爲時間對象(struct_time)。實際上和strftime()是逆操做
In [8]: a = time.localtime() In [9]: time.strftime("%Y-%m-%d %H:%M:%S",a) Out[9]: '2018-05-09 12:01:22' In [10]: s = time.strftime("%Y-%m-%d %H:%M:%S",a) In [11]: s2 = time.strptime(s,"%Y-%m-%d %H:%M:%S") In [12]: print(s2) time.struct_time(tm_year=2018, tm_mon=5, tm_mday=9, tm_hour=12, tm_min=1, tm_sec=22, tm_wday=2, tm_yday=129, tm_isdst=-1)
相比於time模塊,datetime模塊的接口則更直觀、更容易調用
datetime模塊定義瞭如下幾個類
- datetime.date():表示日期的類。經常使用的屬性是year,month,day;
- datetime.time():表示時間的類。經常使用的屬性是hour,minute,second,microsecond;
- datetime.datetime():表示日期時間;
- datetime.timedelta():表示時間間隔,即兩個時間點之間的長度;
咱們須要記住如下幾個方法
1.d=datetime.datetime.now()
返回當前的datetime日期類型
In [1]: import datetime # 導入datetime模塊 In [2]: datetime.datetime.now() # 返回當前datetime日期類型 Out[2]: datetime.datetime(2018, 5, 9, 12, 7, 53, 162336) # 可是若是咱們用變量保存的話 In [3]: d = datetime.datetime.now() In [5]: d.year Out[5]: 2018 In [9]: d.month Out[9]: 5 In [10]: d.day Out[10]: 9
2.datetime.date.fromtimestamp()
:把一個時間戳轉換成datetime日期類型
In [11]: datetime.date.fromtimestamp(1111111) Out[11]: datetime.date(1970, 1, 14) # 可是卻沒有時分秒了
3.時間運算
In [5]: datetime.datetime.now() Out[5]: datetime.datetime(2018, 5, 9, 12, 16, 50, 61482) # 2018年5月9日12:16 In [8]: datetime.datetime.now() + datetime.timedelta(days=1) # 加一天 Out[8]: datetime.datetime(2018, 5, 10, 12, 17, 29, 495152) 2018年5月10日12:17
4.時間替換
In [9]: d = datetime.datetime.now() In [10]: d.replace(year=1027,month=11,day=30) # 想回到1027年的11月30日 Out[10]: datetime.datetime(1027, 11, 30, 12, 18, 48, 62154) #