使用python确定时间戳是否在夏令时之下 [英] Using python to determine if a timestamp is under daylight savings time
本文介绍了使用python确定时间戳是否在夏令时之下的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这不起作用:
t = os.path.getmtime(filename)
dTime = datetime.datetime.fromtimestamp(t)
justTime = dTime.timetuple()
if justTime.tm_isdst == 0 :
tDelta = datetime.timedelta(hours=0)
else:
tDelta = datetime.timedelta(hours=1)
实际发生的是有条件的
我试图使python调用与基于c的调用的行为相匹配。
I am trying to make a python call match the behavior of a c-based call.
推荐答案
要了解给定的时间戳是否对应于当地时区的夏令时(DST):
To find out whether a given timestamp corresponds to daylight saving time (DST) in the local time zone:
import os
import time
timestamp = os.path.getmtime(filename)
isdst = time.localtime(timestamp).tm_isdst > 0
可能会失败。要解决这些问题,您可以使用 pytz
模块对tz数据库进行可移植的访问:
It may fail. To workaround the issues, you could get a portable access to the tz database using pytz
module:
from datetime import datetime
import tzlocal # $ pip install tzlocal
local_timezone = tzlocal.get_localzone() # get pytz tzinfo
isdst = bool(datetime.fromtimestamp(timestamp, local_timezone).dst())
这篇关于使用python确定时间戳是否在夏令时之下的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文