新聞中心
關(guān)于日期處理,Python 提供了很多的庫(kù),比如標(biāo)準(zhǔn)庫(kù) datetime、第三方庫(kù) dateutil、arrow 等等。

在使用之前需要先安裝,直接 pip install pendulum 即可。
下面來(lái)看一下用法,首先是 datetime, date, time 的創(chuàng)建。
import pendulum
dt = pendulum.datetime(
2022, 3, 28, 20, 10, 30)
print(dt.__class__)
print(dt)
"""
2022-03-28T20:10:30+00:00
"""
# 創(chuàng)建的對(duì)象是 DateTime 類(lèi)型
# 并且?guī)в袝r(shí)區(qū),默認(rèn)是 UTC
# 我們可以換一個(gè)時(shí)區(qū)
dt = pendulum.datetime(
2022, 3, 28, 20, 10, 30, tz="Asia/Shanghai")
print(dt)
"""
2022-03-28T20:10:30+08:00
"""
# 如果不想要時(shí)區(qū),那么指定 tz=None
dt = pendulum.datetime(
2022, 3, 28, 20, 10, 30, tz=None)
print(dt)
"""
2022-03-28T20:10:30
"""
# 然后是 date 的創(chuàng)建
d = pendulum.date(2022, 3, 28)
print(d.__class__)
print(d)
"""
2022-03-28
"""
# time 的創(chuàng)建
t = pendulum.time(20, 10, 30)
print(t.__class__)
print(t)
"""
20:10:30
"""
如果創(chuàng)建 datetime 時(shí),時(shí)區(qū)默認(rèn)是 UTC。如果不想要時(shí)區(qū),或者希望時(shí)區(qū)是本地時(shí)區(qū),那么 pendulum 還專(zhuān)門(mén)提供了兩個(gè)方法。
import pendulum
# 創(chuàng)建 datetime 時(shí)設(shè)置為本地時(shí)區(qū)
# 還是調(diào)用了 pendulum.datetime 函數(shù)
# 但是 tz 被設(shè)置成了 pendulum.local_timezone()
dt = pendulum.local(
2022, 3, 28, 20, 10, 30)
print(dt)
"""
2022-03-28T20:10:30+08:00
"""
print(pendulum.local_timezone())
"""
Timezone('Asia/Shanghai')
"""
# 創(chuàng)建 datetime 時(shí)不設(shè)置時(shí)區(qū)
# 內(nèi)部也是調(diào)用了 pendulum.datetime 函數(shù)
# 但是 tz 為 None
dt = pendulum.naive(2022, 3, 28, 20, 10, 30)
print(dt)
"""
2022-03-28T20:10:30
"""
然后 pendulum 還提供了幾個(gè)方法,比如創(chuàng)建當(dāng)前的 datetime,date 等等。
import pendulum
# 創(chuàng)建當(dāng)前的 datetime
# 默認(rèn)是本地時(shí)區(qū),但時(shí)區(qū)可以指定
dt = pendulum.now()
print(dt)
"""
2022-05-29T20:40:49.632182+08:00
"""
# 創(chuàng)建當(dāng)前的 date,但返回的仍是 datetime
# 只不過(guò)時(shí)分秒均為 0,同樣可以指定時(shí)區(qū)
dt = pendulum.today()
print(dt)
"""
2022-05-29T00:00:00+08:00
"""
# 獲取明天對(duì)應(yīng)的 date
# 返回的是 datetime,時(shí)分秒為 0
# 時(shí)區(qū)可以指定,默認(rèn)是本地時(shí)區(qū)
dt = pendulum.tomorrow()
print(dt)
"""
2022-05-30T00:00:00+08:00
"""
# 獲取昨天對(duì)應(yīng)的 date
dt = pendulum.yesterday()
print(dt)
"""
2022-05-28T00:00:00+08:00
"""
我們還可以根據(jù)時(shí)間戳或者字符串來(lái)創(chuàng)建:
import pendulum
# 根據(jù)時(shí)間戳創(chuàng)建
dt1 = pendulum.from_timestamp(1653828466)
dt2 = pendulum.from_timestamp(1653828466,
tz=pendulum.local_timezone())
print(dt1)
print(dt2)
"""
2022-05-29T12:47:46+00:00
2022-05-29T20:47:46+08:00
"""
# 根據(jù)字符串創(chuàng)建
dt1 = pendulum.parse("2020-05-03 12:11:33")
dt2 = pendulum.parse("2020-05-03 12:11:33",
tz=pendulum.local_timezone())
print(dt1)
print(dt2)
"""
2020-05-03T12:11:33+00:00
2020-05-03T12:11:33+08:00
"""
datetime、date、time 的創(chuàng)建我們說(shuō)完了,然后再來(lái)看看它們支持的操作,這也是最核心的部分。
datetime 相關(guān)操作
操作非常多,我們逐一介紹。
import pendulum
dt = pendulum.local(
2022, 3, 28, 20, 10, 30)
# 獲取 date 部分和 time 部分
print(dt.date())
print(dt.time())
"""
2022-03-28
20:10:30
"""
# 替換掉 dt 的某部分,返回新的 datetime
# 年月日時(shí)分秒、以及時(shí)區(qū)都可以替換
print(dt.replace(year=9999))
"""
9999-03-28T20:10:30+08:00
"""
# 轉(zhuǎn)成時(shí)間戳
print(dt.timestamp())
"""
1648469430.0
"""
# 返回年、月、日、時(shí)、分、秒、時(shí)區(qū)
print(dt.year, dt.month, dt.day)
print(dt.hour, dt.minute, dt.second)
print(dt.tz)
"""
2022 3 28
20 10 30
Timezone('Asia/Shanghai')
"""
然后是生成字符串,pendulum.DateTime 對(duì)象可以轉(zhuǎn)成各種格式的日期字符串。
import pendulum
dt = pendulum.local(
2022, 3, 28, 20, 10, 30)
# 下面四個(gè)最為常用
print("datetime:", dt.to_datetime_string())
print("date:", dt.to_date_string())
print("time:", dt.to_time_string())
print("iso8601:", dt.to_iso8601_string())
"""
datetime: 2022-03-28 20:10:30
date: 2022-03-28
time: 20:10:30
iso8601: 2022-03-28T20:10:30+08:00
"""
# 當(dāng)然還支持很多其它格式,不過(guò)用的不多
# 隨便挑幾個(gè)吧
print("atom:", dt.to_atom_string())
print("rss:", dt.to_rss_string())
print("w3c:", dt.to_w3c_string())
print("cookie:", dt.to_cookie_string())
print("rfc822:", dt.to_rfc822_string())
"""
atom: 2022-03-28T20:10:30+08:00
rss: Mon, 28 Mar 2022 20:10:30 +0800
w3c: 2022-03-28T20:10:30+08:00
rfc822: Mon, 28 Mar 22 20:10:30 +0800
"""
我們有時(shí)也需要判斷當(dāng)前日期是星期幾、在當(dāng)前這一年是第幾天等等,pendulum 也已經(jīng)幫我們封裝好了。
import pendulum
dt = pendulum.local(
2022, 3, 28, 20, 10, 30)
# 返回星期幾
# 注意:星期一到星期天分別對(duì)應(yīng) 1 到 7
print(dt.isoweekday()) # 1
# 返回一年當(dāng)中的第幾天
# 范圍是 1 到 366
print(dt.day_of_year) # 87
# 返回一個(gè)月當(dāng)中的第幾天
print(dt.days_in_month) # 31
# 返回一個(gè)月當(dāng)中的第幾周
print(dt.week_of_month) # 5
# 返回一年當(dāng)中的第幾周
print(dt.week_of_year) # 13
# 是否是閏年
print(dt.is_leap_year()) # False
最后就是日期的運(yùn)算,這是 pendulum 最為強(qiáng)大的地方,至于為什么強(qiáng)大,我們演示一下就知道了。
import pendulum
dt = pendulum.local(
2022, 3, 30, 20, 10, 30)
# 返回下一個(gè)月的今天
print(dt.add(months=1))
"""
2022-04-30T20:10:30+08:00
"""
# 返回上一個(gè)月的今天
# 但是上一個(gè)月是 2 月,并且是平年
# 所以最多 28 天
print(dt.add(months=-1))
"""
2022-02-28T20:10:30+08:00
"""
# 我們看到處理的非常完美
# 該方法的原型如下,年月日時(shí)分秒都是支持的,當(dāng)然還有星期也支持
"""
def add(
self,
years=0,
months=0,
weeks=0,
days=0,
hours=0,
minutes=0,
seconds=0,
microseconds=0,
):
"""
像 Python 的內(nèi)置模塊 datetime 在將日期相加的時(shí)候,最多支持到天,我們無(wú)法計(jì)算下一周、下一個(gè)月、下一年的日期。而 pendulum 則可以很方便地處理,這也是我最喜歡的一點(diǎn)。
當(dāng)然啦,add 里面的值為正,相當(dāng)于日期往后退;值為負(fù),相當(dāng)于日期往前推。
然后是兩個(gè)日期還可以做減法:
import pendulum
dt1 = pendulum.local(
2021, 1, 20, 11, 22, 33)
dt2 = pendulum.local(
2022, 3, 30, 20, 10, 30)
period = dt2 - dt1
# 返回的是 Period 對(duì)象
# 相當(dāng)于 datetime 模塊里面的 timedelta
print(period.__class__)
"""
"""
# 但是功能方面,Period 要強(qiáng)大很多
# 兩者差了多少年
print(period.in_years()) # 1
# 兩者差了多少個(gè)月
print(period.in_months()) # 14
# 兩者差了多少個(gè)星期
print(period.in_weeks()) # 62
# 兩者差了多少天
print(period.in_days()) # 434
# 兩者差了多少個(gè)小時(shí)
print(period.in_hours()) # 10424
# 兩者差了多少分鐘
print(period.in_minutes()) # 625487
# 兩者差了多少秒
print(period.in_seconds()) # 37529277
功能非常強(qiáng)大,Python 的 datetime 模塊里面的 timedelta 最多只能計(jì)算兩個(gè)日期差了多少天,而這里年月日時(shí)分秒均可。
以上就是本文的內(nèi)容,當(dāng)然 pendulum 的功能其實(shí)不止我們上面說(shuō)的那些,有興趣的話可以參考官網(wǎng),但常用的差不多就這些東西。
當(dāng)前名稱:一個(gè)好用的Python日期庫(kù)--pendulum
文章URL:http://www.fisionsoft.com.cn/article/dhsidjj.html


咨詢
建站咨詢
