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

GetNextOccurrence based on today's date #44

Open
jerkohren opened this issue Dec 23, 2021 · 2 comments
Open

GetNextOccurrence based on today's date #44

jerkohren opened this issue Dec 23, 2021 · 2 comments

Comments

@jerkohren
Copy link

jerkohren commented Dec 23, 2021

Hi. I have a problem getting next occurrence time or i just miss something in Cronos documentation.

Problem:
User can set scheduler, which is done with the cron expression. User can set date and time and interval. For example 1.2.2012 15:00:00 every two days. But the date doesn't change often, only when user set new time or interval, what means if today is 20.2.2012 12:00:00, then GetNextOccurrence() get the 3.2.2012 15:00:00 as result, but i want to get 21.2.2012, which is the next relevant run time.
Workaround:

var occs = expression.GetOccurrences(schedule.Time, DateTime.UtcNow, TimeZoneInfo.Local).ToList();
job.NextRunTime = expression.GetNextOccurrence(occs.Last(), TimeZoneInfo.Local);

I just get all occurrences till today and then i use last one to get the ACTUAL next one.

Does it make sense to have a function like: GetNextOccurrenceBasedOnDateTimeNow(...)?

Thank You

@jerkohren jerkohren changed the title GetNextOccutre GetNextOccurrence Dec 23, 2021
@jerkohren jerkohren changed the title GetNextOccurrence GetNextOccurrence based on today's date Dec 23, 2021
@jbennink
Copy link

jbennink commented Dec 7, 2022

Probably a stale issue but anyways:

So schedule.Time is that user entered initial start date and the cron expression is something like 0 0 */2 * * In that case when you use GetNextOccurrence() you will have to supply the last occurrence as input parameter to get the next occurrence. Th first time, ie. when the user sets this up you use the schedule.Time as last occurrence and the next one is calculated. You could then store that occurrence in schedule.Time, unles you want to keep that value, in that case you will have to store it in another variable/field.
Your workaround also solves this but, as you stated, in a sub-optimal way.

I don't think this is a bug, it's just the way cron works, you supply it a starting point date and a cron expression and it calculates the next occurrence according to the cron expression.

@FaithfulDev
Copy link
Contributor

It's a few years later, so now it's definitely stale 😅 but...

If I understand this correctly, you want a method that acts as a shortcut for expression.GetNextOccurrence(DateTime.UtcNow, TimeZoneInfo.Local);?

Having a dedicated method for this is a bit overkill IMO. If you want the current date, just supply the current date.

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

No branches or pull requests

3 participants