嘿,我有一个 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/