Using NextOccurrenceInPeriod

I am having rather difficult time getting NextOccurrenceInPeriod to work. In fact, I can’t even find it in the available methods off of the recurrence objects.

So the simple question is, how do I go about finding the next occurence of an event given a recurrence object. Below is an example of what I want to do.

-----------------------
MonthlyDayOfWeek recur = new MonthlyDayOfWeek(2,NthOccurrence.Second,DayOfWeek.Monday);

DateTime nextRecur = recur.NextOccurrenceInPeriod(DateTime.Now)
------------------------

The NextOccurrenceInPeriod method is not used anywhere in the demo application, so I have not been able to find an example of this working either.

Thanks,
Toby

Hi Toby,

NextOccurrenceInPeriod is a protected method and used by the recurrence pattern classes to calculate occurrence dates. It was never intended to be used by client applications. It is called from the recurrence engine core algorithm and I think it is pretty useless just by itself.

Just use GenerateOccurrences to generate the occurrence dates. Let me know if that’s not enough and what you are trying to achieve.

What you’ve said makes sense. However, the GenerateOccurences is not really the approach I want (I think). Given a recurrence pattern, we need to know when is its next occurence? We won’t know at the time if it is a daily or yearly pattern. It is wastful to specify too long of a period (span of a whole year if the pattern is daily), but we need it to be big enough to get back at least one date (if it is a yearly pattern, we would have to have a span of longer than a year.)

How could we go about finding the next occurence for the given pattern (without risking very expensive and lenghtly processing)?

Thanks,
Toby

GenerateOccurrence was optimized for speed so even if you have daily recurrence pattern and specify several years interval it will work fast.

In fact, it all grew from a real calendaring application project where you have several events per day everyday and want to show a yearly view with days with events coloured differently.

Note that a yearly recurrence pattern does not have to occur every year, it occurs every Nth year.

If you are concerned about performance I’d recommend you stress test Aspose.Recurrence to the limits you need and if there are any problems we will either optimize it even further or think about making NextOccurrenceInPeriod and other methods public.

If you want to avoid too long period (year for a daily pattern) you can use different periods depending on the pattern class.

Thanks for the quick replies. I do not doubt that the engine is fast. But if you think about what I am trying to do, GetNextOccurence is exactly that. It is a lot of extra work on my end to check each recurrence pattern and play around with the time span.

We are trying to make a wrapper for a timer of sorts. We will have an array list of Recurrence objects. We want to know when each one needs to fire. The easiest that we can see is simply having a property on each of our objects that tells us the next time that it needs to run. We’ll check our array list ever X minutes. Once the time for the occurence has been met, our process will run and then increment the recurrence pattern to the next period. To handle this logic using GenerateOcurrences seems like a lot of extra work.

Do you know of a simple manner to achieve our objective?

Thanks for your help.

Toby

I see what you are saying, but at the moment it is easier to work with GenerateOccurrences. We had several different models in mind when developing Aspose.Recurrence and the FindNext concept did not suit all patterns easily and nicely that’s why it’s not available.

If you have a time that fires every X minutes and you need to check whether a particular pattern has to fire this time or not just GenerateOccurrences for a short period of time around now, say one day.

Alternatively you can employ approach similar to Aspose.Recurrence.Demos and create a Task object that has a Recurrence pattern and an array of occurrence dates that can be recalculated when needed (when recurrence pattern is changed). Then you can calculate occurrence dates for a long range for all tasks for example at startup and your timer only needs to deal with arrays of dates.