문제

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