/// <summary> /// Despite holding several adjustment rules, Outlook will only use the rule that is marked as effective. /// (see http://msdn.microsoft.com/en-us/library/ff960389.aspx ) /// We should ignore all other rules to be consistent with Outlook (all versions). /// </summary> public TimeZoneInfo ToTimeZoneInfo() { TimeZoneRuleStructure effectiveTimeZoneRuleStructure = GetEffectiveTimeZoneRule(); if (effectiveTimeZoneRuleStructure == null) { throw new InvalidPropertyException("No timezone rule has been marked as effective"); } TimeZoneInfo.AdjustmentRule effectiveTimeZoneRule = effectiveTimeZoneRuleStructure.ToStaticAdjustmentRule(); string standardDisplayName; string daylightDisplayName; string timezoneID = KeyName; if (KeyName == String.Empty) { // It's invalid to have an empty timezone ID, use the ID of current timezone timezoneID = TimeZoneInfo.Local.Id; } string displayName = RegistryTimeZoneUtils.GetDisplayName(timezoneID, out standardDisplayName, out daylightDisplayName); if (effectiveTimeZoneRule == null) { // no daylight savings return(TimeZoneInfo.CreateCustomTimeZone(timezoneID, effectiveTimeZoneRuleStructure.BaseUtcOffset, displayName, standardDisplayName)); } else { TimeZoneInfo.AdjustmentRule[] adjustmentRules = new TimeZoneInfo.AdjustmentRule[] { effectiveTimeZoneRule }; return(TimeZoneInfo.CreateCustomTimeZone(timezoneID, effectiveTimeZoneRuleStructure.BaseUtcOffset, displayName, standardDisplayName, daylightDisplayName, adjustmentRules)); } }
public TimeZoneInfo ToTimeZoneInfo(string timeZoneID) { // baseUtcOffset should be 120 for GMT+2:00 TimeSpan baseUtcOffset = new TimeSpan(0, -(lBias + lStandardBias), 0); string standardDisplayName; string daylightDisplayName; string displayName = RegistryTimeZoneUtils.GetDisplayName(timeZoneID, out standardDisplayName, out daylightDisplayName); // Note about stStandardDate: // If the time zone does not support daylight saving time, the wMonth member in the SYSTEMTIME structure MUST be zero if (stStandardDate.wMonth == 0) { return(TimeZoneInfo.CreateCustomTimeZone(timeZoneID, baseUtcOffset, displayName, standardDisplayName)); } else { TimeZoneInfo.AdjustmentRule rule; if (stStandardDate.wYear == 0) { // If the wYear member is zero, the date is interpreted as a relative date that occurs yearly rule = AdjustmentRuleUtils.CreateStaticAdjustmentRule(lBias, lStandardBias, lDaylightBias, stStandardDate, stDaylightDate); } else { // If the wYear member is not zero, the date is interpreted as an absolute date that only occurs once. DateTime ruleStartDate = DateTimeUtils.GetYearStart(stStandardDate.wYear).Date; DateTime ruleEndDate = DateTimeUtils.GetYearEnd(stStandardDate.wYear).Date; rule = AdjustmentRuleUtils.CreateAdjustmentRule(ruleStartDate, ruleEndDate, lBias, lStandardBias, lDaylightBias, stStandardDate, stDaylightDate); } return(TimeZoneInfo.CreateCustomTimeZone(timeZoneID, baseUtcOffset, displayName, standardDisplayName, daylightDisplayName, new TimeZoneInfo.AdjustmentRule[] { rule })); } }