What's the logic behind the unreachable dates?
See original GitHub issueHi! I am trying to familiarize myself with the scenarios in which the GetNextOccurrence
can return a DateTime?
with null
value, and I have a hard time understanding the logic behind the behavior. It seems that calculating the next occurrence for dates after the year 2100 is unsuccessful about half of the time. Here is a minimal example:
string expression = "0 0 15 6 *"; // June, 15
var cronExpression = CronExpression.Parse(expression);
Console.WriteLine($"Expression: {cronExpression}");
for (int i = 1; i <= 24; i++)
{
var fromUtc = new DateTime(2150, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddMonths(i);
var nextUtc = cronExpression.GetNextOccurrence(fromUtc);
Console.WriteLine($"Date: {fromUtc}, NextOccurrence: {(nextUtc.HasValue ? nextUtc.Value.ToString() : "-")}");
}
Output:
Expression: 0 0 0 15 6 *
Date: 02/01/2150 00:00:00, NextOccurrence: 06/15/2150 00:00:00
Date: 03/01/2150 00:00:00, NextOccurrence: 06/15/2150 00:00:00
Date: 04/01/2150 00:00:00, NextOccurrence: 06/15/2150 00:00:00
Date: 05/01/2150 00:00:00, NextOccurrence: 06/15/2150 00:00:00
Date: 06/01/2150 00:00:00, NextOccurrence: 06/15/2150 00:00:00
Date: 07/01/2150 00:00:00, NextOccurrence: -
Date: 08/01/2150 00:00:00, NextOccurrence: -
Date: 09/01/2150 00:00:00, NextOccurrence: -
Date: 10/01/2150 00:00:00, NextOccurrence: -
Date: 11/01/2150 00:00:00, NextOccurrence: -
Date: 12/01/2150 00:00:00, NextOccurrence: -
Date: 01/01/2151 00:00:00, NextOccurrence: 06/15/2151 00:00:00
Date: 02/01/2151 00:00:00, NextOccurrence: 06/15/2151 00:00:00
Date: 03/01/2151 00:00:00, NextOccurrence: 06/15/2151 00:00:00
Date: 04/01/2151 00:00:00, NextOccurrence: 06/15/2151 00:00:00
Date: 05/01/2151 00:00:00, NextOccurrence: 06/15/2151 00:00:00
Date: 06/01/2151 00:00:00, NextOccurrence: 06/15/2151 00:00:00
Date: 07/01/2151 00:00:00, NextOccurrence: -
Date: 08/01/2151 00:00:00, NextOccurrence: -
Date: 09/01/2151 00:00:00, NextOccurrence: -
Date: 10/01/2151 00:00:00, NextOccurrence: -
Date: 11/01/2151 00:00:00, NextOccurrence: -
Date: 12/01/2151 00:00:00, NextOccurrence: -
Date: 01/01/2152 00:00:00, NextOccurrence: 06/15/2152 00:00:00
Could you explain why the calculation fails when the month+day of the starting date is larger than the month+day in the Cron expression?
Thanks!
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top Results From Across the Web
Y2K bug
The Y2K bug was a computer flaw, or bug, that may have caused problems when dealing with dates beyond December 31, 1999.
Read more >Year 2000 problem
Computer systems' inability to distinguish dates correctly had the potential to bring down worldwide infrastructures for computer reliant industries.
Read more >I'm trying to call my boyfriend and it's saying unreachable. ...
Call status not reachable indicates that the dialed number was not reachable at the point when system tried initiating call to the same....
Read more >Common questions about dates (article)
We are writing this on 12/26/12 or Wednesday, December 26, 2012. Traditionally understood as two-thousand and twelve years (give or take a few)...
Read more >Fallacy by Sherlock Holmes 'Eliminate the impossible, and ...
is a logical fallacy that occurs when some explanation is believed to be true on the basis that alternate explanations are impossible, yet...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
In 78 years when my system fails, I’m coming back here to complain. I’ll be angry, and there will probably be swearing… 😉 😛
Cronos wasn’t planned to calculate occurrences after 2099 year, there’s a hardcoded values, please see the following snippet. Such an upper bound is required, because it’s not possible to know that some cron expression leads to an unreachable date in upfront, and we need to avoid the infinite loop in this case.
https://github.com/HangfireIO/Cronos/blob/b35c34692f74cf59630be8184d12742306ab2b32/src/Cronos/CronExpression.cs#L42
I think there should be
ArgumentOutOfRangeException
thrown when start date’s year is more than this hardcoded value.