Python 中的 Pendulum 模組


Pendulum 模組用於 Python 中的日期時間操作和時區管理。使用此模組,您可以管理隨時間推移的日期、時間和其他相關內容。

Pendulum 模組的主要優勢之一是其輕鬆管理時區的能力。它可以輕鬆處理來自世界各地的日期和時間,因為它可以即時在不同的時區之間進行轉換。對於處理時間敏感資料的任何人來說,Pendulum 模組都是一個強大的工具。您可以使用以下命令安裝此模組:

pip install pendulum

Pendulum 模組中有一些方法

  • add

  • subtract

  • 格式化字串

  • 週期

  • 解析字串

  • in_timezone

add() 方法

此方法將 Pendulum 例項增加一個或多個單位(年、月、日、小時、分鐘、秒等)。

示例

add(days=5) 將向 Pendulum 例項新增 5 天並返回新日期。

import pendulum

now = pendulum.now()
result = now.add(days=3)
print(result)

輸出

2023-06-15T12:59:34.170914+05:30

此輸出表示在 UTC+05:30 時區中,日期為 2023 年 6 月 15 日下午 12:59,時間為 34.170914 秒。

subtract() 方法

此方法從 Pendulum 例項中減去一個或多個單位(年、月、日、小時、分鐘、秒等)。

示例

subtract(days=3, hours=2) 將從 Pendulum 例項中減去 3 天和 2 小時並返回新時間。

import pendulum

now = pendulum.now()
result = now.subtract(days=3, hours=2)
print(result)

輸出

2023-06-09T11:21:58.461996+05:30

此輸出表示在 UTC+05:30 時區中,日期為 2023 年 6 月 9 日晚上 11:21,時間為 58.461996 秒。

format() 方法

format() 方法在 Pendulum 模組中可用,用於以指定的格式顯示日期和時間。它接收一個格式字串並使用它將 Pendulum 例項解析為指定的格式。

示例

在此示例中,我們在 Pendulum 模組中建立了不同的格式字串。

import pendulum

# Creating a Pendulum instance
dt = pendulum.datetime(2023, 6, 12, 2, 19, 35, 123456, tz='UTC')

# Format the Pendulum instance using format()
form_date = dt.format('YYYY-MM-DD')
form_time = dt.format('HH:mm:ss.SSSSSS')
form_datetime = dt.format('YYYY-MM-DD HH:mm:ss')

# Print the formatted results
print("Date formatted:",form_date)
print("Time formatted:",form_time)
print("DateTime Formatted:",form_datetime)

輸出

Date formatted: 2023-06-12 
Time formatted: 02:19:35.123456 
DateTime Formatted: 2023-06-12 02:19:35

Period() 方法

Period 物件將由 diff() 函式或當您從另一個 DateTime 例項中減去一個 DateTime 例項時返回。

示例

我們可以藉助 period() 方法建立週期例項。

import pendulum
start_date = pendulum.datetime(2022, 6, 12)
end_date = pendulum.datetime(2023, 6, 12)
period = pendulum.period(start_date, end_date)
period.days

輸出

365

在此程式中,我們使用 period() 函式查詢兩個日期時間之間的差異。

parse() 方法

pendulum 模組的 parse() 方法用於透過解析給定的日期或時間字串來建立 pendulum 例項。它自動識別各種日期和時間格式,對於將它們解析為 pendulum 物件很有用。

示例

以下是在 Pendulum 中使用 parse() 方法的示例。

import pendulum

result = pendulum.parse("2023-06-12T03:30:48", tz="UTC")
print(result)

輸出

2023-06-12T03:30:48+00:00

在此示例中,我們使用 parse() 方法建立了一個 pendulum DateTime 例項 result。我們將日期和時間字串“2023-06-12T03:30:48”作為引數傳遞給 parse()。使用 tz 引數以便可以檢查解析的 DateTime。在此示例中,我們將其設定為“UTC”時區。parse() 方法自動檢測輸入字串的格式並相應地建立 Pendulum 例項。

in_timezone() 方法

Pendulum 模組中提供了 in_timezones() 函式,用於將給定例項轉換為一個或多個時區。返回一個新的 Pendulum 例項,其中包含根據給定時區的時間和日期。

示例

以下是一個顯示如何使用 in_timezones() 方法的示例。

import pendulum

res = pendulum.now("UTC")
final_res = res.in_timezone("Asia/Kolkata")

print("Current datetime in the UTC timezone: ",res)
print("Converted datetime in the 'Asia/Kolkata' timezone: ",final_res)

輸出

Current datetime in the UTC timezone: 2023-06-14T03:53:50.575082+00:00 Converted datetime in the 'Asia/Kolkata' timezone: 2023-06-14T09:23:50.575082+05:30

在此程式中,我們首先列印“UTC”時區的當前時間,然後藉助 in_timezone 方法將其轉換為“Asia/Kolkata”時區。

結論

對於處理日期和時間的 Python 使用者來說,Pendulum 模組是一項重要的資產。其有用的功能、使用者友好的介面和輕鬆的時區相容性可以簡化您的任務並提高您的效率。

更新於: 2023-09-29

500 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.