如何在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