现在是使用 DST 的特定时区吗?

编程入门 行业动态 更新时间:2024-10-26 20:24:55
本文介绍了现在是使用 DST 的特定时区吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我现在如何让我的 python 脚本检查是否使用 DST 存储在变量中的特定时区?我的服务器设置为 UTC.所以我有比如说

How would I get my python script to check whether or not a specific timezone that is stored in a variable using DST right now? My server is set to UTC. So I have say for instance

zonename = Pacific/Wallis

我想运行关于它现在是否正在使用 DST 的查询,并让回复返回为真或假.

I want to run the query about if it is using DST right now and have the reply come back as either true of false.

推荐答案

from pytz import timezone
from datetime import datetime

zonename = "Pacific/Wallis"
now = datetime.now(tz=timezone(zonename))
dst_timedelta = now.dst()
### dst_timedelta is offset to the winter time, 
### thus timedelta(0) for winter time and timedelta(0, 3600) for DST; 
### it returns None if timezone is not set

print "DST" if dst_timedelta else "no DST"

替代方法是使用:

now.timetuple().tm_isdst 

可以有 3 个值之一:0 表示没有 DST,1 表示 DST,-1 表示未设置时区.

Which can have one of 3 values: 0 for no DST, 1 for DST and -1 for timezone not set.

这篇关于现在是使用 DST 的特定时区吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

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

发布评论

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

>www.elefans.com

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