gpt4 book ai didi

python - Django timezone.make_aware 为 2014-10-26 1 :45:00 引发了 AmbiguousTimeError

转载 作者:太空狗 更新时间:2023-10-30 01:14:55 26 4
gpt4 key购买 nike

我发现了一些奇怪的事情。这里有一些例子。

from django.utils import timezone
value = u'2014-10-26 01:45:00'
#I know that a variable has 'Europe / Moscow' timezone. Let's tell Django about it.
TZ = timezone.pytz.timezone('Europe/Moscow')
d = timezone.datetime.strptime(value,'%Y-%m-%d %H:%M:%S')
print timezone.make_aware(d,TZ)
#raised AmbiguousTimeError: 2014-10-26 01:45:00

然后乐趣就开始了

print timezone.make_aware(d+timezone.timedelta(minutes=15),TZ)
#out: 2014-10-26 02:00:00+03:00
print timezone.make_aware(d+timezone.timedelta(minutes=14),TZ)
#raised AmbiguousTimeError
print timezone.make_aware(d-timezone.timedelta(minutes=46),TZ)
#out: 2014-10-26 00:59:00+04:00
print timezone.make_aware(d-timezone.timedelta(minutes=45),TZ)
#raised AmbiguousTimeError

所以 AmbiguousTimeError 在 2014-10-26 00:59:00 和 2014-10-26 02:00:00 之间引发

为什么?又是如何解决的?

最佳答案

timezon.make_aware(d, TZ) 等同于 TZ.localize(d, is_dst=None) 会在不明确的时间引发错误:2014 -10-26 01:45:00 在欧洲/莫斯科时区出现两次:

# Europe/Moscow               UTC                           timestamp
2014-10-26 00:45:00 MSK+0400; 2014-10-25 20:45:00 UTC+0000; 1414269900
2014-10-26 01:00:00 MSK+0400; 2014-10-25 21:00:00 UTC+0000; 1414270800
2014-10-26 01:15:00 MSK+0400; 2014-10-25 21:15:00 UTC+0000; 1414271700
2014-10-26 01:30:00 MSK+0400; 2014-10-25 21:30:00 UTC+0000; 1414272600
2014-10-26 01:45:00 MSK+0400; 2014-10-25 21:45:00 UTC+0000; 1414273500
2014-10-26 01:15:00 MSK+0300; 2014-10-25 22:15:00 UTC+0000; 1414275300
2014-10-26 01:30:00 MSK+0300; 2014-10-25 22:30:00 UTC+0000; 1414276200
2014-10-26 01:45:00 MSK+0300; 2014-10-25 22:45:00 UTC+0000; 1414277100
2014-10-26 02:00:00 MSK+0300; 2014-10-25 23:00:00 UTC+0000; 1414278000

注意:UTC 偏移量在凌晨 2 点从 +0400 更改为 +0300 (Федеральный закон от 21 июля 2014 г. N 248-ФЗ)。

为避免异常,您可以调用 TZ.localize(d)(注意:没有 is_dst=None),它适用于现有的非歧义时间但可能对于不存在或不明确的时间失败(返回错误答案)。

如果pytz Bug #1378150: Enhance support for end-of-DST-like ambiguous time是固定的那么你可以使用 TZ.localize(d, is_dst=True), TZ.localize(d, is_dst=False) 相应地获取转换前后的时间.

如果错误未修复,您可以使用我在 Parsing of Ordered Timestamps in Local Time (to UTC) While Observing Daylight Saving Time 中的回答获取过渡之后的时间:

# `naive` is a naive datetime object in local (Europe/Moscow) time
if tz.localize(naive, is_dst=False) == tz.localize(naive, is_dst=True):
# Example: 2014/10/26 in Europe/Moscow timezone
# ambiguous time but is_dst=False/True yield the same result
# i.e., tz.localize() can't help, find UTC time manually
#NOTE: assume there is no other changes to UTC offset today (local.day)
new_offset = tz.localize(naive + timedelta(1), is_dst=None).utcoffset()
assert tz.localize(naive).utcoffset() != new_offset
utc = (naive - new_offset).replace(tzinfo=pytz.utc)
local = utc.astimezone(tz)

关于python - Django timezone.make_aware 为 2014-10-26 1 :45:00 引发了 AmbiguousTimeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26465059/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com