![](/img/trans.png)
[英]How to transform a DateTime to Daylight Saving Time based on the date (not the current locale) in Python
[英]How to determine if a timezone specific date in the past is daylight saving or not in python?
有没有办法在我指定的日期检查特定时区是否在夏令时?
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)
只需调用datetime.dst()
方法 :
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))
根据我的经验,时区数据更容易处理时区敏感的pandas.Timestamp()而不是datetime。 我非常确定时区灵敏度可以从日期本身推断夏令时。 通过首先将datetime转换为numpy.datetime64,将datetime转换为pandas.timestamp()是微不足道的。
Timestamp(numpy.datetime64('2012-05-01T01:00:00.000000'))
http://wesmckinney.com/blog/easy-high-performance-time-zone-handling-in-pandas-0-8-0/
python pandas TimeStamps到夏令时的本地时间字符串
在datetime,Timestamp和datetime64之间转换
您也可以尝试查看pandas源代码并弄清楚它是如何推导出tz信息的。 https://github.com/pydata/pandas/blob/master/pandas/src/datetime/np_datetime.c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.