Skip to content

Fix day-of-week ranges that use day names - #226

Open
waw666waw666 wants to merge 1 commit into
dragonmantank:masterfrom
waw666waw666:fix-dow-named-ranges
Open

waw666waw666 wants to merge 1 commit into
dragonmantank:masterfrom
waw666waw666:fix-dow-named-ranges

Conversation

@waw666waw666

@waw666waw666 waw666waw666 commented Nov 14, 2026 •

Copy link
Copy Markdown

Day-of-week ranges written with names are evaluated wrongly, because SUN is stored as 7 and the range handling only looks at the raw text:

$start = new DateTime('2024-01-05 12:00', new DateTimeZone('UTC'));

(new CronExpression('0 10 * * SAT-SUN'))->getMultipleRunDates(4, $start);
// Sat 01-06, Sat 01-13, Sat 01-20, Sat 01-27   (Sundays never run)

(new CronExpression('0 10 * * SUN-THU'))->getNextRunDate($start);
// RuntimeException: Impossible CRON expression   (isDue() is always false)

(new CronExpression('0 10 * * MON-FRI/2'))->getMultipleRunDates(3, $start);
// Sun 01-07, Sun 01-14, Sun 01-21   (expected Mon, Wed, Fri)

(new CronExpression('0 10 * JAN-MAR/2 *'))->getNextRunDate($start);
// OutOfRangeException: Invalid range start requested

FRI-SUN also skips Sunday. The numeric forms (6-7, 0-4, 1-5/2) work, so this only bites people who use names, e.g. ->cron('0 10 * * SAT-SUN') in a Laravel schedule, which silently stops running on Sundays.

Cause

  • DayOfWeekField::isSatisfiedBy() swaps 7→0 / 0→7 on the raw range strings, then picks the N (Sunday = 7) or w (Sunday = 0) date format by looking for a literal 7 character. SAT-SUN contains no 7, so it is checked with w, while isInRange() converts SUN to 7: Sunday (0) never falls in 6-7. SUN-THU becomes 7-4 and matches nothing.
  • AbstractField::isInIncrementsOfRanges() casts the bounds with (int) without converting literals, so MON-FRI/2 becomes 0-0/2 and JAN-MAR/2 starts at 0 and throws.

Fix

  • Convert range bounds to numbers first, then normalise: if a range runs backwards because Sunday is 7 at the start or 0 at the end, use the other value (SUN-THU → 0-4, 5-0 → 5-7, as before for numbers).
  • Check the weekday with w and let a Sunday also match when the value covers 7. This replaces the N/w guess, so 0-7/2 now includes Sunday and 0-0 / 7-7 mean Sunday only (they used to match every day).
  • Convert literals in isInIncrementsOfRanges().

Tests

Added DayOfWeekFieldTest::testNamedRangesIncludeSunday, MonthFieldTest::testNamedRangeWithStep and CronExpressionTest::testNamedDayOfWeekRangesThatEndOrStartOnSunday; all three fail on master (2 failures, 1 OutOfRangeException) and pass with the fix. Full suite: 180 tests, 873 assertions, OK (4 skipped, same as master). PHPStan: no errors.

Behavior change

This also changes the result for some purely numeric day-of-week values. I compared every value of the form a, a-b, a-b/s and */s (a, b in 0-7, s in 1-8) before and after: 83 of the 592 values change, and all of them are corrections:

  • Sunday is now matched where 0 or 7 is in the set (e.g. 0-7/2), where it was missed before.
  • 0-0 and 7-7 now mean Sunday only; before they matched every day.
  • 5-0/2-style values (a range ending on Sunday as 0, with a step) no longer throw.

No other numeric value changes.

SUN is stored as 7, and the range normalisation and the 'N'/'w' format
choice only looked at the raw text, so named ranges were evaluated
wrongly: SAT-SUN and FRI-SUN never matched Sunday, SUN-THU never matched
at all, and MON-FRI/2 was read as 0-0 and fired on Sundays only.

Convert the range bounds to numbers before normalising them, treat a
Sunday as matching when the value covers either 0 or 7, and convert
literals in stepped ranges so JAN-MAR/2 no longer throws.
@waw666waw666 waw666waw666 changed the title (srFix day-of-week ranges that use day namesc/Cron/DayOfWeekField.php). Fix day-of-week ranges that use day names Nov 14, 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.

1 participant