private void ReadRecurrencePattern(byte[] bytes)
        {
            AppointmentRecurrencePatternStructure structure = AppointmentRecurrencePatternStructure.GetRecurrencePatternStructure(bytes);

            m_startDTUtc            = structure.GetStartDTUtc(this.OriginalTimeZone);
            m_duration              = structure.Duration;
            RecurrenceType          = structure.RecurrenceType;
            m_lastInstanceStartDate = structure.LastInstanceStartDate;
            Period = structure.PeriodInRecurrenceTypeUnits;

            if (structure.EndType == RecurrenceEndType.EndAfterNOccurrences)
            {
                EndAfterNumberOfOccurences = true;
            }

            switch (structure.RecurFrequency)
            {
            case RecurrenceFrequency.Daily:
            {
                break;
            }

            case RecurrenceFrequency.Weekly:
            {
                Day = (int)((WeeklyRecurrencePatternStructure)structure).DaysOfWeek;
                break;
            }

            case RecurrenceFrequency.Monthly:
            {
                if (structure.PatternType == PatternType.Month)
                {
                    Day = (int)((MonthlyRecurrencePatternStructure)structure).DayOfMonth;
                }
                else         // MonthNth
                {
                    Day = (int)((MonthlyRecurrencePatternStructure)structure).DayOfWeek;
                    DayOccurenceNumber = ((MonthlyRecurrencePatternStructure)structure).DayOccurenceNumber;
                }
                break;
            }

            case RecurrenceFrequency.Yearly:
            {
                if (structure.PatternType == PatternType.Month)
                {
                    Day = (int)((YearlyRecurrencePatternStructure)structure).DayOfMonth;
                }
                else         // MonthNth
                {
                    Day = (int)((YearlyRecurrencePatternStructure)structure).DayOfWeek;
                    DayOccurenceNumber = ((YearlyRecurrencePatternStructure)structure).DayOccurenceNumber;
                }
            }
            break;
            }

            DeletedInstanceDates = structure.DeletedInstanceDates;
            ExceptionList        = structure.ExceptionList;
        }
        public override void SaveChanges()
        {
            TimeZoneInfo timezone = this.OriginalTimeZone;

            if (timezone == null)
            {
                // if time zone was not provided, store current system time zone definition
                timezone = TimeZoneInfoUtils.GetSystemStaticTimeZone(TimeZoneInfo.Local.Id);
            }
            AppointmentRecurrencePatternStructure structure = GetRecurrencePattern(timezone);

            PC.SetBytesProperty(PropertyNames.PidLidAppointmentRecur, structure.GetBytes(this.File.WriterCompatibilityMode));

            base.SaveChanges();
        }
        private AppointmentRecurrencePatternStructure GetRecurrencePattern(TimeZoneInfo timezone)
        {
            AppointmentRecurrencePatternStructure structure;
            RecurrenceFrequency recurrenceFrequency = RecurrenceTypeHelper.GetRecurrenceFrequency(RecurrenceType);
            PatternType         patternType         = RecurrenceTypeHelper.GetPatternType(RecurrenceType);

            switch (recurrenceFrequency)
            {
            case RecurrenceFrequency.Daily:
                structure = new DailyRecurrencePatternStructure();
                break;

            case RecurrenceFrequency.Weekly:
                structure = new WeeklyRecurrencePatternStructure();
                ((WeeklyRecurrencePatternStructure)structure).DaysOfWeek = (DaysOfWeekFlags)Day;
                break;

            case RecurrenceFrequency.Monthly:
                structure = new MonthlyRecurrencePatternStructure();
                if (patternType == PatternType.Month)
                {
                    ((MonthlyRecurrencePatternStructure)structure).DayOfMonth = (uint)Day;
                }
                else
                {
                    ((MonthlyRecurrencePatternStructure)structure).DayOfWeek          = (OutlookDayOfWeek)Day;
                    ((MonthlyRecurrencePatternStructure)structure).DayOccurenceNumber = DayOccurenceNumber;
                }
                break;

            case RecurrenceFrequency.Yearly:
                structure = new YearlyRecurrencePatternStructure();
                if (patternType == PatternType.Month)
                {
                    ((YearlyRecurrencePatternStructure)structure).DayOfMonth = (uint)Day;
                }
                else
                {
                    ((YearlyRecurrencePatternStructure)structure).DayOfWeek          = (OutlookDayOfWeek)Day;
                    ((YearlyRecurrencePatternStructure)structure).DayOccurenceNumber = DayOccurenceNumber;
                }
                break;

            default:
                throw new InvalidRecurrencePatternException("Recurrence frequency is invalid");
            }
            structure.PatternType = patternType;
            structure.PeriodInRecurrenceTypeUnits = Period;
            structure.SetStartAndDuration(m_startDTUtc, m_duration, timezone);
            DateTime startDTZone = TimeZoneInfo.ConvertTimeFromUtc(m_startDTUtc, timezone);

            structure.FirstDateTimeInDays   = AppointmentRecurrencePatternStructure.CalculateFirstDateTimeInDays(recurrenceFrequency, patternType, Period, startDTZone);
            structure.LastInstanceStartDate = m_lastInstanceStartDate;
            if (EndAfterNumberOfOccurences)
            {
                structure.OccurrenceCount = (uint)CalendarHelper.CalculateNumberOfOccurences(StartDTUtc, m_lastInstanceStartDate, this.RecurrenceType, Period, Day);
                structure.EndType         = RecurrenceEndType.EndAfterNOccurrences;
            }
            else if (m_lastInstanceStartDate.Year >= 4500)
            {
                structure.EndType         = RecurrenceEndType.NeverEnd;
                structure.OccurrenceCount = 10;
            }
            else
            {
                structure.EndType = RecurrenceEndType.EndAfterDate;
                // OccurrenceCount should not be 0, or otherwise Outlook 2003's Appointment recurrence window is not loaded properly
                structure.OccurrenceCount = 10;
            }

            structure.DeletedInstanceDates = DeletedInstanceDates;
            foreach (ExceptionInfoStructure exception in ExceptionList)
            {
                structure.ModifiedInstanceDates.Add(DateTimeUtils.GetDayStart(exception.NewStartDT));
            }
            structure.ExceptionList = ExceptionList;
            return(structure);
        }