我想在Python中推断日期是否是一年中由于DST(夏令时)而改变小时的实际日期。
使用该库,pytz您可以本地化日期时间,并且正确完成实际的 DST 更改。dst()此外,库的方法datetime允许您推断实际日期是夏季还是冬季(示例)。但是,我想推断 DST 更改的实际日期。
具体来说,我需要一个函数(例如is_dst_change(date, timezone)),它接收日期并仅在一年中确实有小时变化的那些日子返回 True 。例如:
import pytz
import datetime
def is_dst_change(day, timezone):
# Localize
loc_day = pytz.timezone(timezone).localize(day)
# Pseudocode: Infer if a date is the actual day in which the dst hour change is made
if loc_day is dst_change_day:
return True
else:
return False
# In the timezone 'Europe/Madrid', the days in which the DST change is made in 2021 are 28/03/2021 and 31/10/2021
is_dst_change(day=datetime.datetime(year=2021, …Run Code Online (Sandbox Code Playgroud)