# Find next occurence

We are using following recurrence pattern for scheduling and trying to get next occurrence from specific date/time. To get next occurrence, we have to get all occurrence of pattern and loop through the all return dates.

We have following main issues :

It take lot of time to generate all occurrences. API GenerateOccurrences use lot of cpu cycle and memory.
Can you please suggest simple and best way to find out next occurrence? or Is there any API to get next occurrence from specific date/time for pattern?

We will be very thankful for any help to solve this problem and let us know if you need more info.

Pattern for schedule :

DTSTART:20081118T072354
RRULE:FREQ=DAILY;UNTIL=20110824T232454Z;BYHOUR=7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23
;BYMINUTE=23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,
50,51,52,53,54,55,56,57,58,59,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22

Hi Dalbir,

Thank you for inquiry.

GenerateOccurrences() method will calculate the dates every time. In your pattern, the start date is in year 2008. In your scenario, you want to find out the next occurrence from specific date/time, could you please try modifying the start and end dates as below, to reduce the number of dates generated. This will also reduce the CPU work and memory storage.

string pattern = @“DTSTART:20081118T072354
RRULE:FREQ=DAILY;UNTIL=20110824T232454Z;BYHOUR=7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23
;BYMINUTE=23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,
50,51,52,53,54,55,56,57,58,59,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22”;
RecurrencePattern recPattern = new RecurrencePattern(pattern);
// This is the specific date, for which you want to find next occurrence
DateTime specificDate = new DateTime(2009, 1, 1, 11, 0, 0);
// Set start date to a day before
DateTime startDate = specificDate.AddDays(-1);
recPattern.StartDate = startDate;
// Modify the rule, sent Until to specific day + 1

I hope it helps.

Thanks for the reply,

That is good to make the interval short. But there are many type of scheduling/frequency ( daily,hourly,weekly, monthly etc.). It is difficult to calculate end date (specific date). Also by adding one day ( recPattern.RRules[0].Until = specificDate.AddDays(1) , we are not able to get the next occurrence for weekly , monthly frequency type?

Is there any API or can you provide that will return next occurrence from specific date instead of getting all occurrences based on recurrence pattern?

Hi,

I have consulted with the development team for their feedback on this issue. I will get back to you as soon as I have some update for you.

Hi Dalbir,

Could you please try the following workaround?

// Original recurrence pattern
string pattern = @“DTSTART:20081118T072354
RRULE:FREQ=DAILY;UNTIL=20110824T232454Z;BYHOUR=7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23;BYMINUTE=23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22”;

// To find next occurrence
// This is the specific date, for which you want to find next occurrence
DateTime specificDate = new DateTime(2009, 11, 18, 07, 10, 54);

// Create another recurrence pattern
RecurrencePattern recPattern2 = new RecurrencePattern(pattern);
// Set start date to the specific date
recPattern2.StartDate = specificDate;
// Set count to 100
recPattern2.RRules[0].Count = 100;

// Generate dates
DateCollection dtColl = recPattern2.GenerateOccurrences();
Console.WriteLine("Date: " + specificDate);
Console.WriteLine("Next occurrence: " + dtColl[1]);

Please note that if we set count to 2, then next occurrence might be incorrect, because BYMINUTE rule starts from 23. Example:
18.11.2009 7:10:54

18.11.2009 7:23:54
To fix this, we set the count to 100 to consider the whole values in the MINUTE rule.

Thanks Saqib,

We try and it works and return the occurrences ( how may we set in count) between start date and end date. It will help to reduce memory and cpu cycle.We will try with more examples and let you know if need any help.

Thanks
Dalbir Singh