如何在 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() 函数 当创建具有初始固定日期时间值的日期时间感知对象时,正确的函数是 localise()。原始日期时间值将保留在生成的日期时间感知对象中。
现在两个时间都在同一时区
使用 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:比较两个不同时区的时间
与上一种方法类似,只是我们提供了两个时区的日期时间。
示例
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
结论
在本文中,我们学习了如何使用三种不同的方法比较不同时区的时间。我们还学习了如何将本地时间与指定时区进行比较。
相关文章
有用资源
python 参考教程 - 该教程包含有关 python 的更多信息:https://www.cainiaomax.com/python/

