现在是使用 DST 的特定时区吗? [英] Is a specific timezone using DST right now?

查看:32
本文介绍了现在是使用 DST 的特定时区吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在如何让我的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆