rianjs / ical.net

ical.NET - an open source iCal library for .NET
MIT License
782 stars 230 forks source link

OccursAt and OccursOn do not work as expected #536

Open kostyabek opened 2 years ago

kostyabek commented 2 years ago

I was trying to check whether these two function work as expected, but I have not managed to get a single TRUE from them. As I understand, they return TRUE when these conditions are met: OccursOn - if the date provided is equal to the valid range of dates from CalendarEvent OccursAt - if the date and time of the event are equal to those that indicate its start.

image

Source code

var rrule = new RecurrencePattern("FREQ=MONTHLY;UNTIL=20221112T215959Z;WKST=SU");

var vEvent = new CalendarEvent
{
    Start = new CalDateTime(new DateTime(2021, 11, 12, 16, 0, 0)),
    End = new CalDateTime(new DateTime(2021, 11, 12, 17, 0, 0)),
    RecurrenceRules = new List<RecurrencePattern> { rrule },
};

var targetDateTime = new CalDateTime(new DateTime(2021, 11, 12, 16, 0, 0));

var res = vEvent.OccursAt(targetDateTime);

What am I doing wrong, if I do? Thanks in advance.

Idrek commented 2 years ago

I think the thing wrong here is that you are not triggering the method that calculates occurrences. Try with following code, after some refactor to start and end dates, and added the missing instruction:

var rrule = new RecurrencePattern("FREQ=MONTHLY;UNTIL=20221112T215959Z;WKST=SU");
var startDate = new CalDateTime(new DateTime(2021, 11, 12, 16, 0, 0));
var endDate = new CalDateTime(new DateTime(2021, 11, 12, 17, 0, 0));
var vEvent = new CalendarEvent
{
    Start = startDate,
    End = endDate,
    RecurrenceRules = new List<RecurrencePattern> { rrule },
};

var targetDateTime = new CalDateTime(new DateTime(2021, 11, 12, 16, 0, 0));
vEvent.GetOccurrences(startDate, endDate);
var res = vEvent.OccursAt(targetDateTime);    //  res should be true here.