How do I check if daylight saving time is in effect?
+6
A:
You can use time.localtime
and look at the tm_isdst
flag in the return value.
>>> import time
>>> time.localtime()
(2010, 5, 21, 21, 48, 51, 4, 141, 0)
>>> _.tm_isdst
0
Using time.localtime()
, you can ask the same question for any arbitrary time to see whether DST would be (or was) in effect for your current time zone.
Greg Hewgill
2010-05-21 09:48:41
It may be a bit more clear to use time.daylight property. It will return a non-zero result if your current time zone has DST.
brian buck
2010-05-21 15:33:30
@brian buck: That's different though. For a given time zone, `time.daylight` is constant because a daylight zone either exists or it doesn't. On the other hand, the `tm_isdst` flag reflects whether the given time is *within* the DST start and end dates.
Greg Hewgill
2010-05-21 19:28:08