如何在 Python 中比較不同時區的時間?
在本文中,我們將向您展示如何使用以下方法在 Python 中將時間與不同的時區進行比較。
將給定的時區與本地時區進行比較
比較兩個時區的當前日期時間
比較具有不同時區的兩個時間
方法 1:將給定的時區與本地時區進行比較
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用 import 關鍵字匯入 datetime, pytz 模組。
使用 pytz 模組的 timezone() 函式(獲取特定位置的時區),獲取 “CET”(本地時區) 的時區並將其儲存在變數中。
使用 timezone() 函式獲取 UTC 時區,並將其儲存在另一個變數中。
使用 datetime.astimezone() 函式(datetime.astimezone() 函式用於修改 DateTime 模組中 DateTime 類的物件)將上述時間轉換為另一個時區,並將本地時區作為引數傳遞給它。
列印本地和 UTC 時區的值。
使用 if 條件語句比較本地時區的值是否等於 UTC 時區,並根據結果進行列印。
以下程式在比較本地時區與給定時區後返回使用者訊息:
示例
# importing datetime, pytz modules from datetime import datetime import pytz # Getting the local timezone localTimeZone = pytz.timezone('CET') # Getting the UTC timeZone utcTimeZone = datetime.now(pytz.utc) # format string format = '%Y:%m:%d %H:%M:%S %Z %z' # Convert the time to the local timezone local = utcTimeZone.astimezone(localTimeZone) # Getting formatted time using strftime() function print("Formatted DateTime in Local Timezone : ",local.strftime(format)) print("Formatted DateTime in UTC Timezone : ",utcTimeZone.strftime(format)) difference = int(local.strftime('%z')) difference2 = int(utcTimeZone.strftime('%z')) # Comparing Time Zones if (difference > difference2): print('UTC TimeZone is behind the Local Timezone by',local.strftime('%z'),'Hours') if(difference < difference2): print('UTC TimeZone is ahead of the Local TimeZone by',utcTimeZone.strftime('%z'),'Hours')
輸出
Formatted DateTime in Local Timezone : 2022:09:14 12:05:05 CEST +0200 Formatted DateTime in UTC Timezone : 2022:09:14 10:05:05 UTC +0000 UTC TimeZone is behind the Local Timezone by +0200 Hours
方法 2:比較兩個時區的當前日期時間
時區可能會使事情變得更加複雜,但幸運的是,我們可以使用相同的邏輯進行比較。唯一的區別是我們正在處理有感知的日期,其中包含有關其所在位置的時區的附加資訊。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用 import 關鍵字匯入 datetime(用於處理日期和時間,Python 有一個名為 datetime 的模組),pytz 模組(使我們的樸素日期變得有感知)。
使用 pytz 模組的 timezone() 函式(獲取特定位置的時區),獲取 “America/New_York” 的時區。
使用 pytz 模組的 timezone() 函式(獲取特定位置的時區),獲取 “Europe/London” 的時區。
使用 datetime.now() 函式獲取當前日期時間。
使用 localize() 函式將上述時區轉換為本地化函式。
localize() function When creating datetime-aware objects with an initial fixed datetime value, the correct function to use is localise(). The original datetime value will be retained in the resulting datetime aware object.
現在兩個時間都在相同的時區。
根據需要使用 if 條件語句比較時區。
示例
# importing datetime, pytz modules from datetime import datetime import pytz # Getting the time in America/New_York timezone timezone_newyork= pytz.timezone('America/New_York') # Getting the time in Europe/London timezone timezone_london = pytz.timezone("Europe/London") # current datetime inputDatetime = datetime.now() # Localize the given date, according to the timezone objects # Here this step converts the given two timezones to local time zones using localize() function datewith_tz_newyork = timezone_newyork.localize(inputDatetime) datewith_tz_london = timezone_london.localize(inputDatetime) # These are now, effectively no longer the same *date* after being localized print("The date and time with timezone newyork:", datewith_tz_newyork) print("The date and time with timezone london:", datewith_tz_london) difference = int(datewith_tz_newyork.strftime('%z')) difference2 = int(datewith_tz_london.strftime('%z')) # checking whether the date with different timezones are equal or NOT after they are in the same timezone if(datewith_tz_newyork > datewith_tz_london): print('Current Time in Newyork time is older than London by',(difference2- difference)/100,'hours') else: print('Current Time in London time is older than Newyork by',(difference- difference2)/100,'hours')
輸出
The date and time with timezone newyork: 2022-09-14 10:13:27.727111-04:00 The date and time with timezone london: 2022-09-14 10:13:27.727111+01:00 Current Time in Newyork time is older than London by 5.0 hours
方法 3:比較具有不同時區的兩個時間
這與前面的方法類似,只是我們提供了兩個時區的 DateTime。
示例
from datetime import datetime import pytz # Getting the time in America/New_York timezone timezone_newyork= pytz.timezone('America/New_York') # Getting the time in Europe/London timezone timezone_london = pytz.timezone("Europe/London") # input the date time of the newyork in the format Year, Month, Day, Hour, Minute, Second newyorkDateTime = datetime(2013, 3, 15, 20, 5, 10) #input date time of the london londonDateTime = datetime(2013, 3, 15, 20, 5, 10) # Localize the given date, according to the timezone objects datewith_tz_newyork = timezone_newyork.localize(newyorkDateTime) datewith_tz_london = timezone_london.localize(londonDateTime) # These are now, effectively no longer the same *date* after being localized print("The date and time with timezone newyork:", datewith_tz_newyork) print("The date and time with timezone london:", datewith_tz_london) difference = int(datewith_tz_newyork.strftime('%z')) difference2 = int(datewith_tz_london.strftime('%z')) # comparingthe date with different timezonesafter they are in the same timezone if(difference > difference2): print('Given Two Times of two different Time Zones are equal',(difference-difference2)/100,'hours') else: print('Given Two Times of two different Time Zones are not equal by',(difference2-difference)/100,'hours')
輸出
The date and time with timezone newyork: 2013-03-15 20:05:10-04:00 The date and time with timezone london: 2013-03-15 20:05:10+00:00 Given Two Times of two different Time Zones are not equal by 4.0 hours
結論
在本文中,我們學習瞭如何使用三種不同的方法比較不同時區的時間。我們還學習瞭如何將本地時間與指定的時區進行比較。
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP