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!

有帮助吗?

解决方案

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 

其他提示

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

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.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top