【问题标题】:TimeZoneInfo to TIME_ZONE_INFORMATION structureTimeZoneInfo 到 TIME_ZONE_INFORMATION 结构
【发布时间】:2010-10-31 04:34:32
【问题描述】:

嘿,我有一个 TimeZoneInfo 对象,我想从这个对象创建一个 TIME_ZONE_INFO 结构。

Bias、StandardDate 和 Daylightdate 很容易获得。但是,我在获取标准偏差和日光偏差时遇到了问题。所以问题是,我怎样才能从 TimeZOneInfo 对象中获得标准偏差,我怎样才能为日光偏差获得相同的东西(有一个 AdjustmentRule.DaylightDelta,但正如你所看到的,我需要偏移量而不是增量)。

谢谢。

【问题讨论】:

    标签: c# timezone


    【解决方案1】:

    TIME_ZONE_INFORMATION 帮助非常有用。它表示大多数时区的标准偏差为 0。对我来说,拥有一个标准偏差非零的时区没有多大意义。这不就是“标准”的意思吗?

    DaylightDelta 是标准 UTC 偏移量和日光 UTC 偏移量之间的差异。 DaylightBias 的定义方式相同,因此您的 DaylightDelta 就是您的 DaylightBias。

    我现在无法破解此问题,但我建议您使用您的时区中的数据。或者,有没有一种方法可以使用 Win32 对象获取 TIME_ZONE_INFORMATION 结构以获得适当的 TimeZoneInfo 而不是创建对象?通过在 DYNAMIC_TIME_ZONE_INFORMATION.StandardName 中指定 TimeZoneInfo.StandardName 类似GetTimeZoneInformationForYear

    【讨论】:

      【解决方案2】:

      我将此代码与使用 CrankedUp(读取 TIME_ZONE_INFORMATION)的结果进行了比较,结果在我的 Windows XP sp3 机器上是相同的。您的结果可能会有所不同。

      TimeZoneInfo.AdjustmentRule[] adjustmentRules = timeZoneInfo.GetAdjustmentRules();
      TimeZoneInfo.AdjustmentRule adjustmentRule = null;
      if (adjustmentRules.Length > 0)
      {
          // Find the single record that encompasses today's date. If none exists, sets adjustmentRule to null.
          adjustmentRule = adjustmentRules.SingleOrDefault(ar => ar.DateStart <= DateTime.Now && DateTime.Now <= ar.DateEnd);
      }
      
      double bias = -timeZoneInfo.BaseUtcOffset.TotalMinutes; // I'm not sure why this number needs to be negated, but it does.
      string daylightName = timeZoneInfo.DaylightName;
      string standardName = timeZoneInfo.StandardName;
      double daylightBias = adjustmentRule == null ? -60 : -adjustmentRule.DaylightDelta.TotalMinutes; // Not sure why default is -60, or why this number needs to be negated, but it does.
      int daylightDay = 0;
      int daylightDayOfWeek = 0;
      int daylightHour = 0;
      int daylightMonth = 0;
      int standardDay = 0;
      int standardDayOfWeek = 0;
      int standardHour = 0;
      int standardMonth = 0;
      
      if (adjustmentRule != null)
      {
          TimeZoneInfo.TransitionTime daylightTime = adjustmentRule.DaylightTransitionStart;
          TimeZoneInfo.TransitionTime standardTime = adjustmentRule.DaylightTransitionEnd;
      
          // Valid values depend on IsFixedDateRule: http://msdn.microsoft.com/en-us/library/system.timezoneinfo.transitiontime.isfixeddaterule.
          daylightDay = daylightTime.IsFixedDateRule ? daylightTime.Day : daylightTime.Week;
          daylightDayOfWeek = daylightTime.IsFixedDateRule ? -1 : (int)daylightTime.DayOfWeek;
          daylightHour = daylightTime.TimeOfDay.Hour;
          daylightMonth = daylightTime.Month;
      
          standardDay = standardTime.IsFixedDateRule ? standardTime.Day : standardTime.Week;
          standardDayOfWeek = standardTime.IsFixedDateRule ? -1 : (int)standardTime.DayOfWeek;
          standardHour = standardTime.TimeOfDay.Hour;
          standardMonth = standardTime.Month;
      }
      

      【讨论】:

      • 如何得到标准偏差?
      猜你喜欢
      • 2016-06-07
      • 1970-01-01
      • 1970-01-01
      • 2014-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      相关资源
      最近更新 更多