我需要找到一种简单的方法来了解本地机器的"自动调整夏令时时钟"选项是否已启用。如果该选项打开,我需要知道它当前是否已应用(即系统中当前是否为DST)。提前感谢
您可以找到当前系统默认时区,以及它当前是否使用夏令时(夏令时),如下所示(.NET 3.5以后版本):
TimeZoneInfo zone = TimeZoneInfo.Local;
if (zone.SupportsDaylightSavingTime)
{
Console.WriteLine("System default zone uses DST...");
Console.WriteLine("In DST? {0}", zone.IsDaylightSavingTime(DateTime.UtcNow));
}
else
{
Console.WriteLine("System default zone does not use DST.");
}
另一个选项可能是DateTime.IsDaylightSavingTime
方法。查看MSDN。
if (DateTime.Now.IsDaylightSavingTime())
Console.WriteLine("Daylight Saving");
else
Console.WriteLine("No Daylight Saving");
您可以读取注册表以确定是否选中了复选框。读取此密钥,
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformationDynamicDaylightTimeDisabled
=0或1(禁用)
所以类似于:
Dim retval As Object = Microsoft.Win32.Registry.GetValue("HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTimeZoneInformation", "DynamicDaylightTimeDisabled", 0)
If retval IsNot Nothing Then
Select Case CInt(retval)
Case 0
Trace.WriteLine("Automatically adjust clock for Daylight Saving Time is checked")
Case 1
Trace.WriteLine("Automatically adjust clock for Daylight Saving Time is NOT checked")
End Select
End If
下面是C#中的另一个例子
private static bool IsDayLightSavingsEnabled()
{
try
{
var result = Registry.GetValue(@"HKEY_LOCAL_MACHINESYSTEMCurrentControlSetControlTimeZoneInformation", "DynamicDaylightTimeDisabled", 1);
return !Convert.ToBoolean(result); //0 - Checked/enabled, 1 - Unchecked/disabled
}
catch
{ }
return false;
}