arrow python處理日期時間


Python針對日期時間的處理提供了大量的package,類和方法,但在可用性上來看非常繁瑣和麻煩

第三方庫Arrow提供了一個合理的、人性化的方法來創建、操作、格式轉換的日期,時間,和時間戳,幫助我們使用較少的導入和更少的代碼來處理日期和時間。

 pip install arrow

獲取當前時間    arrow.utcnow(), arrow.now()

>>> arrow.utcnow()
<Arrow [2013-05-07T04:20:39.369271+00:00]>

>>> arrow.now()
<Arrow [2013-05-06T21:20:40.841085-07:00]>

>>> arrow.now('US/Pacific')
<Arrow [2013-05-06T21:20:44.761511-07:00]>

將時間戳轉化為arrow對象    arrow.get(timestamp) 

>>> arrow.get(1367900664)
<Arrow [2013-05-07T04:24:24+00:00]>

>>> arrow.get(1367900664.152325)
<Arrow [2013-05-07T04:24:24.152325+00:00]>

   # 時間戳可以是int,float或者可以轉化為float的字符串

將字符串轉換為arrow對象    arrow.get(string[,format_string])

>>> arrow.get(datetime.utcnow())
<Arrow [2013-05-07T04:24:24.152325+00:00]>

>>> arrow.get(datetime(2013, 5, 5), 'US/Pacific')
<Arrow [2013-05-05T00:00:00-07:00]>

>>> from dateutil import tz
>>> arrow.get(datetime(2013, 5, 5), tz.gettz('US/Pacific'))
<Arrow [2013-05-05T00:00:00-07:00]>

>>> arrow.get(datetime.now(tz.gettz('US/Pacific')))
<Arrow [2013-05-06T21:24:49.552236-07:00]>

直接創建arrow對象

>>> arrow.get('2013-05-05 12:30:45', 'YYYY-MM-DD HH:mm:ss')
<Arrow [2013-05-05T12:30:45+00:00]>

>>> arrow.get('June was born in May 1980', 'MMMM YYYY') <Arrow [1980-05-01T00:00:00+00:00]>

>>> arrow.get('2013-09-30T15:34:00.000-07:00') <Arrow [2013-09-30T15:34:00-07:00]>
>>> arrow.get(2013, 5, 5) <Arrow [2013-05-05T00:00:00+00:00]> >>> arrow.Arrow(2013, 5, 5) <Arrow [2013-05-05T00:00:00+00:00]>

arrow對象屬性    datetime,timestamp,native,tzinfo

>>> a = arrow.utcnow()
>>> a.datetime
datetime.datetime(2013, 5, 7, 4, 38, 15, 447644, tzinfo=tzutc())

>>> a.timestamp
1367901495

>>> a.naive datetime.datetime(2013, 5, 7, 4, 38, 15, 447644) >>> a.tzinfo tzutc()
 
         
>>> a.year 2013
 
         
>>> a.date() datetime.date(2013, 5, 7) >>> a.time() datetime.time(4, 38, 15, 447644)
 
        

Replace & Shift

替換和推移時間

shift方法獲取某個時間之前或之后的時間,關鍵字參數為 years, months, days, hours, minutes, seconds, microseconds, weeks, quarters, weekday

 

>>> arw = arrow.utcnow()
>>> arw
<Arrow [2013-05-12T03:29:35.334214+00:00]>

>>> arw.replace(hour=4, minute=40)
<Arrow [2013-05-12T04:40:35.334214+00:00]>

>>> arw.shift(weeks=+3) <Arrow [2013-06-02T03:29:35.334214+00:00]>

>>> arw.replace(tzinfo='US/Pacific') <Arrow [2013-05-12T03:29:35.334214-07:00]>

格式化輸出 和 類型轉換

>>> arrow.utcnow().format('YYYY-MM-DD HH:mm:ss ZZ')
'2013-05-07 05:23:16 -00:00
>>> utc = arrow.utcnow()
>>> utc
<Arrow [2013-05-07T05:24:11.823627+00:00]>

>>> utc.to('US/Pacific')
<Arrow [2013-05-06T22:24:11.823627-07:00]>

>>> utc.to(tz.gettz('US/Pacific'))
<Arrow [2013-05-06T22:24:11.823627-07:00]>
>>> utc.to('local')
<Arrow [2013-05-06T22:24:11.823627-07:00]>

>>> utc.to('local').to('utc')
<Arrow [2013-05-07T05:24:11.823627+00:00]>

人性化輸出    a.humanize()

>>> past = arrow.utcnow().shift(hours=-1)
>>> past.humanize()
'an hour ago'

>>> present = arrow.utcnow() >>> future = present.shift(hours=2) >>> future.humanize(present) 'in 2 hours'
 
         
>>> present = arrow.utcnow() >>> future = present.shift(hours=2) >>> future.humanize(present) 'in 2 hours' >>> future.humanize(present, only_distance=True) '2 hours'
 
         
>>> present = arrow.utcnow() >>> future = present.shift(minutes=66) >>> future.humanize(present, granularity="minute") 'in 66 minutes' >>> future.humanize(present, granularity=["hour", "minute"]) 'in an hour and 6 minutes' >>> present.humanize(future, granularity=["hour", "minute"]) 'an hour and 6 minutes ago' >>> future.humanize(present, only_distance=True, granularity=["hour", "minute"]) 'an hour and 6 minutes'
 
>>> future = arrow.utcnow().shift(hours=1) >>> future.humanize(a, locale='ru') 'через 2 час(а,ов)'

參考:github


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM