亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python日期時(shí)間模塊arrow的具體使用

 更新時(shí)間:2021年09月02日 10:25:37   作者:tigeriaf  
Python中有很多時(shí)間和日期處理的庫,有time、datetime等,本文主要介紹了一下arrow,arrow是一個專門處理時(shí)間和日期的輕量級Python庫,感興趣的可以了解一下

Python中有很多時(shí)間和日期處理的庫,有time、datetime等,雖然提供了很完整的對日期、時(shí)間以及時(shí)區(qū)轉(zhuǎn)換處理的功能,但是方法過多,不易于記憶,而且經(jīng)常需要各種轉(zhuǎn)換操作,非常繁瑣,比如時(shí)間和時(shí)間戳的轉(zhuǎn)換,格式化時(shí)間字符串轉(zhuǎn)換等等,幾乎每次使用都要先看一下教程文檔。那么有沒有使用起來更人性化的日期時(shí)間處理庫呢?接下來就來看一下arrow日期時(shí)間庫。

arrow是一個專門處理時(shí)間和日期的輕量級Python庫,它提供了一種合理、人性化的方式來創(chuàng)建、操作、格式化、轉(zhuǎn)換日期、時(shí)間和時(shí)間戳,可以比較輕易的創(chuàng)建具有時(shí)區(qū)意識的日期和時(shí)間實(shí)例。
可以使用pip install arrow進(jìn)行安裝。

arrow模塊的使用

獲取arrow對象

Arrow可以很靈活的將多種格式的時(shí)間數(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)換非常靈活。除此之外,還可以把時(shí)間戳轉(zhuǎn)換為arrow對象。

print(repr(arrow.get(1629683393.6558669)))

獲取當(dāng)前時(shí)間

utc_time = arrow.utcnow()
local_time = arrow.now()
print(utc_time)
print(local_time)

通過utcnow()函數(shù)和now()函數(shù)分別獲取的是utc時(shí)間和本地時(shí)間,當(dāng)然我們也可以在調(diào)用now()時(shí)指定時(shí)區(qū),從而獲取指定時(shí)區(qū)的時(shí)間,例如arrow.now('US/Pacific')。

時(shí)間形式轉(zhuǎn)換

使用日期時(shí)間的時(shí)候我們經(jīng)常需要轉(zhuǎn)換操作,比如轉(zhuǎn)換成指定格式的時(shí)間字符串,轉(zhuǎn)換成時(shí)間戳等。

轉(zhuǎn)換成時(shí)間字符串

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)換成時(shí)間戳

可以使用t.timestamp將arrow對象轉(zhuǎn)換成時(shí)間戳。

now.timestamp

獲取數(shù)據(jù)

轉(zhuǎn)換為Arrow對象后,我們可以很方便的獲取我們想要的各種時(shí)間數(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)

修改時(shí)間

有時(shí)拿到一個時(shí)間時(shí),我們需要對時(shí)間就行修改,例如修改時(shí)區(qū)、修改時(shí)間等等,我們可以使用以下方式去修改。

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()方法切換時(shí)區(qū),使用replace()方法修改時(shí)間,使用shift()進(jìn)行時(shí)間的前后推移。

將字符串轉(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ù)搜索時(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

時(shí)間推移    a.shift(**kwargs)

shift方法獲取某個時(shí)間之前或之后的時(shí)間,關(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]>

時(shí)間替換   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對象也支持時(shí)間的大小對比,還有計(jì)算時(shí)間差操作,除此之外,還有很多意想不到的操作,感興趣的話,可以查看官方文檔:Arrow: Better dates & times for Python — Arrow 🏹 1.1.1 documentation)

到此這篇關(guān)于Python日期時(shí)間模塊arrow的具體使用的文章就介紹到這了,更多相關(guān)Python日期時(shí)間模塊arrow 內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家! 

相關(guān)文章

最新評論