Skip to content

Commit 1f29ed3

Browse files
committed
v1.3.3 修复极端情况下八字转公历时刻出现遗漏的问题;优化代码。
1 parent 153e5fd commit 1f29ed3

File tree

12 files changed

+84
-59
lines changed

12 files changed

+84
-59
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,7 @@
9191
2. 修复:干支月推移错误。
9292
3. 优化:宜忌。
9393
4. 修改:2018之后的△T参数。
94+
95+
## [1.3.3] - 2025-09-08
96+
1. 修复:极端情况下八字转公历时刻出现遗漏的问题。
97+
2. 优化:代码。

src/culture/fetus/FetusDay.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ function getName(): string
7171

7272
$s .= ' ';
7373

74-
$directionName = $this->direction->getName();
7574
if (Side::IN == $this->side) {
7675
$s .= '';
7776
}
7877
$s .= $this->side->getName();
7978

79+
$directionName = $this->direction->getName();
8080
if (Side::OUT == $this->side && str_contains('北南西东', $directionName)) {
8181
$s .= '';
8282
}

src/eightchar/EightChar.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ function getSolarTimes(int $startYear, int $endYear): array
217217
$s = $solarTime->getSecond();
218218
}
219219
$time = SolarTime::fromYmdHms($solarDay->getYear(), $solarDay->getMonth(), $solarDay->getDay(), $hour, $mi, $s);
220+
if ($d == 30) {
221+
$time = $time->next(-3600);
222+
}
220223
// 验证一下
221224
if ($time->getLunarHour()->getEightChar()->equals($this)) {
222225
$l[] = $time;

src/jd/JulianDay.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ static function fromYmdHms(int $year, int $month, int $day, int $hour, int $minu
4545
$year--;
4646
}
4747
if ($g) {
48-
$n = intdiv($year, 100);
49-
$n = 2 - $n + intdiv($n, 4);
48+
$n = (int)($year * 0.01);
49+
$n = 2 - $n + (int)($n * 0.25);
5050
}
5151
return static::fromJulianDay((int)(365.25 * ($year + 4716)) + (int)(30.6001 * ($month + 1)) + $d + $n - 1524.5);
5252
}
@@ -88,26 +88,26 @@ function getSolarDay(): SolarDay
8888
*/
8989
function getSolarTime(): SolarTime
9090
{
91-
$d = (int)($this->day + 0.5);
92-
$f = $this->day + 0.5 - $d;
91+
$n = (int)($this->day + 0.5);
92+
$f = $this->day + 0.5 - $n;
9393

94-
if ($d >= 2299161) {
95-
$c = (int)(($d - 1867216.25) / 36524.25);
96-
$d += 1 + $c - intdiv($c, 4);
94+
if ($n >= 2299161) {
95+
$c = (int)(($n - 1867216.25) / 36524.25);
96+
$n += 1 + $c - intdiv($c, 4);
9797
}
98-
$d += 1524;
99-
$year = (int)(($d - 122.1) / 365.25);
100-
$d -= (int)(365.25 * $year);
101-
$month = (int)($d / 30.601);
102-
$d -= (int)(30.601 * $month);
103-
$day = $d;
104-
if ($month > 13) {
105-
$month -= 12;
98+
$n += 1524;
99+
$y = (int)(($n - 122.1) / 365.25);
100+
$n -= (int)(365.25 * $y);
101+
$m = (int)($n / 30.601);
102+
$n -= (int)(30.601 * $m);
103+
$d = $n;
104+
if ($m > 13) {
105+
$m -= 12;
106106
} else {
107-
$year -= 1;
107+
$y -= 1;
108108
}
109-
$month -= 1;
110-
$year -= 4715;
109+
$m -= 1;
110+
$y -= 4715;
111111
$f *= 24;
112112
$hour = (int)$f;
113113

@@ -118,7 +118,7 @@ function getSolarTime(): SolarTime
118118
$f -= $minute;
119119
$f *= 60;
120120
$second = (int)round($f);
121-
return $second < 60 ? SolarTime::fromYmdHms($year, $month, $day, $hour, $minute, $second) : SolarTime::fromYmdHms($year, $month, $day, $hour, $minute, $second - 60)->next(60);
121+
return $second < 60 ? SolarTime::fromYmdHms($y, $m, $d, $hour, $minute, $second) : SolarTime::fromYmdHms($y, $m, $d, $hour, $minute, $second - 60)->next(60);
122122
}
123123

124124
/**

src/lunar/LunarDay.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,11 +243,9 @@ function getNineStar(): NineStar
243243
{
244244
$d = $this->getSolarDay();
245245
$dongZhi = SolarTerm::fromIndex($d->getYear(), 0);
246-
$xiaZhi = $dongZhi->next(12);
247-
$dongZhi2 = $dongZhi->next(24);
248246
$dongZhiSolar = $dongZhi->getJulianDay()->getSolarDay();
249-
$xiaZhiSolar = $xiaZhi->getJulianDay()->getSolarDay();
250-
$dongZhiSolar2 = $dongZhi2->getJulianDay()->getSolarDay();
247+
$xiaZhiSolar = $dongZhi->next(12)->getJulianDay()->getSolarDay();
248+
$dongZhiSolar2 = $dongZhi->next(24)->getJulianDay()->getSolarDay();
251249
$dongZhiIndex = $dongZhiSolar->getLunarDay()->getSixtyCycle()->getIndex();
252250
$xiaZhiIndex = $xiaZhiSolar->getLunarDay()->getSixtyCycle()->getIndex();
253251
$dongZhiIndex2 = $dongZhiSolar2->getLunarDay()->getSixtyCycle()->getIndex();

src/lunar/LunarHour.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,7 @@ function getSixtyCycle(): SixtyCycle
273273
if ($this->hour >= 23) {
274274
$d = $d->next(1);
275275
}
276-
$heavenStemIndex = $d->getHeavenStem()->getIndex() % 5 * 2 + $earthBranchIndex;
277-
return SixtyCycle::fromName(sprintf('%s%s', HeavenStem::fromIndex($heavenStemIndex)->getName(), EarthBranch::fromIndex($earthBranchIndex)->getName()));
276+
return SixtyCycle::fromName(sprintf('%s%s', HeavenStem::fromIndex($d->getHeavenStem()->getIndex() % 5 * 2 + $earthBranchIndex)->getName(), EarthBranch::fromIndex($earthBranchIndex)->getName()));
278277
}
279278

280279
/**
@@ -296,14 +295,14 @@ function getNineStar(): NineStar
296295
{
297296
$solar = $this->day->getSolarDay();
298297
$dongZhi = SolarTerm::fromIndex($solar->getYear(), 0);
299-
$xiaZhi = $dongZhi->next(12);
300-
$asc = !$solar->isBefore($dongZhi->getJulianDay()->getSolarDay()) && $solar->isBefore($xiaZhi->getJulianDay()->getSolarDay());
301-
$start = [8, 5, 2][$this->day->getSixtyCycle()->getEarthBranch()->getIndex() % 3];
302-
if ($asc) {
303-
$start = 8 - $start;
304-
}
305298
$earthBranchIndex = $this->getIndexInDay() % 12;
306-
return NineStar::fromIndex($start + ($asc ? $earthBranchIndex : -$earthBranchIndex));
299+
$index = [8, 5, 2][$this->day->getSixtyCycle()->getEarthBranch()->getIndex() % 3];
300+
if (!$solar->isBefore($dongZhi->getJulianDay()->getSolarDay()) && $solar->isBefore($dongZhi->next(12)->getJulianDay()->getSolarDay())) {
301+
$index = 8 + $earthBranchIndex - $index;
302+
} else {
303+
$index -= $earthBranchIndex;
304+
}
305+
return NineStar::fromIndex($index);
307306
}
308307

309308
/**

src/lunar/LunarMonth.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ protected function __construct(int $year, int $month, ?array $cache = null)
8484
}
8585

8686
// 冬至
87-
$dongZhi = SolarTerm::fromIndex($year, 0);
88-
$dongZhiJd = $dongZhi->getCursoryJulianDay();
87+
$dongZhiJd = SolarTerm::fromIndex($year, 0)->getCursoryJulianDay();
8988

9089
// 冬至前的初一,今年首朔的日月黄经差
9190
$w = ShouXingUtil::calcShuo($dongZhiJd);
@@ -359,7 +358,7 @@ function getJupiterDirection(): Direction
359358
{
360359
$sixtyCycle = $this->getSixtyCycle();
361360
$n = [7, -1, 1, 3][$sixtyCycle->getEarthBranch()->next(-2)->getIndex() % 4];
362-
return $n == -1 ? $sixtyCycle->getHeavenStem()->getDirection() : Direction::fromIndex($n);
361+
return $n != -1 ? Direction::fromIndex($n) : $sixtyCycle->getHeavenStem()->getDirection();
363362
}
364363

365364
/**

0 commit comments

Comments
 (0)