Python 如何比较不同时区的时间

Python 如何比较不同时区的时间

在本文中,我们将展示如何使用以下方法比较不同时区的时间。

  • 将给定时区与本地时区进行比较

  • 比较两个时区的当前日期时间

  • 比较具有不同时区的两个时间

方法1:将给定时区与本地时区进行比较

步骤

以下是执行所需任务的算法/步骤:

  • 使用import关键字,导入datetime,pytz模块。

  • 使用pytz模块的timezone()函数(获取特定位置的时区)获取”CET”(本地时区)的时区,并将其存储在一个变量中。

  • 使用timezone()函数获取UTC时区,并将其存储在另一个变量中。

  • 使用datetime的astimezone()函数(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.
  • 现在两个时间在同一个时区

  • 根据要求使用条件语句比较时区

示例

# 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

结论

在本文中,我们学习了如何使用三种不同的方法来比较不同时区的时间。我们还学习了如何比较本地时间和指定时区的时间。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程