Python日期時間模塊arrow的具體使用
Python中有很多時間和日期處理的庫,有time、datetime等,雖然提供了很完整的對日期、時間以及時區(qū)轉(zhuǎn)換處理的功能,但是方法過多,不易于記憶,而且經(jīng)常需要各種轉(zhuǎn)換操作,非常繁瑣,比如時間和時間戳的轉(zhuǎn)換,格式化時間字符串轉(zhuǎn)換等等,幾乎每次使用都要先看一下教程文檔。那么有沒有使用起來更人性化的日期時間處理庫呢?接下來就來看一下arrow日期時間庫。
arrow是一個專門處理時間和日期的輕量級Python庫,它提供了一種合理、人性化的方式來創(chuàng)建、操作、格式化、轉(zhuǎn)換日期、時間和時間戳,可以比較輕易的創(chuàng)建具有時區(qū)意識的日期和時間實例。
可以使用pip install arrow進行安裝。
arrow模塊的使用
獲取arrow對象
Arrow可以很靈活的將多種格式的時間數(shù)據(jù)轉(zhuǎn)換成Arrow對象,如下:
import arrow print(repr(arrow.Arrow(2021, 8, 23, 8))) print(repr(arrow.get(2021, 8, 23, 8, 40))) print(repr(arrow.get('2021-08-23 09:00'))) print(repr(arrow.get('2021.08.23'))) print(repr(arrow.get('23/2012/08', 'DD/YYYY/MM')))
執(zhí)行結(jié)果如下:
上面幾種方式都可以將字符數(shù)據(jù)轉(zhuǎn)換為arrow對象,轉(zhuǎn)換非常靈活。除此之外,還可以把時間戳轉(zhuǎn)換為arrow對象。
print(repr(arrow.get(1629683393.6558669)))
獲取當(dāng)前時間
utc_time = arrow.utcnow() local_time = arrow.now() print(utc_time) print(local_time)
通過utcnow()函數(shù)和now()函數(shù)分別獲取的是utc時間和本地時間,當(dāng)然我們也可以在調(diào)用now()時指定時區(qū),從而獲取指定時區(qū)的時間,例如arrow.now('US/Pacific')。
時間形式轉(zhuǎn)換
使用日期時間的時候我們經(jīng)常需要轉(zhuǎn)換操作,比如轉(zhuǎn)換成指定格式的時間字符串,轉(zhuǎn)換成時間戳等。
轉(zhuǎn)換成時間字符串
now = arrow.now() print(now) print(now.format()) print(now.format("YYYY-MM-DD hh:mm:ss")) print(now.format("YYYY-MM-DD"))
執(zhí)行結(jié)果如下:
看到這個,是不是感覺比datetime模塊的'%Y-%M-%D %h:%m:%s'格式化方式更人性化更便于記憶呢。
轉(zhuǎn)換成時間戳
可以使用t.timestamp將arrow對象轉(zhuǎn)換成時間戳。
now.timestamp
獲取數(shù)據(jù)
轉(zhuǎn)換為Arrow對象后,我們可以很方便的獲取我們想要的各種時間數(shù)據(jù),通過year、month、day、hour、minute、second、week等屬性,如:
now = arrow.now() print(now.year) print(now.month) print(now.day) print(now.hour) print(now.minute) print(now.second) print(now.week)
修改時間
有時拿到一個時間時,我們需要對時間就行修改,例如修改時區(qū)、修改時間等等,我們可以使用以下方式去修改。
now = arrow.now() print(now.format("YYYY-MM-DD hh:mm:ss")) # 2021-08-23 10:11:04 now_utc = now.to("utc") print(now_utc.format("YYYY-MM-DD hh:mm:ss")) # 2021-08-23 02:11:04 now1 = now.replace(day=31, hour=12) print(now1.format("YYYY-MM-DD hh:mm:ss")) # 2021-08-31 12:11:04 now2 = now.shift(months=-2) print(now2.format("YYYY-MM-DD hh:mm:ss")) # 2021-06-23 10:11:04
我們可以使用to()方法切換時區(qū),使用replace()方法修改時間,使用shift()進行時間的前后推移。
將字符串轉(zhuǎn)換為arrow對象 arrow.get(string[,format_string])
In [52]: arrow.get('2018-03-22 23:35:34') Out[52]: <Arrow [2018-03-22T23:35:34+00:00]>
可以從字符串中通過格式參數(shù)搜索時間
In [13]: arrow.get('june waw born in May 1999', 'MMMM YYYY') Out[13]: <Arrow [1999-05-01T00:00:00+00:00]>
arrow對象屬性 datetime,timestamp,naive,tzinfo
In [54]: t.datetime Out[54]: datetime.datetime(2019, 3, 22, 21, 39, 0, 223147, tzinfo=tzlocal()) In [55]: t.timestamp Out[55]: 1553261940 In [58]: t.tzinfo Out[58]: tzlocal() In [59]: t.naive Out[59]: datetime.datetime(2019, 3, 22, 21, 39, 0, 223147)
獲取datetime對象的值
In [60]: t.year Out[60]: 2019 In [62]: t.month Out[62]: 3 In [63]: t.day Out[63]: 22 In [64]: t.hour Out[64]: 21
時間推移 a.shift(**kwargs)
shift方法獲取某個時間之前或之后的時間,關(guān)鍵字參數(shù)為years,months,weeks,days,hours,seconds,microseconds
In [65]: t.shift(weeks=-1) Out[65]: <Arrow [2019-03-15T21:39:00.223147+08:00]> In [66]: t.shift(days=20) Out[66]: <Arrow [2019-04-11T21:39:00.223147+08:00]> In [67]: t.shift(hours=1) Out[67]: <Arrow [2019-03-22T22:39:00.223147+08:00]>
時間替換 a.replace(**kwargs)
返回一個被替換后的arrow對象,原對象不變
In [68]: t.replace(year=2018) Out[68]: <Arrow [2018-03-22T21:39:00.223147+08:00]> In [69]: t Out[69]: <Arrow [2019-03-22T21:39:00.223147+08:00]>
格式化輸出 a.format([format_string])
In [70]: t.format() Out[70]: '2019-03-22 21:39:00+08:00' In [71]: t.format('YYYY-MM-DD HH-MM-SS') Out[71]: '2019-03-22 21-03-22'
最牛的是這個人性化輸出 a.humanize()
In [72]: t.humanize() Out[72]: '2 hours ago
同Python內(nèi)置日期datetime庫一樣,arrow對象也支持時間的大小對比,還有計算時間差操作,除此之外,還有很多意想不到的操作,感興趣的話,可以查看官方文檔:Arrow: Better dates & times for Python — Arrow 🏹 1.1.1 documentation)
到此這篇關(guān)于Python日期時間模塊arrow的具體使用的文章就介紹到這了,更多相關(guān)Python日期時間模塊arrow 內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學(xué)習(xí)參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。