这个问题询问DST是否启用,但答案只是说明当前日期/时间是否在DST规则内,因此应进行调整,但操作系统的设置表示要保持标准时区的时间。
如何从C#获取“自动调整夏令时时钟”?
bool hasDST = TimeZoneInfo.Local.SupportsDaylightSavingTime;
所选时区不使用夏令时(如亚利桑那州和夏威夷)。
所选时区使用夏令时,但用户已清除“自动调整夏令时时钟”复选框。
如果您特别想知道用户是否禁用了通常支持它的时区的夏令时,则应这样做:
bool actuallyHasDST = TimeZoneInfo.Local.SupportsDaylightSavingTime;
bool usuallyHasDST = TimeZoneInfo.FindSystemTimeZoneById(TimeZoneInfo.Local.Id)
.SupportsDaylightSavingTime;
bool dstDisabled = usuallyHasDST && !actuallyHasDST;
dstDisabled
变量仅在用户明确取消“自动调整夏令时”复选框时为 true。如果该区域不支持 DST,因此该框不存在,则 dstDisabled
为 false。
这是如何工作的?
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.
TimeZoneInfo.Local
的备注部分。TimeZoneInfo.Local.SupportsDaylightSavingTime
不会更改,因此必须进行缓存。使用注册表将获得最新的信息。也可以通过 WMI 获取此信息。 - Mike ZborayTimeZoneInfo.Local
之前调用TimeZoneInfo.ClearCachedData()
。注册表检查是最直接的方法。如果您能将WMI示例发布为答案,我会很高兴看到它 :)。 - Matt Johnson-Pint
CultureInfo
作为可能性会有所帮助,我的错。 - C.Evenhuis