Python常用模組-時間模組

來源:互聯網
上載者:User

標籤:暫停   strftime   ctime   時間轉換   本質   類比   案例   ber   定義   

                            Python常用模組-時間模組

                                         尹正傑

著作權聲明:原創作品,謝絕轉載!否則將追究法律責任。

 

 

 

一.初始time模組

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 import time 8  9 """"10 模組的分類:11     模組本質就是一個“*.py”檔案,大致分為以下三類:12         1>.內建模組,指的是存在Python解譯器內部的模組,如time模組;13         2>>第三方模組,指的是安裝好Python後的lib檔案夾中的模組;14         3>.自訂模組,指的是你自己寫的Python程式;15 """16 17 print(time.time())      #返回當前的時間戳記,表示從1971年1月1日"00:00:00"到此刻時間節點的秒數。18 19 s = time.localtime()    #建立一個時間對象,也可以說是在結構化時間對象,返回本地時間的struct_time對象格式。20 print(s)21 print(s.tm_year)        #擷取年份。22 print(s.tm_mon)         #擷取月份。23 24 s2 = time.gmtime()      #返回utc時間的struc時間對象格式。25 print(s2)26 27 28 29 30 #以上代碼執行結果如下:31 1520176127.924449732 time.struct_time(tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=23, tm_min=8, tm_sec=47, tm_wday=6, tm_yday=63, tm_isdst=0)33 201834 335 time.struct_time(tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=15, tm_min=8, tm_sec=47, tm_wday=6, tm_yday=63, tm_isdst=0)

二.時間模組的相互轉換

1.轉換助記圖

 

2.案例展示

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 import time 8  9 s = time.localtime(31245244545)                         #結構化時間對象,可以將時間戳記轉換成結構化時間。10 print(s)11 12 s2 = time.mktime(time.localtime())                      #將結構化時間轉換成時間戳記。13 print(s2)14 15 s3 = time.strftime("%Y-%m-%d",time.localtime())        #將結構化時間轉換成字串時間。16 print(s3)17 18 s4 = time.strptime("1993:05:19","%Y:%m:%d")         #將字串時間轉換成結構化時間。19 print(s4)20 21 22 23 #以上代碼執行結果如下:24 time.struct_time(tm_year=2960, tm_mon=2, tm_mday=15, tm_hour=2, tm_min=35, tm_sec=45, tm_wday=4, tm_yday=46, tm_isdst=0)25 1520176194.026 2018-03-0427 time.struct_time(tm_year=1993, tm_mon=5, tm_mday=19, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=139, tm_isdst=-1)

三.time擴充

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7  8 import time 9 print(time.asctime(time.localtime()))           #將結構化時間轉換成字串時間。10 print(time.ctime(565656446))                    #將時間戳記轉換成字串時間。11 12 time.sleep(2)                                   #讓程式暫停2秒鐘,用於類比I/O阻塞,並不佔用CPU資源。13 14 15 16 17 #以上代碼執行結果如下:18 Sun Mar  4 23:11:16 201819 Sat Dec  5 06:47:26 1987

四.time模組小試牛刀

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6  7 import time 8  9 s = "2018-03-04"10 11 def ChangeTime(string,number,format="%Y-%m-%d"):12     StructTime = time.strptime(string,format )13     print(StructTime)14     StampTime = time.mktime(StructTime)15     print(StampTime)16     NewStamp = StampTime + 3600 * 24 * number17     NewStringTime = time.strftime(format, time.localtime(NewStamp))18     print(NewStringTime)19 20 ChangeTime(s,3)     #推算3日後的時間21 22 23 24 25 #以上代碼執行結果如下:26 time.struct_time(tm_year=2018, tm_mon=3, tm_mday=4, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=6, tm_yday=63, tm_isdst=-1)27 1520092800.028 2018-03-07

五.datetime介紹

 1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:[email protected] 6 import time,datetime 7  8 print(datetime.datetime.now())                                  #列印當前系統時間 9 10 print(datetime.date.fromtimestamp(time.time()))                 #時間戳記直接轉成日期格式如:2018-03-0411 12 print(datetime.datetime.now() + datetime.timedelta(3))          #目前時間+3天13 14 print(datetime.datetime.now() + datetime.timedelta(-3))         #目前時間-3天15 16 print(datetime.datetime.now() + datetime.timedelta(hours=3))    #目前時間+3小時17 18 print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #目前時間+30分19 20 c_time  = datetime.datetime.now()21 print(c_time.replace(minute=3,hour=2))                          ##時間替換22 23 24 25 26 #以上代碼執行結果如下:27 2018-03-04 23:12:13.47741628 2018-03-0429 2018-03-07 23:12:13.47741630 2018-03-01 23:12:13.47741631 2018-03-05 02:12:13.47741632 2018-03-04 23:42:13.47741633 2018-03-04 02:03:13.477416

 

Python常用模組-時間模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.