
題圖:Photo by Pablo García Saldaña on Unsplash
抱歉我用了個有點标題黨的标題,因為擔心你錯過了本文,但内容絕對幹貨,本文介紹的關于Python時間日期處理,日期時間處理在實際應用場景中無處不在,是以這也成了程式設計語言中必不可少的子產品,Python 也不例外。但是,你知道在Python中有多少個相關的子產品嗎?datetime、time、calendar、 dateutil、 pytz 等等。 你知道有多少種資料類型嗎?date、time、datetime、tzinfo、timedelta 等等。
有天我遇到這樣的需求,想擷取目前月的前一個月是幾月,假設本月是2018年1月,那麼上個月就是2017年12月,大約要經過這麼幾個步驟
>>> import datetime
# 1. 擷取「今天」
>>> today = datetime.date.today()
# 2. 擷取目前月的第一天
>>> first = today.replace(day=1)
# 3. 減一天,得到上個月的最後一天
>>> last_month = first - datetime.timedelta(days=1)
# 4. 格式化成指定形式
>>> print(last_month.strftime("%Y%m"))
201807
>>>
這是有多麻煩? 麻煩得你懷疑這是 Python 代碼? 可能有人會說,用 datetime.replace 方法将 month-1 就好了,咋看起來沒問題,實際上這是有 bug 的,month 的範圍隻能是 1-12
>>> d.replace(month=d.month-1)
datetime.datetime(2018, 7, 24, 11, 29, 28, 929830)
month-11 就報錯了
>>> d.replace(month=d.month-11)
ValueError: month must be in 1..12
你還知道日期時間、時間戳、字元串之間的互相轉換的那些 API 方法嗎?是不是每次處理時間相關的操作時,總要去官方文檔翻看一遍才能動手。你可以看看 time 子產品和 datetime 子產品中各種類型之間的互相轉換,看着這些箭頭是不是有密集恐懼症?
不管怎樣,你終究還是要熟練這些子產品和API操作的,記不住沒關系,至少你都要手動敲幾遍,下次遇到時要能做到翻看文檔能快速定位到某個類某個方法是做什麼用、怎麼用。
但今天我要強烈安利給你的這個時間日期庫:Arrow。它的靈感來自于 requests 庫。将所有繁雜的東西全部隐藏于身後,留給你的是 for humans 接口。充分诠釋了 keep it simple and stupid 這句話的含義。
這篇文章帶你初步了解并掌握 Arrow 的使用方式。
安裝$ pip install arrow
使用>>> a = arrow.now() # 目前本地時間
>>> a
<Arrow [2018-08-24T07:09:03.468562+08:00]>
>>> arrow.utcnow() # 目前utc時間
<Arrow [2018-08-23T23:11:50.147585+00:00]>
你可以認為 Arrow 對象是一個增強版的 datetime 對象。
通過 Arrow 對象你可以擷取 datetime 對象
>>> t = a.datetime
>>> type(t)
<class 'datetime.datetime'>
>>> t
datetime.datetime(2018, 8, 24, 7, 17, 14, 884750, tzinfo=tzlocal())
通過 Arrow 對象你可以得到時間戳
>>> a.timestamp
1535066234
擷取 arrow 對象的年、月、日、時、分、秒
>>> a.year
2018
>>> a.month
8
>>> a.day
24
>>> a.hour
7
擷取 arrow 對象的時間和日期
>>> a.date()
datetime.date(2018, 8, 24)
>>> a.time()
datetime.time(7, 9, 3, 468562)
注意,擷取時間和日期是用方法,而擷取 datetime 和 timestamp 是兩個屬性
接下來介紹一些 arrow 有意思的方法
shiftshift 有點像遊标卡尺,可以左右兩邊進行加減移位操作,加減的對象可以是年月日時分秒和星期。再回到文章開始地方,想擷取目前月的前一個月,你可以這樣寫:
>>> a.shift(months=-1)
<Arrow [2018-07-24T07:09:03.468562+08:00]>
>>> a.shift(months=-1).format("YYYYMM")
'201807'
>>>
指定參數 months = -1 就可以了。往後一個月就是 month=+1, 加号可以省略。這樣你可以基于一個 arrow 時間對象進行任意的往前加或者往後減。 注意 month 後面有個s, year 同理。 以下是一些例子。
加一個月
>>> a.shift(months=1)
<Arrow [2018-09-24T07:09:03.468562+08:00]>
減一個月
減兩年
>>> a.shift(years=-2)
<Arrow [2016-08-24T07:09:03.468562+08:00]>
加一個小時
>>> a.shift(hours=1)
<Arrow [2018-08-24T08:09:03.468562+08:00]>
還可以按周進行加減
>>> a.shift(weeks=1)
<Arrow [2018-08-31T07:09:03.468562+08:00]>
如果你要明确指定修改成哪年或者哪月,那麼使用 replace 方法即可,repalce 在 datetime 對象中也有該方法,兩者的使用方式是一樣的。
humanizehumanize 方法是相對于目前時刻表示為“多久以前”的一種可讀行字元串形式,預設是英文格式,指定 locale 可顯示相應的語言格式。
>>> a.humanize()
'6 hours ago'
>>> a.humanize(locale='zh')
'6小時前'
formatformat 是格式化工具,可以根據指定的格式将 arrow 對象轉換成字元串格式,格式Token請參考下圖
>>> a.format()
'2018-08-24 07:09:03+08:00'
>>> a.format("YYYY-MM-DD HH:mm:ss")
'2018-08-24 07:09:03'
to 可以将一個本地時區轉換成其它任意時區,例如:
>>> arrow.now()
<Arrow [2018-08-24T16:58:50.990657+08:00]>
>>> arrow.now().to("utc")
<Arrow [2018-08-24T08:59:04.316289+00:00]>
>>> arrow.now().to("utc").to("local")
<Arrow [2018-08-24T16:59:15.800847+08:00]>
>>> arrow.now().to("America/New_York")
<Arrow [2018-08-24T04:59:34.037182-04:00]>
建構 Arrow 對象前面介紹了 arrow 可以轉換成datetime、str、date、time、timestamp,那麼如何建構 Arrow 對象呢?除了使用 now()、utcnow() 方法之後,你還可以使用 get 工廠方法,或者使用 Arrow 構造方法直接指定年月日時分秒
>>> arrow.Arrow(2018, 8, 24, 12, 30, 45)
<Arrow [2018-08-24T12:30:45+00:00]>
>>> arrow.Arrow(2018, 8, 24, 12, 30, 45, tzinfo='utc')
>>> arrow.Arrow(2018, 8, 24, 12, 30, 45, tzinfo='local')
<Arrow [2018-08-24T12:30:45+08:00]>
get第二種方式是用get方法來建立 arrow 對象,get 方法”非常靈活”,直接看例子,跟着敲
# 不帶參數,等價與 utcnow()
>>> arrow.get()
<Arrow [2018-08-24T07:11:50.528742+00:00]>
# 接受時間戳參數
>>> arrow.get(1535113845)
# 接受一個datetime對象
<Arrow [2018-08-24T00:00:00+00:00]>
>>> arrow.get(datetime(2018,8,24))
# 接收一個date對象
>>> from datetime import date
>>> arrow.get("2018-08-11 12:30:56")
>>> arrow.get(date(2018,7,24))
<Arrow [2018-07-24T00:00:00+00:00]>
# 接收日期格式的字元串
<Arrow [2018-08-11T12:30:56+00:00]>
# 接收日期字元串,并指定格式
>>> arrow.get("18-08-11 12:30:56", "YY-MM-DD HH:mm:ss")
Arrow 的不足關于 get 方法,看似強大,使用起來靈活,感覺什麼參數都能接受,但是也帶來了一些問題,甚至是 bug。比如
>>> arrow.get("2018-7-11")
<Arrow [2018-01-01T00:00:00+00:00]>
期望的值應該是 2018-07-11, 但是它并沒有提示錯誤,而正确的做法是要指定格式,因為你傳的字元串不是标準的日期格式。
>>> arrow.get("2018-7-11", "YYYY-M-DD")
<Arrow [2018-07-11T00:00:00+00:00]>
想通過一個方法來相容n種情況是極度困難的,内部實作也會非常複雜,作為使用者使用起來必然也很混亂。
原文釋出時間為:2018-11-24
本文作者:志軍100
本文來自雲栖社群合作夥伴“
Python愛好者社群”,了解相關資訊可以關注“
”。