本文介绍了以编程方式在时钟变换时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对时区和国家的时间变化感到困惑。加利福尼亚时间上午10点,纽约时间早上7点。
i am confused about timezone and countrys at time-changes. Carlifornia changes the time at 10am, NewYork at 7am.
如果加利福尼亚接下来的时间会发生变化,我如何以编程方式获取信息?
How can i programmatically get informations when the time will change next in California?
推荐答案要查找DST转换,您可以访问 Olson时区数据库,例如查找在Python中下一个DST转换的时间:
To find out DST transitions, you could access Olson timezone database e.g., to find out the time of the next DST transition, in Python:
#!/usr/bin/env python from bisect import bisect from datetime import datetime import pytz # $ pip install pytz def next_dst(tz): dst_transitions = getattr(tz, '_utc_transition_times', []) index = bisect(dst_transitions, datetime.utcnow()) if 0 <= index < len(dst_transitions): return dst_transitions[index].replace(tzinfo=pytz.utc).astimezone(tz)例如,在洛杉矶:
dt = next_dst(pytz.timezone('America/Los_Angeles')) print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z'))输出:
2014-11-02 01:00:00 PST-0800或当地时区:
from tzlocal import get_localzone # $ pip install tzlocal dt = next_dst(get_localzone()) if dt is not None: print(dt.strftime('%Y-%m-%d %H:%M:%S %Z%z')) else: print("no future DST transitions")更多推荐
以编程方式在时钟变换时间
发布评论