Skip to content

Fix CronExpression not always returning correct next schedule for weekdayonly and month flags #2838

Merged
lahma merged 4 commits into
quartznet:3.xfrom
jafin:fix/backport-cronexpression-weekdayonly
Mar 1, 2026
Merged

Fix CronExpression not always returning correct next schedule for weekdayonly and month flags #2838
lahma merged 4 commits into
quartznet:3.xfrom
jafin:fix/backport-cronexpression-weekdayonly

Conversation

@jafin

@jafin jafin commented Mar 1, 2026

Copy link
Copy Markdown
Contributor

backport 211255c from main

Fix CronExpression not always returning correct next schedule for weekdayonly and month flags (#2341)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Backports a fix to CronExpression.GetTimeAfter so monthly schedules using the W (weekday-only) modifier (and related month selection logic) compute the correct next fire time, especially around weekend adjustments and shorter months.

Changes:

  • Replaced SortedSet “tail set” logic with TryGetMinValueStartingFrom helpers for selecting next candidate values.
  • Fixed month/day calculation to avoid invalid dates when the next month is shorter (e.g., 31W transitioning into February).
  • Added new unit test scenarios for W/monthly behavior and the short-month regression case.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/Quartz/Util/SortedSetExtensions.cs Adds helper methods to find the next minimum value from a starting point.
src/Quartz/TriggerConstants.cs Introduces a shared earliest-year constant used by cron parsing/docs.
src/Quartz/CronExpressionConstants.cs Adds internal constants used by cron-related helpers.
src/Quartz/CronExpression.cs Updates GetTimeAfter selection logic and adds short-month clamping; includes small doc/cleanup edits.
src/Quartz.Tests.Unit/Impl/Calendar/CronCalendarTest.cs Minor formatting adjustment in a test.
src/Quartz.Tests.Unit/CronExpressionTest.cs Adds new parameterized cron scenarios + a regression test for 31W across shorter months.

Comment thread src/Quartz/Util/SortedSetExtensions.cs
Comment thread src/Quartz.Tests.Unit/CronExpressionTest.cs
jafin and others added 2 commits March 1, 2026 21:27
TryGetMinValueStartingFrom(DateTimeOffset, ...) reads set.Min before checking set.Count == 0. If set is empty, this will throw InvalidOperationException, making the later empty-check dead code. Consider early-returning when set.Count == 0 (and initializing minimumDay to a safe value) before accessing Min/Max.

Co-authored-by: Copilot <[email protected]>
Set Timezone to Utc for Tests.

Co-authored-by: Copilot <[email protected]>
@sonarqubecloud

sonarqubecloud Bot commented Mar 1, 2026

Copy link
Copy Markdown

@lahma lahma left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@lahma
lahma merged commit 5fb3f34 into quartznet:3.x Mar 1, 2026
6 checks passed
This was referenced Mar 2, 2026
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

Successfully merging this pull request may close these issues.

3 participants