internal Misc.CustomTimeZoneInfo ToCustomTimeZoneInfo() { if (this.daylightTime.HasTransitionTime && this.standardTime.HasTransitionTime) { Misc.AdjustmentRule adjustmentRule = Misc.AdjustmentRule.CreateAdjustmentRule( DateTime.MinValue.Date, DateTime.MaxValue.Date, -this.daylightTime.Delta, this.daylightTime.ToTransitionTime(), this.standardTime.ToTransitionTime()); return(Misc.CustomTimeZoneInfo.CreateCustomTimeZone( Guid.NewGuid().ToString(), -this.bias, "Custom time zone", "Standard time", "Daylight time", new Misc.AdjustmentRule[] { adjustmentRule })); } else { // Create no DST time zone return(Misc.CustomTimeZoneInfo.CreateCustomTimeZone( Guid.NewGuid().ToString(), -this.bias, "Custom time zone", "Standard time")); } }
public static AdjustmentRule CreateAdjustmentRule( DateTime dateStart, DateTime dateEnd, TimeSpan daylightDelta, TransitionTime daylightTransitionStart, TransitionTime daylightTransitionEnd) { AdjustmentRule adjustmentRule = new AdjustmentRule(); adjustmentRule.DateStart = dateStart; adjustmentRule.DateEnd = dateEnd; adjustmentRule.DaylightDelta = daylightDelta; adjustmentRule.DaylightTransitionStart = daylightTransitionStart; adjustmentRule.DaylightTransitionEnd = daylightTransitionEnd; return(adjustmentRule); }
/// <summary> /// Initializes this transition group based on the specified asjustment rule. /// </summary> /// <param name="adjustmentRule">The adjustment rule to initialize from.</param> /// <param name="standardPeriod">A reference to the pre-created standard period.</param> internal virtual void InitializeFromAdjustmentRule(Misc.AdjustmentRule adjustmentRule, TimeZonePeriod standardPeriod) { TimeZonePeriod daylightPeriod = new TimeZonePeriod(); // Generate an Id of the form "Daylight/2008" daylightPeriod.Id = string.Format( "{0}/{1}", TimeZonePeriod.DaylightPeriodId, adjustmentRule.DateStart.Year); daylightPeriod.Name = TimeZonePeriod.DaylightPeriodName; daylightPeriod.Bias = standardPeriod.Bias - adjustmentRule.DaylightDelta; this.timeZoneDefinition.Periods.Add(daylightPeriod.Id, daylightPeriod); this.transitionToDaylight = TimeZoneTransition.CreateTimeZoneTransition( this.timeZoneDefinition, daylightPeriod, adjustmentRule.DaylightTransitionStart); TimeZonePeriod standardPeriodToSet = new TimeZonePeriod(); standardPeriodToSet.Id = string.Format( "{0}/{1}", standardPeriod.Id, adjustmentRule.DateStart.Year); standardPeriodToSet.Name = standardPeriod.Name; standardPeriodToSet.Bias = standardPeriod.Bias; this.timeZoneDefinition.Periods.Add(standardPeriodToSet.Id, standardPeriodToSet); this.transitionToStandard = TimeZoneTransition.CreateTimeZoneTransition( this.timeZoneDefinition, standardPeriodToSet, adjustmentRule.DaylightTransitionEnd); this.transitions.Add(this.transitionToDaylight); this.transitions.Add(this.transitionToStandard); }
/// <summary> /// Converts this time zone definition into a TimeZoneInfo structure. /// </summary> /// <param name="service">The service.</param> /// <returns>A TimeZoneInfo representing the same time zone as this definition.</returns> internal Misc.CustomTimeZoneInfo ToTimeZoneInfo(ExchangeService service) { this.Validate(); Misc.CustomTimeZoneInfo result; // Retrieve the base offset to UTC, standard and daylight display names from // the last transition group, which is the one that currently applies given that // transitions are ordered chronologically. TimeZoneTransitionGroup.CustomTimeZoneCreateParams creationParams = this.transitions[this.transitions.Count - 1].TargetGroup.GetCustomTimeZoneCreationParams(); List <Misc.AdjustmentRule> adjustmentRules = new List <Misc.AdjustmentRule>(); DateTime startDate = DateTime.MinValue; DateTime endDate; DateTime effectiveEndDate; for (int i = 0; i < this.transitions.Count; i++) { if (i < this.transitions.Count - 1) { endDate = (this.transitions[i + 1] as AbsoluteDateTransition).DateTime; effectiveEndDate = endDate.AddDays(-1); } else { endDate = DateTime.MaxValue; effectiveEndDate = endDate; } // OM:1648848 Due to bad timezone data from clients the // startDate may not always come before the effectiveEndDate if (startDate < effectiveEndDate) { Misc.AdjustmentRule adjustmentRule = this.transitions[i].TargetGroup.CreateAdjustmentRule(startDate, effectiveEndDate); if (adjustmentRule != null) { adjustmentRules.Add(adjustmentRule); } startDate = endDate; } else { service.TraceMessage( TraceFlags.EwsTimeZones, string.Format( "The startDate '{0}' is not before the effectiveEndDate '{1}'. Will skip creating adjustment rule.", startDate, effectiveEndDate)); } } if (adjustmentRules.Count == 0) { // If there are no adjustment rule, the time zone does not support Daylight // saving time. result = Misc.CustomTimeZoneInfo.CreateCustomTimeZone( this.Id, creationParams.BaseOffsetToUtc, this.Name, creationParams.StandardDisplayName); } else { result = Misc.CustomTimeZoneInfo.CreateCustomTimeZone( this.Id, creationParams.BaseOffsetToUtc, this.Name, creationParams.StandardDisplayName, creationParams.DaylightDisplayName, adjustmentRules.ToArray()); } return(result); }