1

I have a question about python's time module specifically (not asking about datetime, pytz, or any other module).

How can I find out, using the time module, whether it is currently daylight savings time (DST) in the place and date where I currently am?

The documentation seems vague on this topic. I would have thought that time.daylight would be 0 if it's currently standard time (winter) vs nonzero if it's currently daylight time (summer), based on my interpretation of how the docstring is worded: Nonzero if a DST timezone is defined.

However, it seems that this is not what time.daylight does! Currently it is winter (standard) time here, but time.daylight == 1

How can I use the time module to find out if it is currently DST? Or is this impossible? Thanks!

AlcubierreDrive
  • 3,654
  • 2
  • 29
  • 45
  • related: [Use Python to find out if a timezone currently in daylight savings time](http://stackoverflow.com/q/19774709/4279) – jfs Feb 22 '14 at 07:51

3 Answers3

2

How can I find out, using the time module, whether it is currently daylight savings time (DST) in the place and date where I currently am?

Assuming your computer is set to the correct timezone:

import time

is_dst = time.daylight and time.localtime().tm_isdst > 0 
jfs
  • 399,953
  • 195
  • 994
  • 1,670
0

This works on my system. Maybe your OS is set to DST?

some_weired_user
  • 556
  • 1
  • 5
  • 15
  • What do you mean by "my os is set to DST"? If I use a high-level module such as the one described here http://stackoverflow.com/a/12015245/402807 , it correctly reports my timezone as "PST" (Pacific Standard Time) – AlcubierreDrive Feb 22 '14 at 04:55
  • Your system may set DST automatically or it may let you do it manually. But if other modules dump the correct information, this cannot be the reason. – some_weired_user Feb 22 '14 at 05:00
  • To clarify: when you say "this works on my system", are you saying that all of these things are true for you: (1) you live in a geographic area that does indeed utilize DST (e.g. the continental USA) , (2) is it currently not DST (you are in the winter) , and (3) `time.daylight == 0` ? – AlcubierreDrive Feb 22 '14 at 05:18
  • Currently I am in Malaysia and `time.daylight == 0` which is correct. When connecting to a machine in Germany `time.daylight == 1` which is not correct. – some_weired_user Feb 22 '14 at 11:46
-1

pytz is good way to handle timezones, local time conversions and DST in python consistently. Sorry for not posting an actual example of usage but it's a good idea to go through the module docs to see the various pitfalls and use cases of DST conversion.

Will
  • 1,532
  • 10
  • 22
  • Thanks, but in my question I specifically said in the first sentence that I'm not asking about `pytz`. I agree that for most practical cases `pytz` would be the way to go, but I'm curious specifically about the `time` module, hence this question. – AlcubierreDrive Feb 22 '14 at 08:06
  • Ah. Sorry, I must have read your post a bit fast the first time. – Will Feb 23 '14 at 14:56