如何在Python中比较不同时区的时间?
在本文中,我们将展示如何使用以下方法在Python中将时间与不同的时区进行比较。
- 将给定时区与本地时区进行比较
-
比较两个时区的当前日期时间
-
比较具有不同时区的两个时间
方法一:将给定时区与本地时区进行比较
算法(步骤)
以下是执行所需任务的算法/步骤:
- 使用import关键字导入datetime,pytz模块。
-
使用pytz模块的timezone()函数(获取特定位置的时区)获取“CET”(local TimeZone)的时区,并将其存储在一个变量中。
-
使用timezone()函数获取UTC时区并将其存储在另一个变量中。
-
通过将本地时区作为参数传递给datetime.astimezone()函数(datetime.astimezone()函数用于修改DateTime模块中DateTime类的对象)将上述时间转换为另一个时区。
-
打印本地和UTC时区的值。
-
使用if条件语句比较本地时区的值是否等于UTC时区,并根据其打印。
以下程序返回用户消息,比较本地时区与给定时区。
示例
#导入datetime,pytz模块
from datetime import datetime
import pytz
#获取本地时区
localTimeZone = pytz.timezone('CET')
#获取UTC时区
utcTimeZone = datetime.now(pytz.utc)
#格式化字符串
format = '%Y:%m:%d %H:%M:%S %Z %z'
#将时间转换为本地时区
local = utcTimeZone.astimezone(localTimeZone)
#使用strftime()函数获取格式化的时间
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'))
#比较时区
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')
输出
在本地时区格式化的日期时间:2022:09:14 12:05:05 CEST +0200
在UTC时区格式化的日期时间:2022:09:14 10:05:05 UTC +0000
UTC时区比当地时区慢+0200小时
方法2:比较两个时区的当前日期时间
时区可能会让事情变得更加复杂,但是值得庆幸的是,我们可以使用同样的逻辑进行比较。唯一的区别在于,我们使用的是特定于时区的日期,其中包含有关它们所在时区的其他信息。
算法(步骤)
以下是要执行所需任务的算法/步骤 –
- 使用import关键字来导入datetime(为了处理日期和时间,Python有一个名为datetime的模块)、pytz模块(为了使我们的naive日期变成特定于时区的日期对象)。
-
使用pytz模块的timezone()函数(获取特定位置的时区)获取“America/New_York”的时区。
-
使用pytz模块的timezone()函数(获取特定位置的时区)获取“Europe/London”的时区。
-
使用datetime.now()函数提供当前日期时间。
-
使用localize()函数将上述时区转换为本地化函数。
localize()函数
在创建具有初始固定日期时间值的datetime-aware对象时,要使用的正确函数是localise()。原始日期时间值将保留在生成的datetime aware对象中。
-
现在两个时间都在同一个时区。
-
根据要求使用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。
示例
从 datetime 导入 datetime
导入 pytz
# 获取美国/纽约时区的时间
timezone_newyork= pytz.timezone('America/New_York')
# 获取欧洲/伦敦时区的时间
timezone_london = pytz.timezone("Europe/London")
# 以年、月、日、小时、分钟、秒的格式输入纽约的日期时间
newyorkDateTime = datetime(2013, 3, 15, 20, 5, 10)
#输入伦敦的日期时间
londonDateTime = datetime(2013, 3, 15, 20, 5, 10)
# 根据时区对象将给定的日期本地化
datewith_tz_newyork = timezone_newyork.localize(newyorkDateTime)
datewith_tz_london = timezone_london.localize(londonDateTime)
# 这些现在,在本地化之后,实际上不再是相同的*日期*
打印("具有纽约时区的日期和时间:" , datewith_tz_newyork)
打印("具有伦敦时区的日期和时间:" , datewith_tz_london)
difference = int(datewith_tz_newyork.strftime('%z'))
difference2 = int(datewith_tz_london.strftime('%z'))
# 将位于不同时区的日期进行比较
如果(difference > difference2):
打印('给定的两个不同时区的时间相等',(difference-difference2)/100,'小时')
否则:
打印('给定的两个不同时区的时间不相等',(difference2-difference)/100,'小时')
结果
具有纽约时区的日期和时间:2013-03-15 20:05:10-04:00
具有伦敦时区的日期和时间:2013-03-15 20:05:10+00:00
给定的两个不同时区的时间不相等4.0小时
结论
在本文中,我们学习了如何使用三种不同的方法来比较不同时区的时间。我们还学习了如何将本地时间与指定的时区进行比较。
极客教程