Beispiel #1
0
        /// <summary>
        /// PreviousOccurrence(DateTime, DateRange),
        /// Return the previous occurrence in the schedule for the given date, from within the
        /// specified date range. Returns null if nothing is found.
        /// See PreviousOccurrence(DateTime) for additional details.
        /// </summary>
        public DateTime?PreviousOccurrence(DateTime aDate, DateRange during)
        {
            // Make sure that our search begins no later than the end of the during range.
            DateTime duringEnding = during.EndDateTime.SafeAddDays(1);

            if (aDate > duringEnding)
            {
                aDate = duringEnding;
            }
            // Make sure that our search begins no later than the end of the event limits range.
            DateRange eventLimits = _event.GetEventLimitsAsDateRange();
            DateTime  eventEnding = eventLimits.EndDateTime.SafeAddDays(1);

            if (aDate > eventEnding)
            {
                aDate = eventEnding;
            }
            // Find the loop limit date for this search.
            // This should be the latest available start date from all the applicable limiting ranges.
            DateTime earliestSearchDate = during.StartDateTime;

            if (eventLimits.StartDateTime > earliestSearchDate)
            {
                earliestSearchDate = eventLimits.StartDateTime;
            }
            // Perform the search.
            DateTime?occurrence = null;

            while (aDate >= earliestSearchDate)
            {
                // Get a working range for this search.
                var workingRange = DateRangeForPreviousOrNextOccurrence(aDate, true, earliestSearchDate);
                // Find the previous occurrence.
                var dates = Occurrences(workingRange).OrderByDescending(o => o.Date);
                occurrence = dates.SkipWhile(o => o >= aDate.Date).FirstOrDefault();
                occurrence = (occurrence == default(DateTime)) ? null : occurrence;
                // Break the loop if an occurrence was found.
                if (occurrence != null)
                {
                    break;
                }
                // Increment the search date to search an earlier DateRange.
                aDate = workingRange.StartDateTime.AddDays(-1);
            }

            // Make sure that our result is no earlier than the start of the event limits range.
            if (occurrence != null && occurrence < eventLimits.StartDateTime)
            {
                occurrence = null;
            }
            // Make sure that our result is no earlier than the start of the during range.
            if (occurrence != null && occurrence < during.StartDateTime)
            {
                occurrence = null;
            }
            return(occurrence);
        }
Beispiel #2
0
        /// <summary>
        /// RoughlyEstimateMinimumEventOccurrences,
        /// This will return a very rough, and conservative, guess of the minimum number of occurrences
        /// that should be generated by a particular event.
        /// </summary>
        static public int RoughlyEstimateMinimumEventOccurrences(Event eventInstance)
        {
            if (eventInstance.HasBrokenZeroOccurrenceConfiguration())
            {
                return(0);
            }
            int       repeatInterval = eventInstance.RepeatInterval;
            DateRange eventLimits    = eventInstance.GetEventLimitsAsDateRange();
            TimeSpan  timeSpan       = eventLimits.EndDateTime - eventLimits.StartDateTime;
            int       spanDays       = timeSpan.Days;
            int       spanWeeks      = timeSpan.Days / 7;
            int       result         = 0;

            switch (eventInstance.FrequencyTypeOptions)
            {
            case FrequencyTypeEnum.None:
                result = 1;
                break;

            case FrequencyTypeEnum.Daily:
                result = (spanDays / repeatInterval) - 1;
                break;

            case FrequencyTypeEnum.Weekly:
                int includedDays = 0;
                includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Mon) ? 1 : 0;
                includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Tue) ? 1 : 0;
                includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Wed) ? 1 : 0;
                includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Thu) ? 1 : 0;
                includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Fri) ? 1 : 0;
                includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Sat) ? 1 : 0;
                includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Sun) ? 1 : 0;
                result        = ((spanWeeks / repeatInterval) * includedDays) - 1;
                break;

            case FrequencyTypeEnum.Monthly:
                int spanMonths = spanDays / 31;
                result = (spanMonths / repeatInterval) - 1;
                break;

            case FrequencyTypeEnum.Quarterly:
                int spanQuarters = spanDays / 92;
                result = (spanQuarters / repeatInterval) - 1;
                break;

            case FrequencyTypeEnum.Yearly:
                int spanYears = spanDays / 365;
                result = (spanYears / repeatInterval) - 1;
                break;
            }
            return((result > 0) ? result : 0);
        }
Beispiel #3
0
 /// <summary>
 /// RoughlyEstimateMinimumEventOccurrences,
 /// This will return a very rough, and conservative, guess of the minimum number of occurrences
 /// that should be generated by a particular event. 
 /// </summary>
 public static int RoughlyEstimateMinimumEventOccurrences(Event eventInstance)
 {
     if (eventInstance.HasBrokenZeroOccurrenceConfiguration()) { return 0; }
     int repeatInterval = eventInstance.RepeatInterval;
     DateRange eventLimits = eventInstance.GetEventLimitsAsDateRange();
     TimeSpan timeSpan = eventLimits.EndDateTime - eventLimits.StartDateTime;
     int spanDays = timeSpan.Days;
     int spanWeeks = timeSpan.Days / 7;
     int result = 0;
     switch (eventInstance.FrequencyTypeOptions)
     {
         case FrequencyTypeEnum.None:
             result = 1;
             break;
         case FrequencyTypeEnum.Daily:
             result = (spanDays / repeatInterval) - 1;
             break;
         case FrequencyTypeEnum.Weekly:
             int includedDays = 0;
             includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Mon) ? 1 : 0;
             includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Tue) ? 1 : 0;
             includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Wed) ? 1 : 0;
             includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Thu) ? 1 : 0;
             includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Fri) ? 1 : 0;
             includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Sat) ? 1 : 0;
             includedDays += eventInstance.DaysOfWeekOptions.HasFlag(DayOfWeekEnum.Sun) ? 1 : 0;
             result = ((spanWeeks / repeatInterval) * includedDays) - 1;
             break;
         case FrequencyTypeEnum.EveryWeekDay:
             result = ((spanWeeks / repeatInterval) * 5) - 1;
             break;
         case FrequencyTypeEnum.EveryMonWedFri:
             result = ((spanWeeks / repeatInterval) * 3) - 1;
             break;
         case FrequencyTypeEnum.EveryTuTh:
             result = ((spanWeeks / repeatInterval) * 2) - 1;
             break;
         case FrequencyTypeEnum.Monthly:
             int spanMonths = spanDays / 31;
             result = (spanMonths / repeatInterval) - 1;
             break;
         case FrequencyTypeEnum.Quarterly:
             int spanQuarters = spanDays / 92;
             result = (spanQuarters / repeatInterval) - 1;
             break;
         case FrequencyTypeEnum.Yearly:
             int spanYears = spanDays / 365;
             result = (spanYears / repeatInterval) - 1;
             break;
     }
     return (result > 0) ? result : 0;
 }