Skip to content

Commit

Permalink
Merge pull request #2 from tucksaun/master
Browse files Browse the repository at this point in the history
Issue #1 (week of day) fix
  • Loading branch information
mtdowling committed Jun 29, 2011
2 parents 6c6c4b3 + d34f06a commit ac63150
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Cron/CronExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public function getNextRunDate($currentTime = 'now')
}

// Adjust the day of week by incrementing the day until it matches. Resest time.
if (!$this->unitSatisfiesCron($nextRun, 'N', $this->getExpression(self::WEEKDAY))) {
// According cron implementation, 0 si we use 'w' format
if (!$this->unitSatisfiesCron($nextRun, 'w', $this->getExpression(self::WEEKDAY))) {
$nextRun->add(new DateInterval('P1D'));
$nextRun->setTime(0, 0, 0);
continue;
Expand Down Expand Up @@ -216,6 +217,12 @@ protected function unitSatisfiesCron(DateTime $nextRun, $unit, $schedule)
}

$unitValue = (int) $nextRun->format($unit);

// According cron implementation, 0|7 = sunday, so we replace it
if ( $unit == 'w' && strpos($schedule, '7') !== false )
{
$schedule = str_replace('7','0', $schedule);
}

// Check increments of ranges
if (strpos($schedule, '*/') !== false) {
Expand All @@ -226,6 +233,10 @@ protected function unitSatisfiesCron(DateTime $nextRun, $unit, $schedule)
// Check intervals
if (strpos($schedule, '-')) {
list($first, $last) = explode('-', $schedule);
if ( $unit == 'w' && $last == 0 )
{
return $this->unitSatisfiesCron($nextRun, $unit, sprintf('0,%u-6',$first));
}
return $unitValue >= $first && $unitValue <= $last;
}

Expand Down
17 changes: 17 additions & 0 deletions tests/Cron/CronExpressionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ public function scheduleProvider()
array('1 * * * 7', '2015-08-10 21:47:27', '2015-08-16 00:01:00', false),
// Test with exact times
array('47 21 * * *', strtotime('2015-08-10 21:47:30'), '2015-08-11 21:47:00', false),
// Test Day of the week (issue #1)
// According cron implementation, 0|7 = sunday, 1 => monday, etc
array('* * * * 0', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false),
array('* * * * 7', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false),
array('* * * * 1', strtotime('2011-06-15 23:09:00'), '2011-06-20 00:00:00', false),
//should return the sunday date as 7 equals 0
array('0 0 * * 1,7', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false),
array('0 0 * * 0-4', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false),
array('0 0 * * 7-4', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false),
array('0 0 * * 4-7', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false),
array('0 0 * * 7-3', strtotime('2011-06-15 23:09:00'), '2011-06-19 00:00:00', false),
array('0 0 * * 3-7', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false),
array('0 0 * * 3-7', strtotime('2011-06-18 23:09:00'), '2011-06-19 00:00:00', false),
//array('0 0 * * 4-7', strtotime('2011-06-15 23:09:00'), '2011-06-16 00:00:00', false),
// Test Day of the Week and the Day of the Month (issue #1)
array('0 0 1 1 0', strtotime('2011-06-15 23:09:00'), '2012-01-01 00:00:00', false),
array('0 0 1 * 0', strtotime('2011-06-15 23:09:00'), '2012-01-01 00:00:00', false),
);
}

Expand Down

0 comments on commit ac63150

Please sign in to comment.