以编程方式在时钟变换时间

编程入门 行业动态 更新时间:2024-10-10 23:21:15
本文介绍了以编程方式在时钟变换时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对时区和国家的时间变化感到困惑。加利福尼亚时间上午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")

更多推荐

以编程方式在时钟变换时间

本文发布于:2023-11-27 14:19:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1638428.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:时钟   方式   时间

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!