Skip to content

Commit

Permalink
Merge pull request #23 from overtrue/analysis-Xk9gbR
Browse files Browse the repository at this point in the history
Apply fixes from StyleCI
  • Loading branch information
overtrue authored Aug 28, 2018
2 parents 2bf53c4 + abd6b82 commit 0f3968d
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 86 deletions.
44 changes: 26 additions & 18 deletions src/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,9 +809,11 @@ public function dateDiff($date1, $date2)

/**
* 获取两个日期之间以年为单位的距离.
*
* @param array $lunar1
* @param array $lunar2
* @param bool $absolute
* @param bool $absolute
*
* @return int
*/
public function diffInYears($lunar1, $lunar2, $absolute = false)
Expand All @@ -836,20 +838,23 @@ public function diffInYears($lunar1, $lunar2, $absolute = false)
if ($greaterLunar['lunar_month'] == $lessLunar['lunar_month']) {
if ($greaterLunar['is_leap'] && !$lessLunar['is_leap']) {
$monthAdjustFactor = 0;
} else if (!$greaterLunar['is_leap'] && $lessLunar['is_leap']) {
} elseif (!$greaterLunar['is_leap'] && $lessLunar['is_leap']) {
$monthAdjustFactor = 1;
}
}
$yearAdjustFactor = $greaterLunar['lunar_month'] - $monthAdjustFactor >= $lessLunar['lunar_month'] ? 0 : 1;
$diff = $greaterLunar['lunar_year'] - $yearAdjustFactor - $lessLunar['lunar_year'];

return $absolute ? $diff : ($changed ? -1 * $diff : $diff);
}

/**
* 获取两个日期之间以月为单位的距离.
*
* @param array $lunar1
* @param array $lunar2
* @param bool $absolute
* @param bool $absolute
*
* @return int
*/
public function diffInMonths($lunar1, $lunar2, $absolute = false)
Expand Down Expand Up @@ -877,14 +882,13 @@ public function diffInMonths($lunar1, $lunar2, $absolute = false)
$lessLunarAdjustFactor = $lessLunar['is_leap'] || (0 < $leapMonth && $leapMonth < $lessLunar['lunar_month']) ? 1 : 0;
$greaterLunarAdjustFactor = $greaterLunar['is_leap'] || (0 < $leapMonth && $leapMonth < $greaterLunar['lunar_month']) ? 1 : 0;
$diff = $greaterLunar['lunar_month'] + $greaterLunarAdjustFactor - $lessLunar['lunar_month'] - $lessLunarAdjustFactor;

} else {
$lessLunarLeapMonth = $this->leapMonth($lessLunar['lunar_year']);
$greaterLunarLeapMonth = $this->leapMonth($greaterLunar['lunar_year']);

$lessLunarAdjustFactor = (!$lessLunar['is_leap'] && $lessLunarLeapMonth == $lessLunar['lunar_month']) || $lessLunarLeapMonth > $lessLunar['lunar_month'] ? 1 : 0;
$diff += 12 + $lessLunarAdjustFactor - $lessLunar['lunar_month'];
for ($i = $lessLunar['lunar_year'] + 1; $i < $greaterLunar['lunar_year']; $i++) {
for ($i = $lessLunar['lunar_year'] + 1; $i < $greaterLunar['lunar_year']; ++$i) {
$diff += $this->monthsOfYear($i);
}
$greaterLunarAdjustFactor = $greaterLunar['is_leap'] || (0 < $greaterLunarLeapMonth && $greaterLunarLeapMonth < $greaterLunar['lunar_month']) ? 1 : 0;
Expand All @@ -898,9 +902,11 @@ public function diffInMonths($lunar1, $lunar2, $absolute = false)

/**
* 获取两个日期之间以日为单位的距离.
*
* @param array $lunar1
* @param array $lunar2
* @param bool $absolute
* @param bool $absolute
*
* @return int
*/
public function diffInDays($lunar1, $lunar2, $absolute = false)
Expand All @@ -915,7 +921,7 @@ public function diffInDays($lunar1, $lunar2, $absolute = false)
}

/**
* 增加年数
* 增加年数.
*
* @param array $lunar
* @param int $value
Expand Down Expand Up @@ -952,7 +958,7 @@ public function addYears($lunar, $value = 1, $overFlow = true)
}

/**
* 减少年数
* 减少年数.
*
* @param array $lunar
* @param int $value
Expand All @@ -966,7 +972,7 @@ public function subYears($lunar, $value = 1, $overFlow = true)
}

/**
* 增加月数
* 增加月数.
*
* @param array $lunar
* @param int $value
Expand All @@ -991,7 +997,7 @@ public function addMonths($lunar, $value = 1, $overFlow = true)
$isLeap = $newMonth + $value == $leapMonth + ($isLeap ? 0 : 1);

if ((!$currentIsLeap && $leapMonth == $newMonth) || ($newMonth < $leapMonth && $newMonth + $value > $leapMonth)) {
$value--;
--$value;
}
} else {
$isLeap = false;
Expand All @@ -1002,7 +1008,7 @@ public function addMonths($lunar, $value = 1, $overFlow = true)
$value = 0;
} else {
$value = $value + $newMonth - 13;
$newYear++;
++$newYear;
$newMonth = 1;
}

Expand All @@ -1011,7 +1017,7 @@ public function addMonths($lunar, $value = 1, $overFlow = true)
if ($newDay > $maxDays) {
if ($overFlow) {
$newDay = 1;
$value++;
++$value;
} else {
$newDay = $maxDays;
}
Expand All @@ -1024,7 +1030,7 @@ public function addMonths($lunar, $value = 1, $overFlow = true)
}

/**
* 减少月数
* 减少月数.
*
* @param array $lunar
* @param int $value
Expand All @@ -1050,7 +1056,7 @@ public function subMonths($lunar, $value = 1, $overFlow = true)
$isLeap = $newMonth - $value == $leapMonth;

if ($newMonth >= $leapMonth && $newMonth - $value < $leapMonth) {
$value--;
--$value;
}
} else {
$isLeap = false;
Expand All @@ -1061,7 +1067,7 @@ public function subMonths($lunar, $value = 1, $overFlow = true)
$value = 0;
} else {
$value = $value - $newMonth;
$newYear--;
--$newYear;
$newMonth = 12;
}

Expand All @@ -1078,12 +1084,13 @@ public function subMonths($lunar, $value = 1, $overFlow = true)
if ($needOverFlow) {
$ret = $this->addDays($ret, 1);
}

return $ret;
}
}

/**
* 增加天数
* 增加天数.
*
* @param array $lunar
* @param int $value
Expand All @@ -1094,12 +1101,13 @@ public function addDays($lunar, $value = 1)
{
$solar = $this->lunar2solar($lunar['lunar_year'], $lunar['lunar_month'], $lunar['lunar_day'], $lunar['is_leap']);
$date = $this->makeDate("{$solar['solar_year']}-{$solar['solar_month']}-{$solar['solar_day']}");
$date->modify($value . ' day');
$date->modify($value.' day');

return $this->solar2lunar($date->format('Y'), $date->format('m'), $date->format('d'));
}

/**
* 减少天数
* 减少天数.
*
* @param array $lunar
* @param int $value
Expand Down
Loading

0 comments on commit 0f3968d

Please sign in to comment.