如何解决我是否在此代码中正确考虑了夏令时?
所以我在给定的时间段内从德鲁伊数据库中提取数据。例如:
time_from = '2020-11-26T06:00:00'
time_to = '2020-11-29T06:00:00'
在那段时间之间^。 但是,根据日期,时间是 MST(山地标准时间)或 MDT(山地夏令时),这决定了最后是 -06:00 还是 -07:00。
以下代码是否是考虑夏令时的正确方法?
from datetime import datetime
import pytz #must run 'pip install pytz'
#ENTER TIMES HERE FOR MAIN.PY
time_from_1='2020-11-26T06:00:00'
time_to_1='2020-11-29T06:00:00'
#CHECKING MST/MDT AND UPDATING TIMES IF NECESSARY
naive = datetime.strptime(time_from_1,'%Y-%m-%dT%H:%M:%S')
mountain = pytz.timezone('America/Denver')
time_from_aware = mountain.localize(naive,is_dst=None)
naive = datetime.strptime(time_to_1,'%Y-%m-%dT%H:%M:%S')
mountain = pytz.timezone('America/Denver')
time_to_aware = mountain.localize(naive,is_dst=None)
def is_dst(aware_dt):
assert aware_dt.tzinfo is not None
assert aware_dt.tzinfo.utcoffset(aware_dt) is not None
return bool(aware_dt.dst())
if is_dst(time_from_aware)==True:
time_from=time_from_1+'-06:00'
else:
time_from=time_from_1+'-07:00'
if is_dst(time_to_aware)==True:
time_to=time_to_1+'-06:00'
else:
time_to=time_to_1+'-07:00'
print(time_from)
print(time_to)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。