EWSoftware / PDI

A Personal Data Interchange (PDI) library that implements the vCard (RFC 2426), vCalendar, and iCalendar (RFC 2445) specifications including recurrence pattern generation
Microsoft Public License
64 stars 26 forks source link

Recurrence with frequency YEARLY and specifying rule BYMONTHDAY is not function as expected #20

Closed jorgegiro closed 2 years ago

jorgegiro commented 2 years ago

Hi!

I starting to using the library in order to calculate recurrent dates. And I try to set a Recurrence specifying a frequency of YEARLY and only setting a rule of BYMONTHDAY. For example, I am trying to calculate:

    StartDate : 2021/01/01
    Until: DateTime.MaxValue
    Frequency: Yearly
    Interval: 1
    ByMonthDay: 3
var recurrence = new Recurrence();
recurrence.StartDateTime =new DateTime(2021, 01, 01);
recurrence.RecurUntil = DateTime.MaxValue;
recurrence.Frequency = RecurFrequency.Yearly;
recurrence.Interval = 1;
recurrence.ByMonthDay.Add(3);

DateTime nextDate = recurrence.NextInstance(new DateTime(2021, 01, 10), false);

Then, what I expect when NextInstance(new DateTime(2021, 01, 10), false) it is called it is a return date 2022/01/03. But instead, it's returning '2022/01/01'.

This is happening because when expanding dates, expand ByMonthDate and if its not set ByWeekNo expands ByDay even it's not set any ByDay rule. This cause, that a RecurDateTimeCollection is genarated containing the date 2022/01/01 and is included as an instance.

if(byMonthDay.Count != 0)
{
    // Expand by month day if specified without any by month rule part
    isExpanded = true;
    rdtcMoDay = new RecurDateTimeCollection(dates);
    freqRules.ByMonthDay(this, rdtcMoDay);
}

// As long as by week number isn't specified either, we'll expand the by day rule here too
if(byWeekNo.Count == 0)
{
   isExpanded = true;
   rdtcDay = new RecurDateTimeCollection(dates);
   freqRules.ByDay(this, rdtcDay);
}

Please, Could you confirm if this is the right behaviour? Or may be NextInstance should return 2022/01/03? Or may be I am setting something wrong. It's also possible đŸ˜…

EWSoftware commented 2 years ago

Yes, I think it is being applied incorrectly in this case. If you add a BYMONTH=1 condition, you do get the results you'd expect. Since a BYMONTHDAY condition is being specified, it shouldn't be pulling in the day from the start date and should be expanding it using only the BYMONTHDAY values.

EWSoftware commented 2 years ago

I merged the change. It will be in the next release due out soon.