如何在Python中比較不同時區的時間?
在本文中,我們將向您展示如何使用以下方法在Python中比較不同時區的時間。
將給定時區與本地時區進行比較
比較兩個時區的當前日期時間
比較具有不同時區的兩個時間
方法一:將給定時區與本地時區進行比較
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用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
方法二:比較兩個時區的當前日期時間
時區可能會使事情變得更復雜,但幸運的是,我們可以使用相同的邏輯進行比較。唯一的區別是我們正在處理已知日期,其中包含有關其所在時區附加資訊。
演算法(步驟)
以下是執行所需任務的演算法/步驟:
使用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
方法三:比較具有不同時區的兩個時間
它與先前的方法類似,只是我們提供了兩個時區的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
結論
在本文中,我們學習瞭如何使用三種不同的方法比較不同時區的時間。我們還學習瞭如何將本地時間與指定時區進行比較。
資料結構
網路
關係資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP