Skip to content

Commit

Permalink
修复 #51 定时任务分钟数、秒数设置超过59n会报错的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Mar 19, 2020
1 parent 741b822 commit a152fef
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Cron/CronCalculator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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
Expand Down
34 changes: 34 additions & 0 deletions tests/unit/Component/Tests/Cron/CronCalculatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']),
]));
}

}

0 comments on commit a152fef

Please sign in to comment.