Get First/Last Occurence #642
Unanswered
pinkfloydx33
asked this question in
Q&A
Replies: 1 comment 3 replies
-
Sounds like you already came up with a helper like this, but without /// <summary>
/// Provides helper methods for evaluating recurrence rules of calendar events.
/// </summary>
public class RecurrenceHelper
{
/// <summary>
/// Gets the first occurrence of the specified calendar event.
/// </summary>
/// <param name="calendarEvent">The calendar event.</param>
/// <returns>The first occurrence of the event.</returns>
public static IDateTime GetFirstOccurrence(CalendarEvent calendarEvent)
{
return calendarEvent.Start;
}
/// <summary>
/// Gets the last occurrence of the specified calendar event.
/// </summary>
/// <param name="calendarEvent">The calendar event.</param>
/// <returns>The last occurrence of the event.</returns>
/// <exception cref="InvalidOperationException">Thrown when the recurrence rule does not have either COUNT or UNTIL.</exception>
public static IDateTime GetLastOccurrence(CalendarEvent calendarEvent)
{
var recurrenceRule = calendarEvent.RecurrenceRules.FirstOrDefault();
if (recurrenceRule == null)
{
return calendarEvent.Start;
}
if (recurrenceRule.Count > 0)
{
return GetOccurrenceByCount(calendarEvent, recurrenceRule.Count);
}
if (recurrenceRule.Until != DateTime.MinValue)
{
return GetOccurrenceByUntil(calendarEvent, recurrenceRule.Until);
}
throw new InvalidOperationException("Recurrence rule must have either COUNT or UNTIL.");
}
/// <summary>
/// Gets the last occurrence of the specified calendar event within the given upper bound.
/// </summary>
/// <remarks>
/// Without COUNT or UNTIL in the recurrence rule, it is challenging to determine the last occurrence
/// of an event because there is no explicit end condition. Recurrence rules without these properties
/// imply an infinite series of occurrences.
/// </remarks>
/// <param name="calendarEvent">The calendar event.</param>
/// <param name="upperBound">The upper bound date to limit the search for occurrences.</param>
/// <returns>The last occurrence of the event within the specified upper bound.</returns>
public static IDateTime GetLastOccurrence(CalendarEvent calendarEvent, CalDateTime upperBound)
{
var recurrenceRule = calendarEvent.RecurrenceRules.FirstOrDefault();
if (recurrenceRule == null)
{
return calendarEvent.Start;
}
var occurrences = calendarEvent.GetOccurrences(calendarEvent.Start, upperBound);
return occurrences.LastOrDefault()?.Period.EndTime;
}
/// <summary>
/// Gets the occurrence of the specified calendar event by count.
/// </summary>
/// <param name="calendarEvent">The calendar event.</param>
/// <param name="count">The count of occurrences to retrieve.</param>
/// <returns>The occurrence of the event at the specified count.</returns>
private static IDateTime GetOccurrenceByCount(CalendarEvent calendarEvent, int count)
{
var occurrences = calendarEvent.GetOccurrences(calendarEvent.Start, calendarEvent.Start.AddYears(100));
return occurrences.Skip(count - 1).FirstOrDefault()?.Period.EndTime;
}
/// <summary>
/// Gets the occurrence of the specified calendar event by the UNTIL date.
/// </summary>
/// <param name="calendarEvent">The calendar event.</param>
/// <param name="until">The UNTIL date to limit the search for occurrences.</param>
/// <returns>The last occurrence of the event before or on the UNTIL date.</returns>
private static IDateTime GetOccurrenceByUntil(CalendarEvent calendarEvent, CalDateTime until)
{
var occurrences = calendarEvent.GetOccurrences(calendarEvent.Start, until);
return occurrences.LastOrDefault()?.Period.EndTime;
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Is there a way to get only the First and/or Last occurrences of an event?
I'm working with all-day events that cannot recur sub-daily. In several instances I need to know what the first and (if applicable) last event in the series are. Computing the entire hashset of every single occurrence has been proving to be a big memory consumer, especially when events have a high frequency (i.e. daily) or large upper bound (i.e. years). We've been able to recognize some cases where we can optimize the ranges we are looking up (if the recurrence pattern only contains Frequency, Count/Until and Interval) but would like something more general purpose.
I need the information mainly as metadata for the event that we are either storing in our database or using to validate other inputs in our application. Producing the entire hashset of
Occurence
objects is very heavy in these scenarios.Beta Was this translation helpful? Give feedback.
All reactions