From a152fef674f319e37d6d5bd87c3d6875ba7eb2b5 Mon Sep 17 00:00:00 2001 From: Yurun Date: Thu, 19 Mar 2020 10:04:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20#51=20=E5=AE=9A=E6=97=B6?= =?UTF-8?q?=E4=BB=BB=E5=8A=A1=E5=88=86=E9=92=9F=E6=95=B0=E3=80=81=E7=A7=92?= =?UTF-8?q?=E6=95=B0=E8=AE=BE=E7=BD=AE=E8=B6=85=E8=BF=8759n=E4=BC=9A?= =?UTF-8?q?=E6=8A=A5=E9=94=99=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Cron/CronCalculator.php | 14 +++++--- .../Tests/Cron/CronCalculatorTest.php | 34 +++++++++++++++++++ 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/src/Cron/CronCalculator.php b/src/Cron/CronCalculator.php index 9fab8caebe..25859f8516 100644 --- a/src/Cron/CronCalculator.php +++ b/src/Cron/CronCalculator.php @@ -65,13 +65,13 @@ private function generateTime($lastTime, $years, $months, $weeks, $days, $hours, { continue; } - foreach($months as $month) + foreach($months ?: [] as $month) { if($year == $nowYear && $month < $nowMonth) { continue; } - foreach($days as $day) + foreach($days ?: [] as $day) { if('year' === $day) { @@ -117,19 +117,19 @@ private function generateTime($lastTime, $years, $months, $weeks, $days, $hours, private function parseHis($year, $month, $day, $hours, $minutes, $seconds, $nowYear, $nowMonth, $nowDay, $nowHour, $nowMinute, $nowSecond) { - foreach($hours as $hour) + foreach($hours ?: [] as $hour) { if($year == $nowYear && $month == $nowMonth && $day == $nowDay && $hour < $nowHour) { continue; } - foreach($minutes as $minute) + foreach($minutes ?: [] as $minute) { if($year == $nowYear && $month == $nowMonth && $day == $nowDay && $hour == $nowHour && $minute < $nowMinute) { continue; } - foreach($seconds as $second) + foreach($seconds ?: [] as $second) { if($year == $nowYear && $month == $nowMonth && $day == $nowDay && $hour == $nowHour && $minute == $nowMinute && $second <= $nowSecond) { @@ -173,6 +173,10 @@ public function getAll($rule, $name, $min, $max, $dateFormat, $lastTime) $step = (int)substr($rule, 0, -1); if($lastTime < $min) { + if($step > $max - $min) + { + return []; + } return range($min, $max, $step); } else diff --git a/tests/unit/Component/Tests/Cron/CronCalculatorTest.php b/tests/unit/Component/Tests/Cron/CronCalculatorTest.php index 8275d49144..fad462db90 100644 --- a/tests/unit/Component/Tests/Cron/CronCalculatorTest.php +++ b/tests/unit/Component/Tests/Cron/CronCalculatorTest.php @@ -262,4 +262,38 @@ public function testAll() ])); } + /** + * @link https://github.com/Yurunsoft/imi/issues/51 + * + * @return void + */ + public function testBug51() + { + $beginTime = strtotime('2018-06-21 12:34:56'); + + $this->assertEquals(strtotime('2079-06-21 12:34:56'), $this->cronCalculator->getNextTickTime($beginTime, [ + new CronRule(['year' => '61n']), + ])); + + $this->assertEquals(strtotime('2023-07-21 12:34:56'), $this->cronCalculator->getNextTickTime($beginTime, [ + new CronRule(['month' => '61n']), + ])); + + $this->assertEquals(strtotime('2018-08-21 12:34:56'), $this->cronCalculator->getNextTickTime($beginTime, [ + new CronRule(['day' => '61n']), + ])); + + $this->assertEquals(strtotime('2018-06-24 01:34:56'), $this->cronCalculator->getNextTickTime($beginTime, [ + new CronRule(['hour' => '61n']), + ])); + + $this->assertEquals(strtotime('2018-06-21 13:35:56'), $this->cronCalculator->getNextTickTime($beginTime, [ + new CronRule(['minute' => '61n']), + ])); + + $this->assertEquals(strtotime('2018-06-21 12:35:57'), $this->cronCalculator->getNextTickTime($beginTime, [ + new CronRule(['second' => '61n']), + ])); + } + }