atifaziz / NCrontab

Crontab for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Start date equal to end date will give back no sequence?

MichaelSchofeld opened this issue · comments

If i supply a start-date Feb 11, 1pm and an end-date Feb 11, 2 pm. and I say i want all occurrences that fall on Tuesdays in this date range.. Feb 11 is a Tuesday so should it not return 1 occurrence?

Just wondering if this is a bug or by design?

As per the documentation, start/base and end date and time are exclusive:

/// This method does not return the value of <paramref name="baseTime"/>
/// itself if it falls on the schedule. For example, if <paramref name="baseTime" />
/// is midnight and the schedule was created from the expression <c>* * * * *</c>
/// (meaning every minute) then the next occurrence of the schedule
/// will be at one minute past midnight and not midnight itself.
/// The method returns the <em>next</em> occurrence <em>after</em>
/// <paramref name="baseTime"/>. Also, <param name="endTime" /> is
/// exclusive.

So this is by-design assuming your crontab expression was hourly on Tuesdays; that is 0 * * * Tue. If you had something like this (every 15 minutes of a Tuesday ):

var schedule = CrontabSchedule.Parse("*/15 * * * Tue");
var occurrences = schedule.GetNextOccurrences(new DateTime(2020, 2, 11, 1, 0, 0),
                                              new DateTime(2020, 2, 11, 2, 0, 0));
foreach (var dt in occurrences)
    Console.WriteLine(dt.ToString("ddd, dd MMM yyyy HH:mm"));

then you should see 3 occurrences:

Tue, 11 Feb 2020 01:15
Tue, 11 Feb 2020 01:30
Tue, 11 Feb 2020 01:45

Note that 1 am and 2 am do not occur. If you wanted those, you'd have to adjust start and end by a second:

var schedule = CrontabSchedule.Parse("*/15 * * * Tue");
var start = new DateTime(2020, 2, 11, 1, 0, 0);
var end = start.AddHours(1);
var occurrences = schedule.GetNextOccurrences(start.AddSeconds(-1), end.AddSeconds(1));
foreach (var dt in occurrences)
    Console.WriteLine(dt.ToString("ddd, dd MMM yyyy HH:mm"));

Closing as by-design.