atifaziz / NCrontab

Crontab for .NET

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Occurrence intervals interpreted with gaps

Matthias-Hess opened this issue · comments

Consider the following example:

var schedule = CrontabSchedule.Parse("* * * * *");

DateTime a = new DateTime(2020, 11, 17, 21, 25, 0);
DateTime b = new DateTime(2020, 11, 17, 21, 28, 0);
DateTime c = new DateTime(2020, 11, 17, 21, 30, 0);

var occurrencesAB = schedule.GetNextOccurrences(a, b).ToList();
var occurrencesBC = schedule.GetNextOccurrences(b, c).ToList();
var occurrencesAC = schedule.GetNextOccurrences(a, c).ToList();

I would expect to see

  • 3 occurrences in occurrencesAB (21:25, 21:26, 21:27)
  • 2 occurrences in occurrencesBC (21:28, 21:29)
  • 5 occurrences in occurrencesAC (21:25, 21:26, 21:27, 21:28, 21:29)

Instead, what I get is

  • 2 occurrences in occurrencesAB (21:26, 21:27)
  • 1 occurrences in occurrencesBC (21:29)
  • 4 occurrences in occurrencesAC (21:26, 21:27, 21:28, 21:29)

This makes the implementation of a gapless scheduler messier than necessary.
My application should periodically determine which jobs should be executed in the ime period between the last poll and DateTime.Now. When DateTIme.Now is exactly on a minute break, I get Gaps in my occurrences, and some jobs do not run.

I now add 1 Tick to endtime to get what I want, but this seems a bit messy. I am sure I must be missing something...
Best Regards Mat

@Matthias-Hess As noted in the remarks section of the method documentation:

/// 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.
/// </remarks>

the start and end are exclusive. You'll need to account for the fact that b is excluded in your first and second call because it is at the limits of the range you specify unlike in your third call.

var occurrencesAB = schedule.GetNextOccurrences(a, b).ToList();
var occurrencesBC = schedule.GetNextOccurrences(b.AddTicks(-1), c).ToList();
var occurrencesAC = schedule.GetNextOccurrences(a, c).ToList();

I am going to close this as by-design, but feel free to re-open if I've misunderstood your issue.

@atifaziz Thanks for your response. You understood my issue correctly, and I understand that this is by design.
I will use AddTicks() to get exactly what I want.