python arrow庫詳解


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

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

$ pip install arrow
  • 獲取當前時間arrow.utcnow()arrow.now()
>>> import arrow
>>> utc = arrow.utcnow()  # 獲取世界標准時間
>>> utc
<Arrow [2018-06-07T09:37:28.989983+00:00]>
>>> utc = arrow.now()  # 獲取本地時間
>>> utc
<Arrow [2018-06-07T17:40:19.019529+08:00]>
>>> arrow.now('US/Pacific')  # 獲取指定時區的時間
<Arrow [2018-06-07T02:41:54.815029-07:00]>

 

  • 將時間戳轉化為arrow對象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]>

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

  • 將字符串轉換為arrow對象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的字符串不需要格式字符串參數即可轉換
>>> arrow.get('2018-02-24T13:00:00.000-07:00')
<Arrow [2018-02-24T13:00:00-07:00]>
  • 可以從字符串中通過格式參數搜索時間
>>> arrow.get('June was born in May 1980', 'MMMM YYYY')
<Arrow [1980-05-01T00:00:00+00:00]>
  • 直接創建arrow對象
>>> 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對象屬性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()
  • 按名稱或tzinfo轉換為時區
>>> arw = arrow.utcnow()
>>> arw
<Arrow [2018-06-07T11:16:51.695083+00:00]>
>>> arw.to('US/Pacific')
<Arrow [2018-06-07T04:16:51.695083-07:00]>
  • 獲取datetime對象的值
>>> a = arrow.now()
>>> a
<Arrow [2018-06-07T17:44:43.519166+08:00]>
>>> a.year  # 當前年
2018
>>> a.month  # 當前月份
6
>>> a.day  # 當前天
7
>>> a.hour  # 當前第幾個小時
17
>>> a.minute  # 當前多少分鍾
44
>>> a.second  # 當前多少秒
43
>>> a.timestamp  # 獲取時間戳
1528364683
>>> a.float_timestamp  # 浮點數時間戳
1528364683.519166

 

  • 時間推移a.shift(**kwargs)

shift方法獲取某個時間之前或之后的時間,關鍵字參數為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]> 
  • 時間替換a.replace(**kwargs)

返回一個被替換后的arrow對象,原對象不變

>>> 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 = arrow.now()
>>> a
<Arrow [2018-06-07T17:59:36.917894+08:00]>
>>> a.format()
'2018-06-07 17:59:36+08:00'
>>> a.format('YYYY-MM-DD HH:mm:ss ZZ')
'2018-06-07 17:59:36 +08:00'
>>> a.ctime()  # 返回日期和時間的ctime格式化表示。
'Thu Jun  7 17:59:36 2018'  
>>> a.weekday()  # 以整數形式返回星期幾(0-6)
3
>>> a.isoweekday()  # 以整數形式返回一周中的ISO日(1-7)
4
>>> a.isocalendar()  # 返回3元組(ISO年,ISO周數,ISO工作日)
(2018, 23, 4)
>>> a.toordinal()  # 返回日期的格雷戈里序數
736852
  • 人性化輸出    a.humanize()
>>> present = arrow.utcnow()
>>> past = present.shift(hours=-1)
>>> past.humanize()       #相對於當前時間
'an hour age'
>>> future = present.shift(hours=2)
>>> future.humanize(present)    #相對於參數時間
'in 2 hours'
>>> past.humanize(present, locale='zh')   #locale參數可以指定地區語言
'1天前'
  • 時間范圍和區間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所在的時間區間
(<Arrow [2018-02-24T21:00:00+08:00]>, <Arrow [2018-02-24T21:59:59.999999+08:00]>)    
>>> a.floor('hour')   #a所在區間的開始 
<Arrow [2018-02-24T21:00:00+08:00]>
>>> a.ceil('hour')    #a所在區間的結尾
<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之間的時間區間
...     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):        #獲取間隔單位時間的時間
...     print(r)
...
2018-02-24T12:30:00+00:00
2018-02-24T13:30:00+00:00
2018-02-24T14:30:00+00:00

格式化字符串標記

image.png

 


免責聲明!

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



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