Skip to content

Commit

Permalink
Merge pull request #24 from hao-li/master
Browse files Browse the repository at this point in the history
fix array index out range problem of ganZhiYear
  • Loading branch information
overtrue authored Aug 29, 2018
2 parents 0f3968d + 0470e00 commit 5fe5b64
Show file tree
Hide file tree
Showing 2 changed files with 1,216 additions and 1,192 deletions.
50 changes: 20 additions & 30 deletions src/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,27 +356,19 @@ public function solarDays($year, $month)
*/
public function ganZhiYear($lunarYear, $termIndex = null)
{
$ganKey = ($lunarYear - 3) % 10;
$zhiKey = ($lunarYear - 3) % 12;

// 如果余数为 0 则为最后一个天干
if (0 === $ganKey) {
$ganKey = 10;
}

// 如果余数为 0 则为最后一个地支
if (0 === $zhiKey) {
$zhiKey = 12;
}

if (null !== $termIndex) {
if (3 > $termIndex) {
$ganKey += 1;
$zhiKey += 1;
}
}

return $this->gan[$ganKey - 1].$this->zhi[$zhiKey - 1];
/**
* 据维基百科干支词条:『在西历新年后,华夏新年或干支历新年之前,则续用上一年之干支』
* 所以干支年份应该不需要根据节气校正,为免影响现有系统,此处暂时保留原有逻辑
* https://zh.wikipedia.org/wiki/%E5%B9%B2%E6%94%AF
*
* 即使考虑节气,有的年份没有立春,有的年份有两个立春,此处逻辑仍不能处理该特殊情况
*/
$adjust = null !== $termIndex && 3 > $termIndex ? 1 : 0;

$ganKey = ($lunarYear + $adjust - 4) % 10;
$zhiKey = ($lunarYear + $adjust - 4) % 12;

return $this->gan[$ganKey].$this->zhi[$zhiKey];
}

/**
Expand Down Expand Up @@ -510,12 +502,10 @@ public function toChinaDay($day)
*/
public function getAnimal($year, $termIndex = null)
{
$animalIndex = ($year - 4) % 12;
if (null !== $termIndex) {
if (3 > $termIndex) {
$animalIndex += 1;
}
}
// 认为此逻辑不需要,详情参见 ganZhiYear 相关注释
$adjust = null !== $termIndex && 3 > $termIndex ? 1 : 0;

$animalIndex = ($year + $adjust - 4) % 12;

return $this->animals[$animalIndex];
}
Expand Down Expand Up @@ -816,7 +806,7 @@ public function dateDiff($date1, $date2)
*
* @return int
*/
public function diffInYears($lunar1, $lunar2, $absolute = false)
public function diffInYears($lunar1, $lunar2, $absolute = true)
{
$solar1 = $this->lunar2solar($lunar1['lunar_year'], $lunar1['lunar_month'], $lunar1['lunar_day'], $lunar1['is_leap']);
$date1 = $this->makeDate("{$solar1['solar_year']}-{$solar1['solar_month']}-{$solar1['solar_day']}");
Expand Down Expand Up @@ -857,7 +847,7 @@ public function diffInYears($lunar1, $lunar2, $absolute = false)
*
* @return int
*/
public function diffInMonths($lunar1, $lunar2, $absolute = false)
public function diffInMonths($lunar1, $lunar2, $absolute = true)
{
$solar1 = $this->lunar2solar($lunar1['lunar_year'], $lunar1['lunar_month'], $lunar1['lunar_day'], $lunar1['is_leap']);
$date1 = $this->makeDate("{$solar1['solar_year']}-{$solar1['solar_month']}-{$solar1['solar_day']}");
Expand Down Expand Up @@ -909,7 +899,7 @@ public function diffInMonths($lunar1, $lunar2, $absolute = false)
*
* @return int
*/
public function diffInDays($lunar1, $lunar2, $absolute = false)
public function diffInDays($lunar1, $lunar2, $absolute = true)
{
$solar1 = $this->lunar2solar($lunar1['lunar_year'], $lunar1['lunar_month'], $lunar1['lunar_day'], $lunar1['is_leap']);
$date1 = $this->makeDate("{$solar1['solar_year']}-{$solar1['solar_month']}-{$solar1['solar_day']}");
Expand Down
Loading

0 comments on commit 5fe5b64

Please sign in to comment.