From bed91c0aac99f10dbcaa11b0d816f9fdca359e1c Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Fri, 29 Nov 2013 20:11:57 -0800 Subject: [PATCH 1/4] Added getRangeRunDates getRangeRunDates returns all run dates limited to a date range. --- src/Cron/CronExpression.php | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/Cron/CronExpression.php b/src/Cron/CronExpression.php index b7b7a576..e49d0af1 100644 --- a/src/Cron/CronExpression.php +++ b/src/Cron/CronExpression.php @@ -194,6 +194,38 @@ public function getMultipleRunDates($total, $currentTime = 'now', $invert = fals return $matches; } + + /** + * Get ALL run dates limited to a date range + * + * @param string|DateTime $start Start of range + * @param string|DateTime $end End of range + * + * @return array Returns an array of run dates + */ + public function getRangeRunDates($start, $end) + { + $matches = array(); + + if (!($end instanceof DateTime)) { + $end = new DateTime($end ?: 'now'); + $end->setTimezone(new DateTimeZone(date_default_timezone_get())); + } + + if (!($start instanceof DateTime)) { + $start = new DateTime($start ?: 'now'); + $start->setTimezone(new DateTimeZone(date_default_timezone_get())); + } + + for ( + $guard = $this->getNextRunDate($start, 0, true); + $guard <= $end; + $guard = $this->getNextRunDate($guard, 0, false) + ) + $matches[] = $guard; + + return $matches; + } /** * Get all or part of the CRON expression From 84d82a7fc02e4437eeb124981db3e0e1e049846e Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Fri, 29 Nov 2013 20:15:14 -0800 Subject: [PATCH 2/4] optimized getMultipleRunDates max(0, $total) doesn't need to be calculated in every iteration. --- src/Cron/CronExpression.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Cron/CronExpression.php b/src/Cron/CronExpression.php index e49d0af1..1b345e26 100644 --- a/src/Cron/CronExpression.php +++ b/src/Cron/CronExpression.php @@ -188,7 +188,8 @@ public function getPreviousRunDate($currentTime = 'now', $nth = 0, $allowCurrent public function getMultipleRunDates($total, $currentTime = 'now', $invert = false, $allowCurrentDate = false) { $matches = array(); - for ($i = 0; $i < max(0, $total); $i++) { + $guard_terminator = max(0, $total); + for ($i = 0; $i < $guard_terminator; $i++) { $matches[] = $this->getRunDate($currentTime, $i, $invert, $allowCurrentDate); } From 55365d0e6852c0e384e8391cc1c6a5f56e4a9c24 Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Sat, 30 Nov 2013 20:39:13 -0800 Subject: [PATCH 3/4] Changed string|DateTime to null|string|DateTime --- src/Cron/CronExpression.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Cron/CronExpression.php b/src/Cron/CronExpression.php index 1b345e26..42116fde 100644 --- a/src/Cron/CronExpression.php +++ b/src/Cron/CronExpression.php @@ -199,8 +199,8 @@ public function getMultipleRunDates($total, $currentTime = 'now', $invert = fals /** * Get ALL run dates limited to a date range * - * @param string|DateTime $start Start of range - * @param string|DateTime $end End of range + * @param null|string|DateTime $start Start of range + * @param null|string|DateTime $end End of range * * @return array Returns an array of run dates */ From 85cafe79f1f3a39437fdf75514391776e8c31054 Mon Sep 17 00:00:00 2001 From: Sepehr Laal Date: Thu, 5 Dec 2013 13:09:43 -0800 Subject: [PATCH 4/4] better getMultipleRunDates and cleaner code "cleaner code" by re aligning some comments and adding braces. --- src/Cron/CronExpression.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Cron/CronExpression.php b/src/Cron/CronExpression.php index 42116fde..b4d6e6cc 100644 --- a/src/Cron/CronExpression.php +++ b/src/Cron/CronExpression.php @@ -188,19 +188,18 @@ public function getPreviousRunDate($currentTime = 'now', $nth = 0, $allowCurrent public function getMultipleRunDates($total, $currentTime = 'now', $invert = false, $allowCurrentDate = false) { $matches = array(); - $guard_terminator = max(0, $total); - for ($i = 0; $i < $guard_terminator; $i++) { + for ($i = 0, $max = max(0, $total); $i < $max; $i++) { $matches[] = $this->getRunDate($currentTime, $i, $invert, $allowCurrentDate); } return $matches; } - /** + /** * Get ALL run dates limited to a date range * - * @param null|string|DateTime $start Start of range - * @param null|string|DateTime $end End of range + * @param string|DateTime $start Start of range + * @param string|DateTime $end End of range * * @return array Returns an array of run dates */ @@ -222,8 +221,9 @@ public function getRangeRunDates($start, $end) $guard = $this->getNextRunDate($start, 0, true); $guard <= $end; $guard = $this->getNextRunDate($guard, 0, false) - ) + ) { $matches[] = $guard; + } return $matches; }