Skip to content

Instantly share code, notes, and snippets.

@tucksaun
Created June 23, 2011 12:12
Show Gist options
  • Save tucksaun/ad486f026738f1fc534b to your computer and use it in GitHub Desktop.
Save tucksaun/ad486f026738f1fc534b to your computer and use it in GitHub Desktop.
cron-expression - patch issue #1
diff --git a/src/Cron/CronExpression.php b/src/Cron/CronExpression.php
index 2cba31f..30c6416 100644
--- a/src/Cron/CronExpression.php
+++ b/src/Cron/CronExpression.php
@@ -120,7 +120,8 @@ class CronExpression
}
// 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;
@@ -216,6 +217,12 @@ class CronExpression
}
$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) {
@@ -226,6 +233,10 @@ class CronExpression
// 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;
}
diff --git a/tests/Cron/CronExpressionTest.php b/tests/Cron/CronExpressionTest.php
index 1d56d9c..0f5a9bd 100644
--- a/tests/Cron/CronExpressionTest.php
+++ b/tests/Cron/CronExpressionTest.php
@@ -70,6 +70,23 @@ class CronExpressionTest extends \PHPUnit_Framework_TestCase
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),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment