Python與時間相關的time、datetime模組的使用

來源:互聯網
上載者:User

標籤:參數   port   turn   使用   結構   solution   本地   parse   完整   

一、前言

學習python處理時間相關的模組time、datetime

二、time模組

首先來看下time模組

通過help(time)來看一下time模組下都有哪些函數:

    time() -- return current time in seconds since the Epoch as a float    clock() -- return CPU time since process start as a float    sleep() -- delay for a number of seconds given as a float    gmtime() -- convert seconds since Epoch to UTC tuple    localtime() -- convert seconds since Epoch to local time tuple    asctime() -- convert time tuple to string    ctime() -- convert time in seconds to string    mktime() -- convert local time tuple to seconds since Epoch    strftime() -- convert time tuple to string according to format specification    strptime() -- parse string to time tuple according to format specification    tzset() -- change the local timezone

time表現方式有三種:

1、時間戳記(timestamp)的方式:時間戳記表示的是從1970年1月1日00:00:00開始按秒計算的位移量。返回的是float類型,返回時間戳記的函數有time()、clock()

2、元組(struct_time)方式:struct_time元組共有9個元素,返回struct_time的函數主要有gmtime(),localtime(),strptime()。下面列出這種方式元組中的幾個元素:

3、格式化字串(format time)方式:格式化時間,已格式化的結構使時間更具可讀性。包括自訂格式和固定格式。比如“2018-5-11”

下面就說一下常用的一些函數:

1、time.sleep()

time.sleep(secs):線程延遲指定的時間運行。單位為秒。

2、time.time()

擷取目前時間戳

>>>time.time()      1525954582.579378
3、time.clock()

計算cpu計算所執行的時間

>>>time.sleep(3)print(time.clock())0.061649
4、time.gmtime()
>>>a=time.gmtimeprint(a)time.struct_time(tm_year=2018, tm_mon=5, tm_mday=10, tm_hour=6, tm_min=53, tm_sec=30, tm_wday=3, tm_yday=130, tm_isdst=0)

gmtime()方法是將一個時間戳記轉換為UTC時區(0時區)的struct_time。,總共9個參數,其含義如上面那個表格圖所示

5、time.localtime()
>>>a=time.localtime()print(a)time.struct_time(tm_year=2018, tm_mon=5, tm_mday=11, tm_hour=9, tm_min=22, tm_sec=14, tm_wday=4, tm_yday=131, tm_isdst=0)

將一個時間戳記轉換為當前時區的struct_time。secs參數未提供,則以目前時間為準。,總共9個參數,其含義如上面那個表格圖所示

6、time.asctime([t]):

把一個表示時間的元組或者struct_time表示為這種形式:‘Sun Jun 20 23:21:05 1993‘。如果沒有參數,將會將time.localtime()作為參數傳入。

>>>time.asctime()‘Thu May 5 14:55:43 2011‘

將一個時間戳記轉換為當前時區的struct_time。secs參數未提供,則以目前時間為準。,總共9個參數,其含義如上面那個表格圖所示

7、time.ctime()
>>>time.ctime(3600)print(a)Thu Jan  1 09:00:00 1970

將時間戳記轉為這個形式的‘Sun Jun 20 23:21:05 1993‘格式化時間。如果沒有參數,將會將time.localtime()作為參數傳入。

8、time.strftime(format[, t])
>>>local_time = time.localtime()print(time.strftime(‘%Y-%m-%d  %H:%M:%S‘, local_time))2018-05-11  09:38:46

把一個代表時間的元組或者struct_time(如由time.localtime()和time.gmtime()返回)轉化為格式化的時間字串。如果t未指定,將傳入time.localtime()。如果元組中任何一個元素越界,ValueError的錯誤將會被拋出。

9、time.strptime(string[, format])
>>>a=time.localtime()print(a)time.struct_time(tm_year=2018, tm_mon=5, tm_mday=11, tm_hour=9, tm_min=22, tm_sec=14, tm_wday=4, tm_yday=131, tm_isdst=0)

把一個格式化時間字串轉化為struct_time。實際上它和strftime()是逆操作。在這個函數中,format預設為:"%a %b %d %H:%M:%S %Y"。

他們之間轉換關係如:

時間格式化:

%y 兩位元的年份表示(00-99)%Y 四位元的年份表示(000-9999)%m 月份(01-12)%d 月內中的一天(0-31)%H 24小時制小時數(0-23)%I 12小時制小時數(01-12) %M 分鐘數(00=59)%S 秒(00-59)%a 本地簡化星期名稱%A 本地完整星期名稱%b 本地簡化的月份名稱%B 本地完整的月份名稱%c 本地相應的日期表示和時間表示%j 年內的一天(001-366)%p 本地A.M.或P.M.的等價符%U 一年中的星期數(00-53)星期天為星期的開始%w 星期(0-6),星期天為星期的開始%W 一年中的星期數(00-53)星期一為星期的開始%x 本地相應的日期表示%X 本地相應的時間表示%Z 當前時區的名稱%% %號本身
三、datetime模組

datetime內建對象關係如下:

分為date/time/timedelta/tzinfo 四個類,其中date、tzinfo又分別有各自的子類datetim/timezone

1、datetime.datetime.now() 擷取目前時間字串
>>>datetime.datetime.now()2018-05-11 10:05:29.397237
2、datetime.datetime.now().timestamp() 擷取目前時間時間戳記
>>>datetime.datetime.now().timestamp()1526004460.797517
3、datetime.datetime.today() 擷取目前時間年月日
>>>datetime.datetime.today()2018-05-11
4、其他一些方法
In [42]: #設定一個時間對象In [43]: d=datetime(2016,7,21,22,23,15)In [44]: #自訂格式顯示In [45]: d.strftime(‘%x %X‘)Out[45]: ‘07/21/16 22:23:15‘In [46]: #顯示英文格式In [47]: d.ctime()Out[47]: ‘Thu Jul 21 22:23:15 2016‘In [48]: #顯示日曆(年,年中第幾周,周幾)In [49]: d.isocalendar()Out[49]: (2016, 29, 4)

datetime子模組單位時間間隔:datetime.resolution=1微秒。

date子模組的時間間隔為1天 date.resolution=1天

時間間隔乘以一個數,表示間隔幾天

>>>from datetime import date#現在時間是date.today()datetime.date(2018, 5, 11)#100天以前的日期是result=date.today()-date.resolution*100print(result)輸出結果:2018-01-31

Python與時間相關的time、datetime模組的使用

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.