/// <summary> /// Compares the transitions. /// </summary> /// <param name="x">The first transition.</param> /// <param name="y">The second transition.</param> /// <returns>A negative number if x is less than y, 0 if x and y are equal, a positive number if x is greater than y.</returns> private int CompareTransitions(TimeZoneTransition x, TimeZoneTransition y) { if (x == y) { return(0); } else if (x.GetType() == typeof(TimeZoneTransition)) { return(-1); } else if (y.GetType() == typeof(TimeZoneTransition)) { return(1); } else { AbsoluteDateTransition firstTransition = (AbsoluteDateTransition)x; AbsoluteDateTransition secondTransition = (AbsoluteDateTransition)y; return(DateTime.Compare(firstTransition.DateTime, secondTransition.DateTime)); } }
/// <summary> /// Initializes a new instance of the <see cref="TimeZoneDefinition"/> class. /// </summary> /// <param name="timeZoneInfo">The time zone info used to initialize this definition.</param> internal TimeZoneDefinition(TimeZoneInfo timeZoneInfo) : this() { this.Id = timeZoneInfo.Id; this.Name = timeZoneInfo.DisplayName; // TimeZoneInfo only supports one standard period, which bias is the time zone's base // offset to UTC. TimeZonePeriod standardPeriod = new TimeZonePeriod(); standardPeriod.Id = TimeZonePeriod.StandardPeriodId; standardPeriod.Name = TimeZonePeriod.StandardPeriodName; standardPeriod.Bias = -timeZoneInfo.BaseUtcOffset; TimeZoneInfo.AdjustmentRule[] adjustmentRules = timeZoneInfo.GetAdjustmentRules(); TimeZoneTransition transitionToStandardPeriod = new TimeZoneTransition(this, standardPeriod); if (adjustmentRules.Length == 0) { this.periods.Add(standardPeriod.Id, standardPeriod); // If the time zone info doesn't support Daylight Saving Time, we just need to // create one transition to one group with one transition to the standard period. TimeZoneTransitionGroup transitionGroup = new TimeZoneTransitionGroup(this, "0"); transitionGroup.Transitions.Add(transitionToStandardPeriod); this.transitionGroups.Add(transitionGroup.Id, transitionGroup); TimeZoneTransition initialTransition = new TimeZoneTransition(this, transitionGroup); this.transitions.Add(initialTransition); } else { for (int i = 0; i < adjustmentRules.Length; i++) { TimeZoneTransitionGroup transitionGroup = new TimeZoneTransitionGroup(this, this.transitionGroups.Count.ToString()); transitionGroup.InitializeFromAdjustmentRule(adjustmentRules[i], standardPeriod); this.transitionGroups.Add(transitionGroup.Id, transitionGroup); TimeZoneTransition transition; if (i == 0) { // If the first adjustment rule's start date in not undefined (DateTime.MinValue) // we need to add a dummy group with a single, simple transition to the Standard // period and a group containing the transitions mapping to the adjustment rule. if (adjustmentRules[i].DateStart > DateTime.MinValue.Date) { TimeZoneTransition transitionToDummyGroup = new TimeZoneTransition( this, this.CreateTransitionGroupToPeriod(standardPeriod)); this.transitions.Add(transitionToDummyGroup); AbsoluteDateTransition absoluteDateTransition = new AbsoluteDateTransition(this, transitionGroup); absoluteDateTransition.DateTime = adjustmentRules[i].DateStart; transition = absoluteDateTransition; this.periods.Add(standardPeriod.Id, standardPeriod); } else { transition = new TimeZoneTransition(this, transitionGroup); } } else { AbsoluteDateTransition absoluteDateTransition = new AbsoluteDateTransition(this, transitionGroup); absoluteDateTransition.DateTime = adjustmentRules[i].DateStart; transition = absoluteDateTransition; } this.transitions.Add(transition); } // If the last adjustment rule's end date is not undefined (DateTime.MaxValue), // we need to create another absolute date transition that occurs the date after // the last rule's end date. We target this additional transition to a group that // contains a single simple transition to the Standard period. DateTime lastAdjustmentRuleEndDate = adjustmentRules[adjustmentRules.Length - 1].DateEnd; if (lastAdjustmentRuleEndDate < DateTime.MaxValue.Date) { AbsoluteDateTransition transitionToDummyGroup = new AbsoluteDateTransition( this, this.CreateTransitionGroupToPeriod(standardPeriod)); transitionToDummyGroup.DateTime = lastAdjustmentRuleEndDate.AddDays(1); this.transitions.Add(transitionToDummyGroup); } } }
/// <summary> /// Initializes a new instance of the <see cref="TimeZoneDefinition"/> class. /// </summary> /// <param name="timeZoneInfo">The time zone info used to initialize this definition.</param> internal TimeZoneDefinition(TimeZoneInfo timeZoneInfo) : this() { this.Id = timeZoneInfo.Id; this.Name = timeZoneInfo.DisplayName; // TimeZoneInfo only supports one standard period, which bias is the time zone's base // offset to UTC. TimeZonePeriod standardPeriod = new TimeZonePeriod(); standardPeriod.Id = TimeZonePeriod.StandardPeriodId; standardPeriod.Name = TimeZonePeriod.StandardPeriodName; standardPeriod.Bias = -timeZoneInfo.BaseUtcOffset; this.periods.Add(standardPeriod.Id, standardPeriod); TimeZoneInfo.AdjustmentRule[] adjustmentRules = timeZoneInfo.GetAdjustmentRules(); TimeZoneTransition transitionToStandardPeriod = new TimeZoneTransition(this, standardPeriod); if (adjustmentRules.Length == 0) { // If the time zone info doesn't support Daylight Saving Time, we just need to // create one transition to one group with one transition to the standard period. TimeZoneTransitionGroup transitionGroup = new TimeZoneTransitionGroup(this, "0"); transitionGroup.Transitions.Add(transitionToStandardPeriod); this.transitionGroups.Add(transitionGroup.Id, transitionGroup); TimeZoneTransition initialTransition = new TimeZoneTransition(this, transitionGroup); this.transitions.Add(initialTransition); } else { for (int i = 0; i < adjustmentRules.Length; i++) { TimeZoneTransitionGroup transitionGroup = new TimeZoneTransitionGroup(this, this.transitionGroups.Count.ToString()); transitionGroup.InitializeFromAdjustmentRule(adjustmentRules[i], standardPeriod); this.transitionGroups.Add(transitionGroup.Id, transitionGroup); TimeZoneTransition transition; if (i == 0) { // If the first adjustment rule's start date in not undefined (DateTime.MinValue) // we need to add a dummy group with a single, simple transition to the Standard // period and a group containing the transitions mapping to the adjustment rule. if (adjustmentRules[i].DateStart > DateTime.MinValue.Date) { TimeZoneTransition transitionToDummyGroup = new TimeZoneTransition( this, this.CreateTransitionGroupToPeriod(standardPeriod)); this.transitions.Add(transitionToDummyGroup); AbsoluteDateTransition absoluteDateTransition = new AbsoluteDateTransition(this, transitionGroup); absoluteDateTransition.DateTime = adjustmentRules[i].DateStart; transition = absoluteDateTransition; } else { transition = new TimeZoneTransition(this, transitionGroup); } } else { AbsoluteDateTransition absoluteDateTransition = new AbsoluteDateTransition(this, transitionGroup); absoluteDateTransition.DateTime = adjustmentRules[i].DateStart; transition = absoluteDateTransition; } this.transitions.Add(transition); } // If the last adjustment rule's end date is not undefined (DateTime.MaxValue), // we need to create another absolute date transition that occurs the date after // the last rule's end date. We target this additional transition to a group that // contains a single simple transition to the Standard period. DateTime lastAdjustmentRuleEndDate = adjustmentRules[adjustmentRules.Length - 1].DateEnd; if (lastAdjustmentRuleEndDate < DateTime.MaxValue.Date) { AbsoluteDateTransition transitionToDummyGroup = new AbsoluteDateTransition( this, this.CreateTransitionGroupToPeriod(standardPeriod)); transitionToDummyGroup.DateTime = lastAdjustmentRuleEndDate.AddDays(1); this.transitions.Add(transitionToDummyGroup); } } }