3

In Windows 7, Time Zone Settings, you can enable or disable the "Automatically adjust clock for Daylight Saving Time". If this is disabled, then the PC clock will always show standard time, even if the timezone is set to a timezone that follows Daylight Saving Time.

This question asks if DST is enable, but the answer only says if the current date/time is within the DST rules, so it should be adjusted, but the settings for the OS says to keep the time in the standard time zone.

How do I get the "Automatically adjust clock for Daylight Saving Time" from C#

Community
  • 1
  • 1
Robert Deml
  • 12,390
  • 20
  • 65
  • 92
  • 1
    In that question there's another answer that shows how to get the setting from the registry. Is anything wrong with that? I never heard of a managed API to get that. – Marcel N. Aug 12 '14 at 20:17
  • http://stackoverflow.com/questions/1252314/how-to-get-timezone-from-properties-in-cultureinfo also deals with the same issue – C.Evenhuis Aug 12 '14 at 20:24
  • Marvel N. - Is going to the registry from C# OK? I will if that is the only solution, but I'd rather not touch the registry. Every hack that I've tried that said "just go to this key in the registry ..." has never worked. – Robert Deml Aug 12 '14 at 20:30
  • @C.Evenhuis - no it doesn't. Not even close. – Matt Johnson-Pint Aug 16 '14 at 03:51
  • @MattJohnson You're right, when I posted that I figured ruling out `CultureInfo` as a possibility would help a bit, my bad – C.Evenhuis Aug 16 '14 at 07:28

1 Answers1

3

If you just want to know whether DST is supported by the local time zone, use:

bool hasDST = TimeZoneInfo.Local.SupportsDaylightSavingTime;

This will be false in either of these conditions:

  • The selected time zone does not use DST (such as Arizona and Hawaii)

  • The selected time zone uses DST but the user has cleared the "Automatically Adjust Clock for Daylight Saving Time" checkbox.

If you specifically want to know if the user has disabled DST for a time zone that normally supports it, then do this:

bool actuallyHasDST = TimeZoneInfo.Local.SupportsDaylightSavingTime;
bool usuallyHasDST = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id)
                                 .SupportsDaylightSavingTime;
bool dstDisabled = usuallyHasDST && !actuallyHasDST;

The dstDisabled variable will be true only when the user has specifically cleared the "Automatically Adjust Clock for Daylight Saving Time" checkbox. If the box doesn't exist because the zone doesn't support DST to begin with, then dstDisabled will be false.

How does this work?

  • Windows stores the chosen time zone settings in the registry at:

    HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation
    
  • The DynamicDaylightTimeDisabled key is set to 1 when the box is cleared. Otherwise it is set to 0.

    One of the answers in the other question you mentioned specifically checked for this value, which is also an acceptable solution.

  • Calling TimeZoneInfo.Local takes into account all of the information in that key.

  • Looking up the time zone by the Id does not take into account any of the information in the registry, other than the Id itself, which is stored in the TimeZoneKeyName value.

  • By comparing the registry-created information against the looked-up information, you can determine whether DST has been disabled.

Note that this is also well described in the remarks section of the MSDN documentation for TimeZoneInfo.Local.

Community
  • 1
  • 1
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • One thing I noticed was that `TimeZoneInfo.Local.SupportsDaylightSavingTime` did not change if the setting was changed while the program was running so it must be cached. Using the registry will get the most update to date information. It is also possible to get this through WMI. – Mike Zboray Aug 16 '14 at 04:25
  • @mikez - Correct. You would need to call `TimeZoneInfo.ClearCachedData()` before checking `TimeZoneInfo.Local` if you were concerned about that. The registry check is the most direct way. I'd love to see the WMI example if you could post as an answer. :) – Matt Johnson-Pint Aug 16 '14 at 04:34