Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ranges that cross over midnight #24

Closed
zumoshi opened this issue Jan 27, 2017 · 2 comments
Closed

ranges that cross over midnight #24

zumoshi opened this issue Jan 27, 2017 · 2 comments

Comments

@zumoshi
Copy link

zumoshi commented Jan 27, 2017

Hello,
I would like to report an unexpected behavior.

Cron:
* 20-4 * * *

I expect it to work as if written:
* 20,21,22,...,3,4 * * *

it works as if written:
* 4,5,6,...,19,20 * * *

I believe 20-4 should expand to 20-23,0-4 rather than 4-20. since it makes little sense to write the numbers backwards. the original crontab considers midnight crossing ranges as invalid so either case is violating the original implantation, might as well implant the one that makes more sense.

I know that this was mentioned before in #14, but there was no alternatives proposed there.

@atifaziz
Copy link
Owner

As pointed out in #14, this won't work. In fact, I should disallow inverted ranges and throw an error instead to avoid confusion. You can also see on crontab.guru that * 20-4 * * * is considered invalid (hour is in red):

image

Your best bet to get the result you want is to create two schedules, one that goes from hour 20 to 23 and another from 0 to 4. You can then use SortedMerge from MoreLINQ and DistinctUntilChanged from System.Interactive to merge both schedules into one, like this:

var bt = new DateTime(2000, 12, 4);
var et = bt.AddDays(2);
var s1 = NCrontab.CrontabSchedule.Parse("0 20-23 * * *");
var o1 = s1.GetNextOccurrences(bt, et);
var s2 = NCrontab.CrontabSchedule.Parse("0 0-4 * * *");
var o2 = s2.GetNextOccurrences(bt, et);

var ms =
    o1.SortedMerge(OrderByDirection.Ascending, o2)
      .DistinctUntilChanged();
      
foreach (var dt in ms)
    Console.WriteLine(dt);
04/12/2000 01:00:00
04/12/2000 02:00:00
04/12/2000 03:00:00
04/12/2000 04:00:00
04/12/2000 20:00:00
04/12/2000 21:00:00
04/12/2000 22:00:00
04/12/2000 23:00:00
05/12/2000 00:00:00
05/12/2000 01:00:00
05/12/2000 02:00:00
05/12/2000 03:00:00
05/12/2000 04:00:00
05/12/2000 20:00:00
05/12/2000 21:00:00
05/12/2000 22:00:00
05/12/2000 23:00:00

@atifaziz
Copy link
Owner

I'm going to close this because the behaviour is by-design. The right fix is to disallow such an expression to avoid confusion and which is now being tracked as issue #25.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants