private static byte[] GenerateBlobFromRuleGroups(string keyName, IList <ExTimeZoneRuleGroup> groups)
        {
            O12TimeZoneFormatter.ExchangeTimeZoneHeader exchangeTimeZoneHeader = new O12TimeZoneFormatter.ExchangeTimeZoneHeader(keyName, (ushort)groups.Count);
            int size = exchangeTimeZoneHeader.GetSize();
            int num  = size + O12TimeZoneFormatter.ExchangeTimeZoneRule.Size * groups.Count;

            byte[] array = new byte[num];
            int    num2  = exchangeTimeZoneHeader.ToBytes(new ArraySegment <byte>(array, 0, size));

            foreach (ExTimeZoneRuleGroup exTimeZoneRuleGroup in groups)
            {
                O12TimeZoneFormatter.TzRuleFlags flag = O12TimeZoneFormatter.TzRuleFlags.None;
                if (groups.Count == 1 || (exTimeZoneRuleGroup.EffectiveUtcStart <= DateTime.UtcNow && exTimeZoneRuleGroup.EffectiveUtcEnd > DateTime.UtcNow))
                {
                    flag = O12TimeZoneFormatter.TzRuleFlags.EffectiveTimeZone;
                }
                int year;
                if (exTimeZoneRuleGroup.EffectiveUtcStart != DateTime.MinValue)
                {
                    ExTimeZoneRule ruleForUtcTime = exTimeZoneRuleGroup.GetRuleForUtcTime(exTimeZoneRuleGroup.EffectiveUtcStart);
                    year = ruleForUtcTime.FromUtc(exTimeZoneRuleGroup.EffectiveUtcStart).Year;
                }
                else
                {
                    year = exTimeZoneRuleGroup.EffectiveUtcStart.Year;
                }
                REG_TIMEZONE_INFO regInfo = TimeZoneHelper.RegTimeZoneInfoFromExTimeZoneRuleGroup(exTimeZoneRuleGroup);
                O12TimeZoneFormatter.ExchangeTimeZoneRule exchangeTimeZoneRule = new O12TimeZoneFormatter.ExchangeTimeZoneRule(flag, (ushort)year, regInfo);
                exchangeTimeZoneRule.ToBytes(new ArraySegment <byte>(array, num2, O12TimeZoneFormatter.ExchangeTimeZoneRule.Size));
                num2 += O12TimeZoneFormatter.ExchangeTimeZoneRule.Size;
            }
            return(array);
        }
        private void AddRuleToRuleGroup(ExTimeZoneRuleGroup timeZoneRuleGroup, TimeZoneDefinition.Transition transitionToPeriod, TimeZoneDefinition.Transition transitionFromPeriod, TimeZoneDefinition.TransitionsGroup transitionToGroup)
        {
            TimeZoneDefinition.RecurringDayTransition  recurringDayTransition  = transitionFromPeriod as TimeZoneDefinition.RecurringDayTransition;
            TimeZoneDefinition.RecurringDateTransition recurringDateTransition = transitionFromPeriod as TimeZoneDefinition.RecurringDateTransition;
            if (!this.periodsDictionary.ContainsKey(transitionToPeriod.To.Value))
            {
                throw new TimeZoneException((Strings.IDs) 3865092385U, transitionToGroup, transitionToPeriod);
            }
            TimeZoneDefinition.PeriodType periodType = this.periodsDictionary[transitionToPeriod.To.Value];
            TimeSpan bias = XmlConvert.ToTimeSpan(periodType.Bias);

            bias = bias.Negate();
            ExYearlyRecurringTime observanceEnd;

            if (recurringDateTransition != null)
            {
                TimeSpan timeSpan = TimeZoneDefinition.ConvertOffsetToTimeSpan(recurringDateTransition.TimeOffset, transitionToPeriod, transitionToGroup);
                try
                {
                    observanceEnd = new ExYearlyRecurringDate(recurringDateTransition.Month, recurringDateTransition.Day, timeSpan.Hours, timeSpan.Minutes, timeSpan.Seconds, timeSpan.Milliseconds);
                    goto IL_16C;
                }
                catch (ArgumentOutOfRangeException ex)
                {
                    throw new TimeZoneException((Strings.IDs) 3961981453U, ex, transitionToGroup, transitionToPeriod, "ParameterName", ex.ParamName);
                }
            }
            if (recurringDayTransition != null)
            {
                TimeSpan  timeSpan2 = TimeZoneDefinition.ConvertOffsetToTimeSpan(recurringDayTransition.TimeOffset, transitionToPeriod, transitionToGroup);
                DayOfWeek?dayOfWeek = this.ConvertToDayOfWeek(recurringDayTransition.DayOfWeek);
                if (dayOfWeek == null)
                {
                    throw new TimeZoneException(Strings.IDs.MessageInvalidTimeZoneDayOfWeekValue, transitionToGroup, transitionToPeriod);
                }
                try
                {
                    observanceEnd = new ExYearlyRecurringDay(recurringDayTransition.Occurrence, dayOfWeek.Value, recurringDayTransition.Month, timeSpan2.Hours, timeSpan2.Minutes, timeSpan2.Seconds, timeSpan2.Milliseconds);
                    goto IL_16C;
                }
                catch (ArgumentOutOfRangeException ex2)
                {
                    throw new TimeZoneException((Strings.IDs) 3961981453U, ex2, transitionToGroup, transitionToPeriod, "ParameterName", ex2.ParamName);
                }
            }
            observanceEnd = null;
IL_16C:
            ExTimeZoneRule ruleInfo = new ExTimeZoneRule(periodType.Id, periodType.Name, bias, observanceEnd);

            timeZoneRuleGroup.AddRule(ruleInfo);
        }
        private static ExTimeZone LoadGmtTimeZone()
        {
            ExTimeZone result;

            if (!ExTimeZoneEnumerator.Instance.TryGetTimeZoneByName("Greenwich Standard Time", out result))
            {
                ExTimeZoneInformation exTimeZoneInformation = new ExTimeZoneInformation("Greenwich Standard Time", "Greenwich Standard Time");
                ExTimeZoneRuleGroup   exTimeZoneRuleGroup   = new ExTimeZoneRuleGroup(null);
                ExTimeZoneRule        ruleInfo = new ExTimeZoneRule("Standard", "Standard", new TimeSpan(0L), null);
                exTimeZoneRuleGroup.AddRule(ruleInfo);
                exTimeZoneInformation.AddGroup(exTimeZoneRuleGroup);
                result = new ExTimeZone(exTimeZoneInformation);
            }
            return(result);
        }
        private void RenderTransitionToPeriod(XmlElement xmlTransitionsGroup, ExTimeZoneRule rule, ExTimeZoneRule nextRule, int ruleGroupIds)
        {
            ExYearlyRecurringTime observanceEnd         = rule.ObservanceEnd;
            ExYearlyRecurringDate exYearlyRecurringDate = rule.ObservanceEnd as ExYearlyRecurringDate;
            ExYearlyRecurringDay  exYearlyRecurringDay  = rule.ObservanceEnd as ExYearlyRecurringDay;
            string localName;

            if (exYearlyRecurringDay != null)
            {
                localName = "RecurringDayTransition";
            }
            else if (exYearlyRecurringDate != null)
            {
                localName = "RecurringDateTransition";
            }
            else
            {
                if (observanceEnd != null)
                {
                    return;
                }
                localName = "Transition";
            }
            XmlElement xmlElement    = TimeZoneDefinition.XmlHelper.CreateElement(xmlTransitionsGroup, this.typePrefix, localName, this.typeNameSpace);
            XmlElement parentElement = TimeZoneDefinition.XmlHelper.CreateTextElement(xmlElement, this.typePrefix, "To", nextRule.Id, this.typeNameSpace);

            TimeZoneDefinition.XmlHelper.CreateAttribute(parentElement, "Kind", "Period");
            if (observanceEnd != null)
            {
                this.RenderExYearlyRecurringTime(xmlElement, observanceEnd);
                if (exYearlyRecurringDay != null)
                {
                    TimeZoneDefinition.XmlHelper.CreateTextElement(xmlElement, this.typePrefix, "DayOfWeek", exYearlyRecurringDay.DayOfWeek.ToString(), this.typeNameSpace);
                    TimeZoneDefinition.XmlHelper.CreateTextElement(xmlElement, this.typePrefix, "Occurrence", exYearlyRecurringDay.Occurrence.ToString(), this.typeNameSpace);
                    return;
                }
                if (exYearlyRecurringDate != null)
                {
                    TimeZoneDefinition.XmlHelper.CreateTextElement(xmlElement, this.typePrefix, "Day", exYearlyRecurringDate.Day.ToString(), this.typeNameSpace);
                }
            }
        }
        public static byte[] GetTimeZoneBlob(ExTimeZone timeZone, ExDateTime time)
        {
            if (timeZone == null)
            {
                throw new ArgumentNullException("timeZone");
            }
            if (timeZone == ExTimeZone.UnspecifiedTimeZone)
            {
                throw new ArgumentException("timeZone should not be UnspecifiedTimeZone");
            }
            DateTime       universalTime  = time.UniversalTime;
            ExTimeZoneRule ruleForUtcTime = timeZone.TimeZoneInformation.GetRuleForUtcTime(universalTime);

            if (ruleForUtcTime != null && ruleForUtcTime.RuleGroup != null)
            {
                List <ExTimeZoneRuleGroup> list = new List <ExTimeZoneRuleGroup>();
                list.Add(ruleForUtcTime.RuleGroup);
                return(O12TimeZoneFormatter.GenerateBlobFromRuleGroups(timeZone.AlternativeId, list));
            }
            throw new ArgumentException("no time zone rule found for specified time: " + time.ToString());
        }
        internal static REG_TIMEZONE_INFO RegTimeZoneInfoFromExTimeZoneRuleGroup(ExTimeZoneRuleGroup group)
        {
            REG_TIMEZONE_INFO result          = default(REG_TIMEZONE_INFO);
            ExTimeZoneRule    exTimeZoneRule  = group.Rules[0];
            ExTimeZoneRule    exTimeZoneRule2 = (group.Rules.Count > 1) ? group.Rules[1] : null;

            if (exTimeZoneRule2 != null && exTimeZoneRule.Bias > exTimeZoneRule2.Bias)
            {
                ExTimeZoneRule exTimeZoneRule3 = exTimeZoneRule;
                exTimeZoneRule  = exTimeZoneRule2;
                exTimeZoneRule2 = exTimeZoneRule3;
            }
            result.Bias         = (int)(-(int)exTimeZoneRule.Bias.TotalMinutes);
            result.StandardBias = 0;
            if (exTimeZoneRule2 != null)
            {
                result.DaylightBias = (int)(exTimeZoneRule.Bias.TotalMinutes - exTimeZoneRule2.Bias.TotalMinutes);
                result.StandardDate = TimeZoneHelper.Win32SystemTimeFromRecurringTime(exTimeZoneRule2.ObservanceEnd);
                result.DaylightDate = TimeZoneHelper.Win32SystemTimeFromRecurringTime(exTimeZoneRule.ObservanceEnd);
            }
            return(result);
        }
        private void RenderPeriodAndReference(XmlElement xmlPeriods, XmlElement xmlTransitionsGroup, ExTimeZoneRule rule, ExTimeZoneRule nextRule, int ruleGroupIdx)
        {
            XmlElement parentElement = TimeZoneDefinition.XmlHelper.CreateElement(xmlPeriods, this.typePrefix, "Period", this.typeNameSpace);

            TimeZoneDefinition.XmlHelper.CreateAttribute(parentElement, "Bias", TimeZoneDefinition.RenderXsDuration(rule.Bias.Negate()));
            TimeZoneDefinition.XmlHelper.CreateAttribute(parentElement, "Name", rule.DisplayName);
            TimeZoneDefinition.XmlHelper.CreateAttribute(parentElement, "Id", rule.Id);
            this.RenderTransitionToPeriod(xmlTransitionsGroup, rule, nextRule, ruleGroupIdx);
        }