【问题标题】:Displaying Time Zones in WPF/C#. Discover Daylight Savings Time Offset在 WPF/C# 中显示时区。发现夏令时偏移
【发布时间】:2010-09-18 14:07:03
【问题描述】:

我无法理解系统注册表如何帮助我将 DateTime 对象转换为相应的 TimeZone。我有一个示例,我一直在尝试进行逆向工程,但我无法遵循 UTC 时间根据夏令时偏移的关键步骤。

我正在使用 .NET 3.5(感谢上帝),但它仍然让我感到困惑。

谢谢

编辑:附加信息:此问题用于 WPF 应用程序环境。我在下面留下的代码 sn-p 使答案示例更进一步,以获得我正在寻找的内容。

【问题讨论】:

  • 只是想我应该指出这与 WPF 无关,因此您可能希望将其从标题/描述/标签中删除。
  • 实际上,我的问题是在 WPF 应用程序中使用的,正如您在下面的回答中看到的那样。虽然是 .NET 代码,但 WPF 是最终用途,所以我在下面留下的代码 sn-p 是 C# for WPF。
  • -1。请改进标题,50% 是多余的。每个 SO 问题都可能以“有人与 X 合作过吗?”开头。接受的答案:“是的,有些人确实使用它。”

标签: c# wpf datetime timezone


【解决方案1】:

这是我在 WPF 应用程序中使用的 C# 代码 sn-p。这将为您提供您提供的时区 ID 的当前时间(根据夏令时调整)。

// _timeZoneId is the String value found in the System Registry.
// You can look up the list of TimeZones on your system using this:
// ReadOnlyCollection<TimeZoneInfo> current = TimeZoneInfo.GetSystemTimeZones();
// As long as your _timeZoneId string is in the registry 
// the _now DateTime object will contain
// the current time (adjusted for Daylight Savings Time) for that Time Zone.
string _timeZoneId = "Pacific Standard Time";
DateTime startTime = DateTime.UtcNow;
TimeZoneInfo tst = TimeZoneInfo.FindSystemTimeZoneById(_timeZoneId);
_now = TimeZoneInfo.ConvertTime(startTime, TimeZoneInfo.Utc, tst);

这是我最终得到的代码片段。感谢您的帮助。

【讨论】:

    【解决方案2】:

    您可以使用 DateTimeOffset 来获取 UTC 偏移量,因此您无需深入注册表以获取该信息。

    TimeZone.CurrentTimeZone 返回额外的时区数据,TimeZoneInfo.Local 有关于时区的元数据(例如是否支持夏令时,各种状态的名称等)。

    更新:我认为这专门回答了您的问题:

    var tzi = TimeZoneInfo.FindSystemTimeZoneById("Pacific Standard Time");
    var dto = new DateTimeOffset(2008, 10, 22, 13, 6, 0, tzi.BaseUtcOffset);
    Console.WriteLine(dto);
    Console.ReadLine();
    

    该代码在 -8 偏移处创建一个 DateTime。默认安装的时区是listed on MSDN

    【讨论】:

    • DateTimeOffset、TimeZone 和 TimeZoneInfo 是 3.0 中可用的值类型,抱歉,我的回答中并不清楚。
    • 如果你创建一个新的 DateTime 对象,你能设置它的时区吗?这就是我的大脑受伤的地方。我的理解可能不正确,TimeZone.CurrentTimeZone 返回本地机器的时区信息,但不能设置为另一个区域。去 MSDN 深入挖掘。谢谢
    • 是的,你可以,我已经用代码更新了答案,我验证了它的工作原理。
    • “那个代码创建了一个 DateTime...”应该是“那个代码创建了一个 DateTimeOffset...”而且这个解决方案没有考虑夏令时。 TimeZoneInfo.ConvertTime 将检查时间是否在 DST 范围内并自动调整。
    【解决方案3】:
    //C#.NET
        public static bool IsDaylightSavingTime()
        {
            return IsDaylightSavingTime(DateTime.Now);
        }
        public static bool IsDaylightSavingTime(DateTime timeToCheck)
        {
            bool isDST = false;
            System.Globalization.DaylightTime changes 
                = TimeZone.CurrentTimeZone.GetDaylightChanges(timeToCheck.Year);
            if (timeToCheck >= changes.Start && timeToCheck <= changes.End)
            {
                isDST = true;
            }
            return isDST;
        }
    
    
    '' VB.NET
    Const noDate As Date = #1/1/1950#
    Public Shared Function IsDaylightSavingTime( _ 
     Optional ByVal timeToCheck As Date = noDate) As Boolean
        Dim isDST As Boolean = False
        If timeToCheck = noDate Then timeToCheck = Date.Now
        Dim changes As DaylightTime = TimeZone.CurrentTimeZone _
             .GetDaylightChanges(timeToCheck.Year)
        If timeToCheck >= changes.Start And timeToCheck <= changes.End Then
            isDST = True
        End If
        Return isDST
    End Function
    

    【讨论】:

      猜你喜欢
      • 2014-08-14
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 2015-02-04
      • 2020-07-16
      • 1970-01-01
      • 2016-11-25
      • 2019-09-06
      相关资源
      最近更新 更多