python模組--時間模組

來源:互聯網
上載者:User

標籤:time   datetime   

python模組--時間模組


一、python中時間介紹:

   python中時間的表示形式有兩種:  

        1、時間戳記標記法,即以整型或浮點型表示的是一個以秒為單位的時間間隔。這個時間的基礎值是從1970年的1月1號零點開始算起。

        2、元組格式標記法,即一種Python的資料結構表示。這個元組有9個整型內容。分別表示不同的時間含義。



二、datetime模組


2.1 datetime模組的所有函數


In [2]: dir(datetime)Out[2]: [‘MAXYEAR‘, ‘MINYEAR‘, ‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘date‘, ‘datetime‘, ‘datetime_CAPI‘, ‘time‘, ‘timedelta‘, ‘tzinfo‘]In [3]:


2.2 datetime模組函數詳解


datetime.datetime.now()    擷取當前系統時間

In [9]: print datetime.datetime.now()2016-11-29 12:27:55.391272#datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)#一般用於資料庫跟時間有關欄位的賦值#In [29]: print datetime.datetime.now().strftime(‘%Y-%m-%d %H:%M:%S‘)2016-11-29 13:26:22


datetime.date.today()     擷取今天日期

In [13]: print datetime.date.today()2016-11-29


datetime.timedelta(days=x)  給定兩個時間差值

In [16]: help(datetime.timedelta)class timedelta(__builtin__.object) |  Difference between two datetime values. |   |  Methods defined here: |   |  __abs__(...) |      x.__abs__() <==> abs(x) |   |  __add__(...) |      x.__add__(y) <==> x+y |   |  __div__(...) |      x.__div__(y) <==> x/y |   |  __eq__(...) |      x.__eq__(y) <==> x==y |   |  __floordiv__(...) |      x.__floordiv__(y) <==> x//y |   |  __ge__(...) |      x.__ge__(y) <==> x>=y |   |  __getattribute__(...) |      x.__getattribute__(‘name‘) <==> x.name##小李子In [14]: print datetime.timedelta(days=1)1 day, 0:00:00


datetime.datetime.strptime()    將字串格式轉換成時間

##注意這個函數字串中串連符只能是‘-‘,其他則會報錯In [26]: print datetime.datetime.strptime("2016-12-20",‘%Y-%m-%d‘)2016-12-20 00:00:00In [25]: print datetime.datetime.strptime("2016 12 20",‘%Y-%m-%d‘)---------------------------------------------------------------------------ValueError                                Traceback (most recent call last)<ipython-input-25-aac02a333bd5> in <module>()----> 1 print datetime.datetime.strptime("2016 12 20",‘%Y-%m-%d‘)/usr/local/python2.7/lib/python2.7/_strptime.pyc in _strptime(data_string, format)    323     if not found:    324         raise ValueError("time data %r does not match format %r" %--> 325                          (data_string, format))    326     if len(data_string) != found.end():    327         raise ValueError("unconverted data remains: %s" %ValueError: time data ‘2016 12 20‘ does not match format ‘%Y-%m-%d‘In [27]: import timeIn [28]: time.strftime("%Y-%m-%d %H:%M:%S",time.localtime())Out[28]: ‘2016-11-29 13:18:21‘



三、time模組


3.1 time模組的所有函數

In [3]: dir(time)Out[3]: [‘__doc__‘, ‘__file__‘, ‘__name__‘, ‘__package__‘, ‘accept2dyear‘, ‘altzone‘, ‘asctime‘, ‘clock‘, ‘ctime‘, ‘daylight‘, ‘gmtime‘, ‘localtime‘, ‘mktime‘, ‘sleep‘, ‘strftime‘, ‘strptime‘, ‘struct_time‘, ‘time‘, ‘timezone‘, ‘tzname‘, ‘tzset‘]


3.2 time模組具體函數講解

    

    time.time()    -- 返回目前時間戳,浮點數形式。不接受參數  

In [30]: time.time()Out[30]: 1480398573.196829

    

time.sleep() -- 延遲一個時間段,接受整型、浮點型。

In [10]: time.sleep(3)#執行完成後睡眠3秒


time.localtime()        -- 將時間戳記轉換為本地時間元組格式。接受一個浮點型時間戳記參數,其預設值為目前時間戳。

In [11]: time.localtime()Out[11]: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=10, tm_hour=11, tm_min=49, tm_sec=26, tm_wday=5, tm_yday=345, tm_isdst=0)In [13]: time.localtime(1481341446.412076)Out[13]: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=10, tm_hour=11, tm_min=44, tm_sec=6, tm_wday=5, tm_yday=345, tm_isdst=0)


time.asctime()             -- 將時間元組格式轉換為字串形式。接受一個時間元組,其預設值為localtime()傳回值

In [14]: time.asctime()Out[14]: ‘Sat Dec 10 11:51:26 2016‘In [23]: time.asctime((2016,12,10,11,52,47,5,345,0))Out[23]: ‘Sat Dec 10 11:52:47 2016‘


time.strftime()             -- 將時間元組以指定的格式轉換為字串形式。接受字串格式化串、時間元組。時間元組為可選,預設為localtime()

In [25]: time.strftime("%Y   %H:%M:%S")Out[25]: ‘2016 11:59:55‘



 strptime() -- 將指定格式的時間字串解析為時間元組,strftime()的逆向過程。接受字串,時間格式2個參數,都是必選。

In [28]: time.strptime(‘Sat Dec 10 12:00:59 2016‘)Out[28]: time.struct_time(tm_year=2016, tm_mon=12, tm_mday=10, tm_hour=12, tm_min=0, tm_sec=59, tm_wday=5, tm_yday=345, tm_isdst=-1)


本文出自 “11931192” 部落格,轉載請與作者聯絡!

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.