3

我正在使用 python 2.7.3 和 pytz。

对于描述一个地区的给定时区(例如 America/New_York),我想知道该时区是否在一年中的部分时间遵守 DST。我关心当前有效的时区定义。重新表述这个问题,鉴于当前的时区定义,这个时区观察者会在接下来的 365 天内(或停止观察它)吗?

此外,我想知道当它观察 DST 时这个时区与 UTC 的偏移量是多少,以及它不观察 DST 时的偏移量是多少。

最后,我想知道给定的时区目前是否正在观察 DST。

最终目标是生成这样的列表:

Name                    Observes DST     DST Offset     non-DST Offset   Presently DST
--------------------------------------------------------------------------------------
America/New_York        Yes              6              5                No

我不知道如何从 pytz 获取这些信息。

4

2 回答 2

1

据我所知,没有公共接口。您可以检查(及其子类)实例_utc_transition_times上存在的属性。DstTzInfo

于 2012-12-12T15:59:46.580 回答
0

我能够使用此功能解决此问题:

def get_tz_dst_info(tz):
    """
    Gets a 3-tuple of info about DST for a timezone. The returned elements are:
    - a boolean if this timezone observes DST
    - a Decimal UTC offset when not in DST
    - a Decimal UTC offset when in DST

    >>> from pytz import timezone
    >>> get_tz_dst_info(timezone('America/New_York'))
    (True, Decimal('-4'), Decimal('-5'))
    >>> get_tz_dst_info(timezone('Europe/Paris'))
    (True, Decimal('2'), Decimal('1'))
    >>> get_tz_dst_info(timezone('UTC'))
    (False, Decimal('0'), Decimal('0'))
    """
    dec_int_offset = timedelta_utc_offset_to_decimal(
        tz.utcoffset(DECEMBER_DATE)
    )
    jul_int_offset = timedelta_utc_offset_to_decimal(tz.utcoffset(JULY_DATE))
    jul_dst = tz.dst(JULY_DATE)
    dec_dst = tz.dst(DECEMBER_DATE)

    dst_offset = dec_int_offset
    non_dst_offset = jul_int_offset
    if jul_dst >= timedelta(seconds=0):
        dst_offset = jul_int_offset
        non_dst_offset = dec_int_offset
    elif dec_dst >= timedelta(seconds=0):
        dst_offset = jul_int_offset
        non_dst_offset = dec_int_offset
    return (dec_int_offset != jul_int_offset,
            non_dst_offset,
            dst_offset)
于 2013-03-26T22:20:15.200 回答