Detect a transition in or out of daylight savings time with Delphi
I need to trigger an event when the PC transitions into or out of daylight savings time. I could monitor the current state with GetTimezoneInformation and check it every second waiting for a change, but I just thought there might be a simpler way that di开发者_运维知识库dn't involve continuous checking.
The Delphi TZDB helps you keep track of the algorithms on when a transition should have occurred.
--jeroen
According to this http://www.pcreview.co.uk/forums/thread-1469513.php windows doesn't send a WM_TIMECHANGE on daylight savings event. Workarounds(I didn't use any of them, so I'm not sure if it works):
- check the local time every few seconds or so (yuck)
- change your calculations to be based on GMT
- watch the HKLM\System\CurrentControlSet\Control\TimeZoneInformation\ActiveTimeBias value by using RegNotifyChangeKeyValue() (this value changes when there is a DST transition as well as when the time zone changes).
Best regards,
Radu
For the UK at least (not sure of other countries) the transition only occurs on a Sunday morning so that could help reduce the frequency of your checks to just a few hours of each week. Additionally (for the UK again) this always occurs on the last weekend of March and October so you can further reduce the period for which you need to perform checks.
If you can collect the rules for each country that you are interested in and code their logic then that will obviously be more efficient than a continuous check but of course you are then responsible for being correct rather than relying on the OS.
精彩评论