溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Python日期庫(kù)pendulum來處理日期和時(shí)間

發(fā)布時(shí)間:2023-04-24 14:39:40 來源:億速云 閱讀:140 作者:iii 欄目:編程語(yǔ)言

這篇文章主要介紹了如何使用Python日期庫(kù)pendulum來處理日期和時(shí)間的相關(guān)知識(shí),內(nèi)容詳細(xì)易懂,操作簡(jiǎn)單快捷,具有一定借鑒價(jià)值,相信大家閱讀完這篇如何使用Python日期庫(kù)pendulum來處理日期和時(shí)間文章都會(huì)有所收獲,下面我們一起來看看吧。

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

在使用之前需要先安裝,直接 pip install pendulum 即可。

下面來看一下用法,首先是 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 類型
# 并且?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 還專門提供了兩個(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
# 只不過時(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í)間戳或者字符串來創(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)建我們說完了,然后再來看看它們支持的操作,這也是最核心的部分。

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)然還支持很多其它格式,不過用的不多
# 隨便挑幾個(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í)候,最多支持到天,我們無法計(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í)分秒均可。

關(guān)于“如何使用Python日期庫(kù)pendulum來處理日期和時(shí)間”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對(duì)“如何使用Python日期庫(kù)pendulum來處理日期和時(shí)間”知識(shí)都有一定的了解,大家如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI