转换和格式化时区

Converting and Formatting Time Zones

我正在处理我最糟糕的噩梦 - 时区和 DST。我已经阅读了很多关于 Whosebug 的帖子,但我仍然无法弄清楚。这是问题所在: 我正在为需要 UTC 一天的数据发出 API 请求,但我使用的系统需要在 US/Pacific 时间内发出请求。文档说:

除了令人困惑的 2017 和 2018 混合之外,没有实际参数来指定您需要的时区,但您必须调整以下格式的数据:2018-03-11T00:00:00-08:00.

对我来说,它看起来像一个 ISO 格式,但我花了很多时间试图获得 yyyy-MM-dd'T'HH:mm:ssXXX 而不是 'yyyy-MM-dd'T'HH:mm:ss.SSSXXX',但无法完成这项工作。所以我创建了以下解决方法:

def dst_calc(single_date):
    zone = pytz.timezone("US/Pacific")
    day = single_date.strftime("%Y-%m-%d")

    tdelta_1 = datetime.strptime('2:00:00', '%H:%M:%S') - datetime.strptime('1:00:00', '%H:%M:%S')
    tdelta_0 = datetime.strptime('1:00:00', '%H:%M:%S') - datetime.strptime('1:00:00', '%H:%M:%S')

    logger.info('check for DST')
    if zone.localize(datetime(single_date.year, single_date.month, single_date.day)).dst() == tdelta_1:
        logger.info('summertime')
        start = single_date.strftime("%Y-%m-%d") + "T00:00:00-07:00"
        end = single_date.strftime("%Y-%m-%d") + "T23:59:59-07:00"
    elif zone.localize(datetime(single_date.year, single_date.month, single_date.day) + timedelta(days=1)).dst() == tdelta_1:
        logger.info('beginning of summertime')
        start = single_date.strftime("%Y-%m-%d") + "T00:00:00-08:00"
        end = single_date.strftime("%Y-%m-%d") + "T23:59:59-07:00"
    elif zone.localize(datetime(single_date.year, single_date.month, single_date.day)).dst() == tdelta_0:
        logger.info('wintertime')
        start = single_date.strftime("%Y-%m-%d") + "T00:00:00-08:00"
        end = single_date.strftime("%Y-%m-%d") + "T23:59:59-08:00"

显然这仅在 US/Pacific 时区,要获得 UTC 日,我需要从开始时间减去 8 小时差和 8 个时间戳,即有 T16:00:00-08:00,但我想知道是否有更好的方法可以做到这一点的方式/包/格式化程序是一种更符合逻辑的方式。

您可以使用 datetime 的 astimezone 方法来确定正确的时间。

import datetime, pytz
now = datetime.datetime.now() # datetime.datetime(2019, 2, 12, 17, 0, 0, 0)

now.astimezone(pytz.utc)
# datetime.datetime(2019, 2, 12, 16, 0, 0, 0, tzinfo=<UTC>)

now.astimezone(pytz.timezone('US/Pacific'))
# datetime.datetime(2019, 2, 12, 8, 0, 0, 0, tzinfo=<DstTzInfo 'US/Pacific' PST-1 day, 16:00:00 STD>)