TimeZoneInfo到TIME_ZONE_INFORMATION结构

时间:2009-06-04 01:04:48

标签: c# timezone

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

偏见,StandardDate和Daylightdate非常容易获得。但是,我在获取standardbias和daylightbias方面遇到了问题。所以问题是,如何从TimeZOneInfo对象中获取标准偏差?如何为日光灯获取相同的东西(有一个AdjustmentRule.DaylightDelta,但正如您所见,我需要偏移而不是delta)。

感谢。

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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

我现在无法解决这个问题,但我建议您使用时区中的数据。或者,有没有办法可以使用Win32对象获取 TIME_ZONE_INFORMATION结构来获取相应的TimeZoneInfo,而不是创建对象?通过在DYNAMIC_TIME_ZONE_INFORMATION.StandardName中指定TimeZoneInfo.StandardName来获取GetTimeZoneInformationForYear之类的内容?