嘿,我有一個TimeZoneInfo對象,從這個我想創建一個TIME_ZONE_INFO結構。TimeZoneInfo到TIME_ZONE_INFORMATION結構
偏差,StandardDate和Daylightdate很容易獲得。但是,我遇到了標準偏差和日光偏差的問題。所以問題是,我如何從TimeZOneInfo對象獲得標準偏移量,以及如何獲得日光偏移量的相同值(有一個AdjustmentRule.DaylightDelta,但正如你所看到的,我需要偏移量而不是增量值)。
謝謝。
嘿,我有一個TimeZoneInfo對象,從這個我想創建一個TIME_ZONE_INFO結構。TimeZoneInfo到TIME_ZONE_INFORMATION結構
偏差,StandardDate和Daylightdate很容易獲得。但是,我遇到了標準偏差和日光偏差的問題。所以問題是,我如何從TimeZOneInfo對象獲得標準偏移量,以及如何獲得日光偏移量的相同值(有一個AdjustmentRule.DaylightDelta,但正如你所看到的,我需要偏移量而不是增量值)。
謝謝。
TIME_ZONE_INFORMATION幫助非常有用。它說大多數時區的標準偏差爲0。對我來說,擁有一個標準偏差不爲零的時區是沒有什麼意義的。這不就是「標準」的含義嗎?
DaylightDelta是標準UTC偏移和日光UTC偏移之間的差異。 DaylightBias的定義方式相同,因此您的DaylightDelta是您的DaylightBias。
我現在無法破解此操作,但我建議您使用時區中的數據。另外,是否有一種方法,你可以使用Win32對象得到的TIME_ZONE_INFORMATION結構適當的TimeZoneInfo而不是創建對象?通過在DYNAMIC_TIME_ZONE_INFORMATION.StandardName中指定TimeZoneInfo.StandardName,可以像GetTimeZoneInformationForYear那樣?
我將此代碼與使用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;
}