時區是一個地理區域,所有時鐘都設定為相同的標準時間,但由於政治選擇、歷史時區變更、夏令時的差異和其他因素,不同的地點可能具有不同的時間偏移量。 Python的datetime模組和pytz函式庫分別提供了一組用於處理日期、時間和時區的類別。軟體開發中的時區管理非常重要,因為它會影響程式提供結果的準確性。本文將透過三個註解的範例,介紹如何使用datetime 和pytz 模組在Python中管理時區。
必須使用Python的datetime和pytz模組才能操作時區。時區功能由第三方套件pytz庫提供,而datetime模組則提供了用於處理日期和時間的類別。
pip install pytz pip install datetime
datetime模組提供了幾個用於處理日期和時間的類別。主要的類別有date, tzinfo, timedelta, time和datetime。
The datetime class represents a specific date and time and has several attributes, including year, month, day, hour, minute, second, and microsecond.
##. 在datetime類別中還有許多處理日期時間物件的方法。我們可以使用replace()
函數來修改一個或多個datetime物件的特徵,同時保持其他特徵不受影響。strftime()
function.#Syntax
The pytz library's timezone() function may be used to set the timezone in Python. The datetime module can utilize the timezone object that is returned by theimport pytz from datetime import datetime eastern = pytz.timezone('US/Eastern') dt = datetime.now(eastern)
However, the datetime class does not provide built-in support for timezones. That's where the pytz library comes in. The pytz module provides the timezone class, which represents a timezone object. A timezone formin, timezones timezone object the timezone object the timezones objects timezone object the timezones soo, timezones daylight saving time rules, and timezone name. The pytz module also provides several functions for working with timezones, including localize() and
normalize().function is used to set the timezone for a datetimezone for a datetimezone for a datetimezone for a datetimezone for a datetimezone for a datetimezone for a datetimezone object, while the normalize() function is used to convert a datetime object from one timezone to another.
建立datetime物件以在特定時區顯示時間
#Use
localize()Change timezone with astimezone() function
函數將datetime物件轉換為字串
使用pytz.timezone()函數建立一個時區對象,並將其指派給一個變數範例
import pytz
from datetime import datetime
# Create a timezone object for US/Eastern
eastern_tz = pytz.timezone('US/Eastern')
now = datetime.now()
now_eastern = eastern_tz.localize(now)
print(now_eastern)
2023-04-17 16:58:31.317459-04:00
, create a datetime object for the current time using
datetime.now()method of the timezone object.
使用datetime和pytz,我們可以使用datetime物件的astimezone()方法。 範例
import pytz from datetime import datetime # Create a timezone object for US/Eastern eastern_tz = pytz.timezone('US/Eastern') now = datetime.now() now_eastern = eastern_tz.localize(now) # Convert the datetime object to Pacific timezone pacific_tz = pytz.timezone('US/Pacific') now_pacific = now_eastern.astimezone(pacific_tz) print(now_pacific)
2023-04-17 13:58:41.599015-07:00
使用pytz,為US/Eastern建立一個時區物件。透過呼叫timezone()方法來建立一個目前時間的datetime物件。呼叫now()之後,使用時區物件的
localise()###函數設定datetime物件的時區。 ### ###使用###pytz.timezone()###為US/Pacific實例化另一個時區對象,並使用datetime物件的astimezone()方法將datetime物件轉換為太平洋時區。 ### ###使用時區格式化時間### ###Formatting becomes easier with the strftime() method of the datetime object.######Example####### ###import pytz from datetime import datetime # Create a timezone object for US/Eastern eastern_tz = pytz.timezone('US/Eastern') now = datetime.now() now_eastern = eastern_tz.localize(now) # Add formatting fmt = '%Y-%m-%d %H:%M:%S %Z%z' now_str = now_eastern.strftime(fmt) print(now_str)
2023-04-17 16:59:06 EDT-0400
以上是在Python中處理時區的詳細內容。更多資訊請關注PHP中文網其他相關文章!