简体   繁体   English

如何从不同的时区获取python daytime.time之间的正确差异?

[英]How do I get the correct difference between python daytime.time from different time zones?

I have instantiated two datetime.time objects with different time zones. 我已经实例化了两个具有不同时区的datetime.time对象。 Then I try to get the difference of time between both objects. 然后,我尝试获取两个对象之间的时间差。 In this example, I have set a time in La Paz and another in Lima (10:29 both in their respective timezones). 在此示例中,我在拉巴斯和利马设置了一个时间(在各自的时区均为10:29)。 Then I used datetime.combine to try to get the difference of time. 然后,我使用datetime.combine尝试获取时间差。

from datetime import time, datetime, date
from pytz import timezone

departure_timezone = timezone('America/La_Paz')
arrival_timezone = timezone('America/Lima')
departure_time = time(hour=10, minute=29, tzinfo=departure_timezone)
arrival_time = time(hour=10, minute=29, tzinfo=arrival_timezone)

dateTimeA = datetime.combine(date.today(), departure_time)
dateTimeB = datetime.combine(date.today(), arrival_time)

diff = dateTimeB - dateTimeA

print(dateTimeA)
print(dateTimeB)

print(diff)

Then it prints 然后打印

2018-02-20 10:29:00-04:33
2018-02-20 10:29:00-05:08
0:35:00

which is not what I expected, since the time difference between La Paz and Lima is 1 hour, but instead I get 35 min. 这不是我所期望的,因为拉巴斯和利马之间的时差是1小时,但我得到了35分钟。 What do I need to change to get the expected outcome? 为了获得预期的结果,我需要更改什么?

You need to use the timezone.localize() method; 您需要使用timezone.localize()方法; the pytz timezones contain all historical timezone changes, and assigning to tzinfo will always do the wrong thing. pytz时区包含所有历史时区更改,分配给tzinfo总是会做错事。 See the documenation : 参见文档

This library only supports two ways of building a localized time. 该库仅支持构建本地化时间的两种方式。 The first is to use the localize() method provided by the pytz library. 第一种是使用pytz库提供的localize()方法。

[...] [...]

Unfortunately using the tzinfo argument of the standard datetime constructors ''does not work'' with pytz for many timezones. 不幸的是,在许多时区中,使用标准datetime构造函数的tzinfo参数对pytz

 >>> datetime(2002, 10, 27, 12, 0, 0, tzinfo=amsterdam).strftime(fmt) '2002-10-27 12:00:00 LMT+0020' 

It is safe for timezones without daylight saving transitions though, such as UTC[.] 对于没有夏令时转换的时区,例如UTC [。],这是安全的。

Neither method can be applied to a time() object; 两种方法都不能应用于time()对象; apply it to the datetime object instead: 而是将其应用于datetime对象:

dateTimeA = departure_time.tzinfo.localize(
    datetime.combine(date.today(), departure_time, tzinfo=None))
dateTimeB = arrival_time.tzinfo.localize(
    datetime.combine(date.today(), arrival_time, tzinfo=None))

The above re-uses the tzinfo reference on the departure_time time object and sets tzinfo to None , explicitly, to produce a correct timezone offset. 上述重新使用tzinfo的对参考departure_time时间对象,并且设置tzinfoNone ,明确地,以产生正确的时区偏移。

With those changes, the output becomes: 经过这些更改,输出将变为:

2018-02-20 10:29:00-04:00
2018-02-20 10:29:00-05:00
1:00:00

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM