HangfireIO / Cronos

A fully-featured .NET library for working with Cron expressions. Built with time zones in mind and intuitively handles daylight saving time transitions
MIT License
974 stars 114 forks source link

Feature Request: Support an array of cron expressions #47

Closed mattheys closed 6 months ago

mattheys commented 2 years ago

My users have just asked if they can change the current schedule of every 30 mins between 10am and 5pm to also include 5pm.

I can't see a way of doing this without also including 5:30pm therefore i would like to have an array of two expressions built into one so that GetNextOccurrence will get the next one from either expression.

            var expressions = new string[]
            {
                "0,30 10-16 * * 1-5", 
                "0 17 * * 1-5"
            };
            var cronosExpression = CronExpression.Parse(expressions);

I've reworked my app to handle this in my IHostedService manually but there might be others who will need something like this in the future too, it's easy enough to do in a Linux crontab because you just run the same script twice.

0,30 10-16 * * 1-5  /opt/example.sh
0 17 * * 1-5        /opt/example.sh
jbennink commented 1 year ago

@mattheys you Linux example is based on the fact that the two crontabs are mutually exclusive. If there was overlap crontab would happily run your script twice. I would guess this library (Cronos) is meant to parse cron expressions only. Your use-case goes beyond that, and as you mention is easily implemented in your own code without having to add this to Cronos.

odinserj commented 6 months ago

I think this can be implemented manually by creating multiple instances of the CronExpression class, and checking what date returned by GetNextOccurrence goes first.