Ejemplo n.º 1
0
        public static TimeSpan GetDuration(this ListItem item, DateTime?startDate = null, DateTime?endDate = null)
        {
            TimeSpan duration;
            bool     isRecurrence = (bool)item["fRecurrence"];

            if (isRecurrence)
            {
                bool isAllDayEvent = (bool)item["fAllDayEvent"];
                duration = new TimeSpan();
                RecurrenceRule recurrenceRule = item.GetRecurrenceRule("EventDate", "EndDate", "RecurrenceData");
                TimeSpan       startTime      = recurrenceRule.StartDate.TimeOfDay;
                TimeSpan       endTime;
                if (endDate.HasValue)
                {
                    if (recurrenceRule.EndDate.HasValue)
                    {
                        endTime = recurrenceRule.EndDate.Value.TimeOfDay;
                        if (recurrenceRule.EndDate.Value > endDate.Value)
                        {
                            recurrenceRule.EndDate = endDate;
                        }
                    }
                    else
                    {
                        if (recurrenceRule.StartDate <= endDate.Value)
                        {
                            endTime = TimeSpan.FromDays(1);
                            recurrenceRule.EndDate = endDate;
                        }
                        else
                        {
                            duration = new TimeSpan();
                            return(duration);
                        }
                    }
                }
                else
                {
                    endTime = TimeSpan.FromDays(1);
                }

                if (startDate.HasValue &&
                    (recurrenceRule.EndDate.HasValue && startDate.Value > recurrenceRule.EndDate.Value))
                {
                    duration = new TimeSpan();
                    return(duration);
                }

                var occurrences = isAllDayEvent
                    ? recurrenceRule.GetOccurrences(new TimeSpan(), TimeSpan.FromDays(1))
                    : recurrenceRule.GetOccurrences(startTime, endTime);
                foreach (Occurrence occurrence in occurrences)
                {
                    if (startDate.HasValue && occurrence.Start < startDate.Value)
                    {
                        if (occurrence.End > startDate.Value)
                        {
                            duration = duration.Add((occurrence.End - startDate.Value).Duration());
                        }
                        continue;
                    }
                    duration = duration.Add(occurrence.Duration);
                }
            }
            else
            {
                var start = (DateTime)item["EventDate"];
                var end   = (DateTime)item["EndDate"];
                if (startDate.HasValue && (startDate.Value > start))
                {
                    start = startDate.Value;
                }
                if (endDate.HasValue && (endDate.Value < end))
                {
                    end = endDate.Value;
                }
                duration = (end - start).Duration();
            }
            return(duration);
        }
Ejemplo n.º 2
0
        public static RecurrenceRule ParseRule(DateTime startDate, DateTime?endDate, string recurrenceData)
        {
            RecurrenceRule recurrenceRule = null;
            var            ruleParser     = new SPRecurrenceRuleParser(recurrenceData);

            if (ruleParser.Type.HasValue)
            {
                switch (ruleParser.Type)
                {
                case RecurrenceType.Daily:
                    var dyilyRecurrenceRule = new DyilyRecurrenceRule {
                        IsWeekday = ruleParser.IsWeekday
                    };
                    recurrenceRule = dyilyRecurrenceRule;
                    break;

                case RecurrenceType.Weekly:
                    var weeklyRecurrenceRule = new WeeklyRecurrenceRule();
                    var daysOfWeek           = new List <DayOfWeek>(ruleParser.DaysOfWeek.Select(GetDayOfWeek));
                    if (ruleParser.IsDay)
                    {
                        daysOfWeek.Add(DayOfWeek.Day);
                    }
                    if (ruleParser.IsWeekday)
                    {
                        daysOfWeek.Add(DayOfWeek.Weekday);
                    }
                    if (ruleParser.IsWeekendDay)
                    {
                        daysOfWeek.Add(DayOfWeek.WeekendDay);
                    }
                    weeklyRecurrenceRule.DaysOfWeek = daysOfWeek.ToArray();
                    if (ruleParser.FirstDayOfWeek.HasValue)
                    {
                        weeklyRecurrenceRule.FirstDayOfWeek = ruleParser.FirstDayOfWeek.Value;
                    }
                    recurrenceRule = weeklyRecurrenceRule;
                    break;

                case RecurrenceType.Monthly:
                    var monthlyRecurrenceRule = new MonthlyRecurrenceRule();
                    if (ruleParser.Day != null)
                    {
                        monthlyRecurrenceRule.DayOfMonth = ruleParser.Day.Value;
                    }
                    recurrenceRule = monthlyRecurrenceRule;
                    break;

                case RecurrenceType.MonthlyByDay:
                    var monthlyByDayRecurrenceRule = new MonthlyByDayRecurrenceRule();
                    if (ruleParser.IsDay)
                    {
                        monthlyByDayRecurrenceRule.DayOfWeek = DayOfWeek.Day;
                    }
                    else if (ruleParser.IsWeekday)
                    {
                        monthlyByDayRecurrenceRule.DayOfWeek = DayOfWeek.Weekday;
                    }
                    else if (ruleParser.IsWeekendDay)
                    {
                        monthlyByDayRecurrenceRule.DayOfWeek = DayOfWeek.WeekendDay;
                    }
                    else if (ruleParser.DaysOfWeek.Length > 0)
                    {
                        monthlyByDayRecurrenceRule.DayOfWeek = GetDayOfWeek(ruleParser.DaysOfWeek.First());
                    }
                    if (ruleParser.Ordinal != null)
                    {
                        monthlyByDayRecurrenceRule.DayOfWeekOrdinal = ruleParser.Ordinal.Value;
                    }
                    recurrenceRule = monthlyByDayRecurrenceRule;
                    break;

                case RecurrenceType.Yearly:
                    var yearlyRecurrenceRule = new YearlyRecurrenceRule();
                    if (ruleParser.Month != null)
                    {
                        yearlyRecurrenceRule.Month = (Month)ruleParser.Month;
                    }
                    if (ruleParser.Day != null)
                    {
                        yearlyRecurrenceRule.DayOfMonth = ruleParser.Day.Value;
                    }
                    recurrenceRule = yearlyRecurrenceRule;
                    break;

                case RecurrenceType.YearlyByDay:
                    var yearlyByDayRecurrenceRule = new YearlyByDayRecurrenceRule();
                    if (ruleParser.Month != null)
                    {
                        yearlyByDayRecurrenceRule.Month = (Month)ruleParser.Month;
                    }
                    if (ruleParser.IsDay)
                    {
                        yearlyByDayRecurrenceRule.DayOfWeek = DayOfWeek.Day;
                    }
                    else if (ruleParser.IsWeekday)
                    {
                        yearlyByDayRecurrenceRule.DayOfWeek = DayOfWeek.Weekday;
                    }
                    else if (ruleParser.IsWeekendDay)
                    {
                        yearlyByDayRecurrenceRule.DayOfWeek = DayOfWeek.WeekendDay;
                    }
                    else if (ruleParser.DaysOfWeek.Length > 0)
                    {
                        yearlyByDayRecurrenceRule.DayOfWeek = GetDayOfWeek(ruleParser.DaysOfWeek.First());
                    }
                    if (ruleParser.Ordinal != null)
                    {
                        yearlyByDayRecurrenceRule.DayOfWeekOrdinal = ruleParser.Ordinal.Value;
                    }
                    recurrenceRule = yearlyByDayRecurrenceRule;
                    break;
                }

                if (recurrenceRule != null)
                {
                    if (ruleParser.Frequency != null)
                    {
                        recurrenceRule.Interval = ruleParser.Frequency.Value;
                    }
                    if (ruleParser.WindowEnd.HasValue)
                    {
                        recurrenceRule.EndDate = endDate < ruleParser.WindowEnd.Value
                            ? endDate
                            : ruleParser.WindowEnd.Value;
                        recurrenceRule.StartDate = startDate > ruleParser.WindowEnd.Value
                            ? ruleParser.WindowEnd.Value
                            : startDate;
                    }
                    else
                    {
                        recurrenceRule.StartDate = startDate;
                        recurrenceRule.EndDate   = endDate;
                    }
                    if (ruleParser.RepeatInstances != null)
                    {
                        recurrenceRule.NumberOfOccurrences = ruleParser.RepeatInstances.Value;
                    }
                }
            }
            return(recurrenceRule);
        }