c# - TimeZoneInfo 到 TIME_ZONE_INFORMATION 结构

标签 c# timezone

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

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

谢谢。

最佳答案

我将此代码与使用 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;
}

关于c# - TimeZoneInfo 到 TIME_ZONE_INFORMATION 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/948091/

相关文章:

c# - 如何显示 Servicestack api 站点的 "cover page"/重定向到另一个网站

c# - 如何在 Webbrowser 控件中禁用 “Security Warning” 窗口

c# - 如何在 C# 中从 CultureInfo 获取 "UK"

c# - 如何在C#中用指针编写C结构体

c# - 在 Window 服务中处理 WebApp.Start 实例的正确位置?

Python 时区偏移错误?

sql - 带时区的 Postgres 时间戳转换

c++ - 如何找到当前的系统时区?

python - 如何在Windows中使用python更改系统时区?

ruby-on-rails - 为什么 zone.utc_offset 和 zone.now.utc_offset 之间存在差异?