DHTMLX / scheduler-recurring-events-dotnet

# Recurring Events Helper for dhtmlxScheduler on ASP.NET/ASP.NET Core backends
3 stars 4 forks source link

GetOccurrences ignores occurrences that should replace generated occurrences #4

Open ditchcode opened 6 months ago

ditchcode commented 6 months ago
using System;
using System.Collections.Generic;
using DHTMLX.Scheduler;
using DHTMLX.Scheduler.RecurringEvents;

public class Program
{
    public static void Main()
    {
        var timestampForOccurrenceJanuary3 = Convert.ToInt32(((new DateTime(2019, 1, 3, 13, 0, 0)) - (new DateTime(1970, 1, 1))).TotalSeconds);

        var schEvents = new[]{
            new SchedulerEvent {
                id = "1",
                text = "birthday",
                start_date = new DateTime(2019, 1, 2, 13, 0, 0),
                end_date = new DateTime(2019, 1, 5, 13, 0, 0),
                rec_type = "day_1___",
                event_length = 1800,                
            },
            new SchedulerEvent {
                id = "1#January3",
                text = "reschedule Jan 3 to Jan 10",
                start_date = new DateTime(2019, 1, 10, 12, 0, 0),
                end_date = new DateTime(2019, 1, 10, 12, 30, 0),
                rec_type = "",
                event_length = timestampForOccurrenceJanuary3,  
                event_pid = "1"
            },
        };

        //because event id 2 falls outside the From-To of 1/1 to 1/9, the event is bypassed and never replaces the occurrence generated for 1/3
        var occurrences = new RecurringEventsHelper().GetOccurrences(schEvents, new DateTime(2019, 1, 1), new DateTime(2019, 1, 9));
        foreach(var o in occurrences)
        {
            Console.WriteLine(o);
            /*
            id = 1
            text = birthday
            start_date = 2019-01-02 13:00
            end_date = 2019-01-02 13:30
            rec_type = 
            event_length = 0
            event_pid = 

            id = 1
            text = birthday
            start_date = 2019-01-03 13:00 <=========== SHOULD HAVE BEEN RESCHEDULED TO 1/10
                                                       AND NOT RETURNED IN THE RESULTS
                                               BECAUSE IT FALLS OUTSIDE OUR FROM-TO OF 1/1 to 1/9
            end_date = 2019-01-03 13:30
            rec_type = 
            event_length = 0
            event_pid = 

            id = 1
            text = birthday
            start_date = 2019-01-04 13:00
            end_date = 2019-01-04 13:30
            rec_type = 
            event_length = 0
            event_pid = 
            */
        }

        //because event id 2 falls within the From-To of 1/1 to 1/15, the event correctly replaces the occurrence generated for 1/3
        occurrences = new RecurringEventsHelper().GetOccurrences(schEvents, new DateTime(2019, 1, 1), new DateTime(2019, 1, 15));
        foreach(var o in occurrences)
        {
            Console.WriteLine(o);
            /*
            id = 1
            text = birthday
            start_date = 2019-01-02 13:00
            end_date = 2019-01-02 13:30
            rec_type = 
            event_length = 0
            event_pid = 

            id = 1
            text = birthday
            start_date = 2019-01-04 13:00
            end_date = 2019-01-04 13:30
            rec_type = 
            event_length = 0
            event_pid = 

            id = 1#January3
            text = reschedule Jan 3 to Jan 10
            start_date = 2019-01-10 12:00
            end_date = 2019-01-10 12:30
            rec_type = 
            event_length = 1546520400
            event_pid = 1
            */
        }
    }
}
ditchcode commented 6 months ago

Seems to me RecurringEventsHelper _GetOccurrences should not be testing this: if (tmp.start_date < toDate && tmp.end_date > fromDate) and skipping instances that do not match. But should instead be testing the original DateTime stored as a Unix timestamp in event_length against toDate and fromDate to see if it falls inside the range and thus should be applied to correct any SeriesInfo occurrences that is covers. And then if tmp.start_time and tmp.end_time falls outside that From-To range, it should be filtered from the results.