python - 如何确定过去的时区特定日期是否在 python 中是夏令时?

标签 python datetime time timezone

有没有一种方法可以检查特定时区在我指定的日期是否处于夏令时?

    test_dt = datetime(year=2015, month=2, day=1)
    pst = pytz.timezone('America/Los_Angeles')
    test_dt = pst.localize(test_dt) 

    # should return False
    is_day_light_saving(test_dt)        

最佳答案

只需调用 the datetime.dst() method :

def is_summer_time(aware_dt):
    assert aware_dt.tzinfo is not None
    assert aware_dt.tzinfo.utcoffset(aware_dt) is not None
    return bool(aware_dt.dst())

例子:

#!/usr/bin/env python
from datetime import datetime
import pytz # $ pip install pytz

naive = datetime(2015, 2, 1)
pacific = pytz.timezone('America/Los_Angeles')
aware = pacific.localize(naive, is_dst=None) 

print(is_summer_time(aware))

相当于:

bool(pytz.timezone('America/Los_Angeles').dst(datetime(2015, 2, 1), is_dst=None))

关于python - 如何确定过去的时区特定日期是否在 python 中是夏令时?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31146092/

相关文章:

php - 使用 MySQL 的 TIMESTAMP 与直接存储时间戳

java - 如何在 Java 8 Instant 中表示 MS-DTYP `DATETIME`?

javascript - 将毫秒转换为 yyyy-MM-dd HH :mm:ss format Javascript

python - 我的 python 脚本有什么问题?不打印变量

python - 使用 Pyinstaller 创建 .exe 错误 : Assembly amd64_Microsoft. VC90.CRT_1fc8b3b9a1e18e3b_9.0.21022.8_none not found

python - KeyError 在 Python 中使用字典

php - 格式字符串中的 strftime 编码问题

c# - 在 asp .net 中将字符串转换为日期时间格式

python - Python中读取请求参数

PHP:从时间戳生成相对日期/时间