在 C# 中处理时区

srs*_*esh 0 .net c# wpf timezone

我需要实现的是,显示带有组合框(包含所有时区)的 UI,以及组合框下方的复选框。仅当时区(选定的组合框项)支持夏令时时才应显示该复选框。还可以在呈现 UI 控件时根据上次配置选中/取消选中复选框。

现在 C# 中的 TimeZoneInfo 类允许我获取时区列表(组合框的数据)。但是 TimeZoneInfo 类中的 SupportsDayLightSaving 属性在 Checkbox 被选中时显示为 true,当没有复选框或 Checkbox 未选中时为 false

那么我如何通过 C# 确定,

1.时区是否支持DayLightSavingTime(例如:印度标准时间不支持夏令时) 2.是否启用夏令时(勾选/取消勾选)时区是否支持夏令时。

编辑:如果我之前没有正确解释,这里是更详细的信息。我想做的事 :

  1. 显示/隐藏复选框(做出此决定的条件是什么)
  2. 如果显示复选框,则选中/取消选中复选框(做出此决定的条件是什么)。

PS:根据 MSDN SupportsDaylightSavingTime 属性,如果复选框被选中,该值将为 true,如果复选框未选中或时区不支持 DST,则值为 false。有了这个属性,如果值为 false,那么我无法确定是隐藏复选框还是显示它并取消选中复选框。例如:对于印度标准时间,我不应该显示复选框,为此,如果我必须依赖属性值,那么我会将其设为 false 并且我可以隐藏它。但考虑到我们有柏林时区(+1 UTC),使用控制面板设置取消选中复选框,然后我将属性值检索为 false 并应用上述逻辑将隐藏复选框,但在这里我想显示未选中状态的复选框。

编辑答案:看起来我无法正确解释问题,对此我深表歉意。经过长时间的谷歌搜索和搜索,我可以看到 .NET 没有提供 API 来支持我的需求。因此提出了一个解决方案,

检查 SupportsDaylightSavingsTime 属性是否为 true ,如果然后显示选中状态的复选框。如果上述属性值为 false ,则检查注册表中的 DynamicDaylightTimeDisabled 值 (HKLM\System\CurrentControlSet\Control\TimeZoneInformation)。因为这里的 false 并不意味着时区不支持 DST,而是也可以禁用它。如果值为 1,则显示未选中状态的复选框(因为它支持 DST 但目前已禁用)。如果值为 0 ,则不显示复选框。(因为时区不支持 DST)。

    //get the time zone info for the currently selected time zone.
        if (timeZoneInfo.SupportsDaylightSavingsTime)
        {
            //Show the checkbox.
            //Mark the checkbox state as checked.
        }
        else
        {
            //doesnt mean that the timezone doesnt support DST.
            int regValue;//Get the reg value of DynamicDaylightTimeDisabled  in 
            //location HKLM\System\CurrentControlSet\Control\TimeZoneInformation\

            if(regValue == 0)
            {
                //Donot show the checkbox.
            }
            if(regValue == 1)
            {
                //show the checkbox.
                //Mark the checkbox state as unchecked.
            }

        }
Run Code Online (Sandbox Code Playgroud)

Mat*_*int 5

您的解释中缺少什么,但我想我可以从评论中推断出您是在专门讨论此复选框:

                        时区设置

这仅在您使用TimeZoneInfo.Local. 换句话说,我总是可以这样做:

var tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
Debug.WriteLine(tzi.SupportsDaylightSavingTime);
Run Code Online (Sandbox Code Playgroud)

并且无论是否设置该复选框,tzi.SupportsDaylightSavingTime都将返回 true,因为该区域的数据支持它。

但是使用

var tzi = TimeZoneInfo.Local;
Debug.WriteLine(tzi.SupportsDaylightSavingTime);
Run Code Online (Sandbox Code Playgroud)

如果未选中该框,则结果可能为 false,即使区域数据支持它。这在 MSDN这篇优秀文章中进行了讨论

如果我理解正确,您想具体知道“夏令时自动调整时钟”是否已清除,以便您可以构建一个模仿 Windows 中的 UI?

正如您在其中一项编辑中指出的那样,如果您愿意,可以从注册表中获取它,但是您需要检查两个不同的键,而不仅仅是一个。根据文章

根据所使用的 Windows 版本,此复选框会将“DisableAutoDaylightTimeSet”或“DynamicDaylightTimeDisabled”注册表项值设置为一 (1):

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation
    "DynamicDaylightTimeDisabled"=dword:00000001`

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\TimeZoneInformation
    "DisableAutoDaylightTimeSet"=dword:00000001
Run Code Online (Sandbox Code Playgroud)

但是,有一种更简单的方法来检测这种情况:

static bool LocalDstDisabled()
{
    TimeZoneInfo localZone = TimeZoneInfo.Local;
    TimeZoneInfo realZone = TimeZoneInfo.FindSystemTimeZoneById(localZone.Id);

    return realZone.SupportsDaylightSavingTime &&
           !localZone.SupportsDaylightSavingTime;
}
Run Code Online (Sandbox Code Playgroud)