为什么 django.utils.timezone.make_aware 和 pytz.localize 在处理 DST 时返回不同的结果?

Why are django.utils.timezone.make_aware and pytz.localize returning different results when dealing with DST?

我正在使用 django 1.11 和 pytz 2018.6

我在理解 django 如何处理 DST 时遇到一些问题。

我的主要问题是将日期 2018-11-04 00:00:00 本地化为 America/Sao_Paulo 时区。根据最新的 pytz 版本,这是 2018 年该时区 DST 开始的日期。

好吧,在我的应用程序中,当我尝试本地化提到的日期时,我开始看到 pytz.exceptions.NonExistentTimeError 异常。以下代码重现此异常:

import os
import datetime
import django
import pytz
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings")
django.setup()
from django.utils.timezone import make_aware

sp = pytz.timezone('America/Sao_Paulo')
dst_start_date = datetime.datetime(2018, 11, 4, 0, 0, 0)
make_aware(dst_start_date, sp)
# Exception raised: pytz.exceptions.NonExistentTimeError: 2018-11-04 00:00:00

但是,如果我尝试使用 pytz.localize 而不是 make_aware 进行本地化,我会得到不同的结果:

sp.localize(dst_start_date) # Returns 2018-11-04 00:00:00-03:00

我预计在尝试本地化时会收到相同的异常。但它没有引发异常,实际上返回了错误的结果(-03:00 偏移量是在我们不在 DST 时。在特定日期,我期望 2018-11-04 00:00:00-03:00 日期转换为 2018-11-04 00:00:00-02:00).

这让我感到困惑,因为阅读 django.utils.timezone 中的 make_aware 代码我理解调用了相同的 pytz.tzinfo.localize 方法。

# django.utils.timezone
def make_aware(value, timezone=None, is_dst=None):
    """
    Makes a naive datetime.datetime in a given time zone aware.
    """
    if timezone is None:
        timezone = get_current_timezone()
    if hasattr(timezone, 'localize'):
        # This method is available for pytz time zones.
        return timezone.localize(value, is_dst=is_dst)
    else:
        # Check that we won't overwrite the timezone of an aware datetime.
        if is_aware(value):
            raise ValueError(
                "make_aware expects a naive datetime, got %s" % value)
        # This may be wrong around DST changes!
        return value.replace(tzinfo=timezone)

为什么两个结果不同?为什么我在手动尝试将日期 2018-11-04 00:00:00 本地化为 America/Sao_Paulo 时没有出现异常?

在尝试此代码之前,请确保您拥有最新的 pytz 版本 (pip install pytz --upgrade),因为我们今年的 DST 日期发生了变化。

区别在于传递给localizeis_dst参数。当您自己调用它但将其关闭时,它默认为 False。在您发布的 make_aware 代码中,它默认为 None.