Python 處理日期時(shí)間的Arrow庫使用
Python針對(duì)日期時(shí)間的處理提供了大量的package,類和方法,但在可用性上來看非常繁瑣和麻煩
第三方庫Arrow提供了一個(gè)合理的、人性化的方法來創(chuàng)建、操作、格式轉(zhuǎn)換的日期,時(shí)間,和時(shí)間戳,幫助我們使用較少的導(dǎo)入和更少的代碼來處理日期和時(shí)間。
$ pip install arrow
獲取當(dāng)前時(shí)間 arrow.utcnow(), arrow.now()
>>> arrow.utcnow() <Arrow [2018-02-24T13:15:29.981135+00:00]> >>> arrow.now() <Arrow [2018-02-24T21:15:50.841056+08:00]>
將時(shí)間戳轉(zhuǎn)化為arrow對(duì)象 arrow.get(timestamp)
>>> arrow.get(1519534533) <Arrow [2018-02-25T04:55:33+00:00]> >>> arrow.get('1519534533') <Arrow [2018-02-25T04:55:33+00:00]> >>> arrow.get(1519534533.153443) <Arrow [2018-02-25T04:55:33.153443+00:00]> >>> arrow.get('1519534533.153443') <Arrow [2018-02-25T04:55:33.153443+00:00]>
時(shí)間戳可以是int,float或者可以轉(zhuǎn)化為float的字符串
將字符串轉(zhuǎn)換為arrow對(duì)象 arrow.get(string[,format_string])
>>> arrow.get('2018-02-24 12:30:45', 'YYYY-MM-DD HH:mm:ss') <Arrow [2018-02-24T12:30:45+00:00]>
遵循ISO-8601的字符串不需要格式字符串參數(shù)即可轉(zhuǎn)換
>>> arrow.get('2018-02-24T13:00:00.000-07:00') <Arrow [2018-02-24T13:00:00-07:00]>
可以從字符串中通過格式參數(shù)搜索時(shí)間
>>> arrow.get('June was born in May 1980', 'MMMM YYYY') <Arrow [1980-05-01T00:00:00+00:00]>
直接創(chuàng)建arrow對(duì)象
>>> arrow.get(2018, 2, 24) <Arrow [2018-02-24T00:00:00+00:00]> >>> arrow.Arrow(2018, 2, 24) <Arrow [2018-02-24T00:00:00+00:00]>
arrow對(duì)象屬性 datetime,timestamp,native,tzinfo
>>> a = arrow.utcnow() >>> a.datetime datetime.datetime(2018, 2, 24, 21, 15, 50, 841056, tzinfo=tzlocal()) >>> a.timestamp 1519478150 >>> a.naive datetime.datetime(2018, 2, 24, 21, 58, 4, 309575) >>> a.tzinfo tzlocal()
獲取datetime對(duì)象的值
>>> a.hour 21 >>> a.day 2
時(shí)間推移 a.shift(**kwargs)
shift方法獲取某個(gè)時(shí)間之前或之后的時(shí)間,關(guān)鍵字參數(shù)為years,months,weeks,days,hours,seconds,microseconds
>>> a.shift(weeks=+3) #三周后 <Arrow [2018-03-17T21:58:04.309575+08:00]> >>> a.shift(days=-1) #一天前 <Arrow [2018-02-23T21:58:04.309575+08:00] >>> a.shift(weekday=6) #距離最近a的星期日,weekday從0到6 <Arrow [2018-02-25T21:58:04.309575+08:00]>
時(shí)間替換 a.replace(**kwargs)
返回一個(gè)被替換后的arrow對(duì)象,原對(duì)象不變
>>> a <Arrow [2018-02-24T21:58:04.309575+08:00]> >>> a.replace(hour=9) <Arrow [2018-02-24T09:58:04.309575+08:00]>
格式化輸出 a.format([format_string])
>>> a.format() '2018-02-24 21:58:04+08:00' >>> a.format('YYYY-MM-DD HH:mm:ss ZZ') '2018-02-24 21:58:04 +08:00'
人性化輸出 a.humanize()
>>> present = arrow.utcnow() >>> past = present.shift(hours=-1) >>> past.humanize() #相對(duì)于當(dāng)前時(shí)間 'an hour age' >>> future = present.shift(hours=2) >>> future.humanize(present) #相對(duì)于參數(shù)時(shí)間 'in 2 hours' >>> past.humanize(present, locale='zh') #locale參數(shù)可以指定地區(qū)語言 '1天前'
時(shí)間范圍和區(qū)間 a.span(string), a.floor(), a.ceil()
arrow.Arrow.span_range(),arrow.Arrow.range()
>>> a <Arrow [2018-02-24T21:58:04.309575+08:00]> >>> a.span('hour') #a所在的時(shí)間區(qū)間 (<Arrow [2018-02-24T21:00:00+08:00]>, <Arrow [2018-02-24T21:59:59.999999+08:00]>) >>> a.floor('hour') #a所在區(qū)間的開始 <Arrow [2018-02-24T21:00:00+08:00]> >>> a.ceil('hour') #a所在區(qū)間的結(jié)尾 <Arrow [2018-02-24T21:59:59.999999+08:00]
>>> start = datetime.datetime(2018, 2, 24, 12, 30) >>> end = datetime.datetime(2018, 2, 24, 15, 20) >>> for r in arrow.Arrow.span_range('hour',start,end): #獲取start,end之間的時(shí)間區(qū)間 ... print(r) ... (<Arrow [2018-02-24T12:00:00+00:00]>, <Arrow [2018-02-24T12:59:59.999999+00:00]>) (<Arrow [2018-02-24T13:00:00+00:00]>, <Arrow [2018-02-24T13:59:59.999999+00:00]>) (<Arrow [2018-02-24T14:00:00+00:00]>, <Arrow [2018-02-24T14:59:59.999999+00:00]>) (<Arrow [2018-02-24T15:00:00+00:00]>, <Arrow [2018-02-24T15:59:59.999999+00:00]>) >>> for r in arrow.Arrow.range('hour',start,end): #獲取間隔單位時(shí)間的時(shí)間 ... print(r) ... 2018-02-24T12:30:00+00:00 2018-02-24T13:30:00+00:00 2018-02-24T14:30:00+00:00
格式化字符串標(biāo)記
更多請(qǐng)參考官方文檔和Github
官方文檔
Github
到此這篇關(guān)于Python 處理日期時(shí)間的Arrow庫使用的文章就介紹到這了,更多相關(guān)Python 日期時(shí)間Arrow庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pytest參數(shù)化:@pytest.mark.parametrize詳解
pytest.mark.parametrize裝飾器能夠?qū)y(cè)試函數(shù)進(jìn)行參數(shù)化處理,使得一個(gè)測(cè)試函數(shù)可以用多組數(shù)據(jù)執(zhí)行多次,這有助于檢查不同輸入下的期望輸出是否匹配,提高測(cè)試的效率和覆蓋率,裝飾器可以應(yīng)用于函數(shù)、模塊或類,支持多個(gè)裝飾器組合使用,增強(qiáng)測(cè)試的靈活性和綜合性2024-10-10django的模型類管理器——數(shù)據(jù)庫操作的封裝詳解
這篇文章主要介紹了django的模型類管理器——數(shù)據(jù)庫操作的封裝詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-04-04解決Pytorch在測(cè)試與訓(xùn)練過程中的驗(yàn)證結(jié)果不一致問題
這篇文章主要介紹了解決Pytorch在測(cè)試與訓(xùn)練過程中的驗(yàn)證結(jié)果不一致問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06利用pandas進(jìn)行大文件計(jì)數(shù)處理的方法
今天小編就為大家分享一篇利用pandas進(jìn)行大文件計(jì)數(shù)處理的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07Python本地cache不當(dāng)使用導(dǎo)致內(nèi)存泄露的問題分析與解決
最近在項(xiàng)目開發(fā)中遇到了本地cache不當(dāng)使用導(dǎo)致的一個(gè)內(nèi)存泄露問題,所以本文主要分析了問題出現(xiàn)的原因已經(jīng)解決方法,需要的小伙伴可以參考下2023-08-08