diff --git a/CHANGELOG.md b/CHANGELOG.md index da47150..d6621f0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,3 +44,7 @@ 2. 修复:吉神宜趋、凶神宜忌错误。 3. 新增:Lunar的2种起运流派。 4. 新增:支持八字晚子时日柱算当天。 + +## [1.1.4] - 2024-10-14 +1. 新增:八字转公历时刻支持晚子时算当天。 +2. 新增:小六壬。 diff --git a/src/culture/ren/MinorRen.php b/src/culture/ren/MinorRen.php new file mode 100644 index 0000000..d016683 --- /dev/null +++ b/src/culture/ren/MinorRen.php @@ -0,0 +1,60 @@ +nextIndex($n)); + } + + /** + * 吉凶 + * @return Luck 吉凶 + */ + function getLuck(): Luck + { + return Luck::fromIndex($this->index % 2); + } + + /** + * 五行 + * @return Element 五行 + */ + function getElement(): Element + { + return Element::fromIndex([0, 4, 1, 3, 0, 2][$this->index]); + } +} diff --git a/src/culture/star/six/SixStar.php b/src/culture/star/six/SixStar.php index 458034b..1264f91 100644 --- a/src/culture/star/six/SixStar.php +++ b/src/culture/star/six/SixStar.php @@ -6,7 +6,7 @@ use com\tyme\LoopTyme; /** - * 六曜(孔明六曜星、小六壬) + * 六曜(孔明六曜星) * @author 6tail * @package com\tyme\culture\star\six */ diff --git a/src/eightchar/EightChar.php b/src/eightchar/EightChar.php index bd90b7e..3706a66 100644 --- a/src/eightchar/EightChar.php +++ b/src/eightchar/EightChar.php @@ -166,33 +166,42 @@ function getSolarTimes(int $startYear, int $endYear): array $y = $this->year->next(-57)->getIndex() + 1; // 节令偏移值 $m *= 2; - // 时辰地支转时刻,子时按零点算 + // 时辰地支转时刻 $h = $this->hour->getEarthBranch()->getIndex() * 2; + // 兼容子时多流派 + $hours = [$h]; + if ($h == 0) { + $hours[] = 23; + } $baseYear = $startYear - 1; + if ($baseYear > $y) { + $y += 60 * (int)ceil(($baseYear - $y) / 60.0); + } while ($y <= $endYear) { - if ($y >= $baseYear) { - // 立春为寅月的开始 - $term = SolarTerm::fromIndex($y, 3); - // 节令推移,年干支和月干支就都匹配上了 - if ($m > 0) { - $term = $term->next($m); + // 立春为寅月的开始 + $term = SolarTerm::fromIndex($y, 3); + // 节令推移,年干支和月干支就都匹配上了 + if ($m > 0) { + $term = $term->next($m); + } + $solarTime = $term->getJulianDay()->getSolarTime(); + if ($solarTime->getYear() >= $startYear) { + // 日干支和节令干支的偏移值 + $solarDay = $solarTime->getSolarDay(); + $d = $this->day->next(-$solarDay->getLunarDay()->getSixtyCycle()->getIndex())->getIndex(); + if ($d > 0) { + // 从节令推移天数 + $solarDay = $solarDay->next($d); } - $solarTime = $term->getJulianDay()->getSolarTime(); - if ($solarTime->getYear() >= $startYear) { + foreach ($hours as $hour) { $mi = 0; $s = 0; - // 日干支和节令干支的偏移值 - $solarDay = $solarTime->getSolarDay(); - $d = $this->day->next(-$solarDay->getLunarDay()->getSixtyCycle()->getIndex())->getIndex(); - if ($d > 0) { - // 从节令推移天数 - $solarDay = $solarDay->next($d); - } else if ($h == $solarTime->getHour()) { + if ($d == 0 && $hour == $solarTime->getHour()) { // 如果正好是节令当天,且小时和节令的小时数相等的极端情况,把分钟和秒钟带上 $mi = $solarTime->getMinute(); $s = $solarTime->getSecond(); } - $time = SolarTime::fromYmdHms($solarDay->getYear(), $solarDay->getMonth(), $solarDay->getDay(), $h, $mi, $s); + $time = SolarTime::fromYmdHms($solarDay->getYear(), $solarDay->getMonth(), $solarDay->getDay(), $hour, $mi, $s); // 验证一下 if ($time->getLunarHour()->getEightChar()->equals($this)) { $l[] = $time; diff --git a/src/lunar/LunarDay.php b/src/lunar/LunarDay.php index d977750..e50b678 100644 --- a/src/lunar/LunarDay.php +++ b/src/lunar/LunarDay.php @@ -10,6 +10,7 @@ use com\tyme\culture\fetus\FetusDay; use com\tyme\culture\God; use com\tyme\culture\Phase; +use com\tyme\culture\ren\MinorRen; use com\tyme\culture\star\nine\NineStar; use com\tyme\culture\star\six\SixStar; use com\tyme\culture\star\twelve\TwelveStar; @@ -383,4 +384,13 @@ function getSixStar(): SixStar { return SixStar::fromIndex(($this->month->getMonth() + $this->day - 2) % 6); } + + /** + * 小六壬 + * @return MinorRen 小六壬 + */ + function getMinorRen(): MinorRen + { + return $this->getLunarMonth()->getMinorRen()->next($this->day - 1); + } } diff --git a/src/lunar/LunarHour.php b/src/lunar/LunarHour.php index 1ee9728..1140a5f 100644 --- a/src/lunar/LunarHour.php +++ b/src/lunar/LunarHour.php @@ -4,6 +4,7 @@ use com\tyme\AbstractTyme; +use com\tyme\culture\ren\MinorRen; use com\tyme\culture\star\nine\NineStar; use com\tyme\culture\star\twelve\TwelveStar; use com\tyme\culture\Taboo; @@ -345,4 +346,13 @@ function getAvoids(): array { return Taboo::getHourAvoids($this->getDaySixtyCycle(), $this->getSixtyCycle()); } + + /** + * 小六壬 + * @return MinorRen 小六壬 + */ + function getMinorRen(): MinorRen + { + return $this->getLunarDay()->getMinorRen()->next($this->getIndexInDay()); + } } diff --git a/src/lunar/LunarMonth.php b/src/lunar/LunarMonth.php index 01a667f..a94d73d 100644 --- a/src/lunar/LunarMonth.php +++ b/src/lunar/LunarMonth.php @@ -6,6 +6,7 @@ use com\tyme\AbstractTyme; use com\tyme\culture\Direction; use com\tyme\culture\fetus\FetusMonth; +use com\tyme\culture\ren\MinorRen; use com\tyme\culture\star\nine\NineStar; use com\tyme\jd\JulianDay; use com\tyme\sixtycycle\EarthBranch; @@ -122,7 +123,7 @@ protected function __construct(int $year, int $month, array $cache = null) static function fromYm(int $year, int $month): static { $c = null; - $key = sprintf("%d%d", $year, $month); + $key = sprintf('%d%d', $year, $month); if (!empty(static::$cache[$key])) { $c = static::$cache[$key]; } @@ -368,4 +369,13 @@ function getFetus(): FetusMonth { return FetusMonth::fromLunarMonth($this); } + + /** + * 小六壬 + * @return MinorRen 小六壬 + */ + function getMinorRen(): MinorRen + { + return MinorRen::fromIndex(($this->month - 1) % 6); + } } diff --git a/test/EightCharTest.php b/test/EightCharTest.php index e2685fd..73cc6bc 100644 --- a/test/EightCharTest.php +++ b/test/EightCharTest.php @@ -4,6 +4,8 @@ use com\tyme\eightchar\EightChar; use com\tyme\eightchar\provider\impl\China95ChildLimitProvider; use com\tyme\eightchar\provider\impl\DefaultChildLimitProvider; +use com\tyme\eightchar\provider\impl\DefaultEightCharProvider; +use com\tyme\eightchar\provider\impl\LunarSect2EightCharProvider; use com\tyme\enums\Gender; use com\tyme\lunar\LunarHour; use com\tyme\sixtycycle\HeavenStem; @@ -647,4 +649,21 @@ public function test42() $expected = array('1903年3月29日 18:00:00', '1963年3月14日 18:00:00'); $this->assertEquals($expected, $actual); } + + public function test46() + { + LunarHour::$provider = new LunarSect2EightCharProvider(); + + $eightChar = new EightChar('壬寅', '丙午', '己亥', '丙子'); + $solarTimes = $eightChar->getSolarTimes(1900, 2024); + $actual = array(); + foreach ($solarTimes as $solarTime) { + $actual[] = $solarTime->__toString(); + } + + $expected = array('1962年6月30日 23:00:00', '2022年6月15日 23:00:00'); + $this->assertEquals($expected, $actual); + + LunarHour::$provider = new DefaultEightCharProvider(); + } } diff --git a/test/LunarDayTest.php b/test/LunarDayTest.php index 07d6e69..ae08726 100644 --- a/test/LunarDayTest.php +++ b/test/LunarDayTest.php @@ -163,4 +163,10 @@ function test26() $this->assertEquals('戊子', $lunar->getLunarMonth()->getSixtyCycle()->getName()); $this->assertEquals('戊子', $lunar->getMonthSixtyCycle()->getName()); } + + function test28() + { + $lunar = LunarDay::fromYmd(2024, 3, 5); + $this->assertEquals('大安', $lunar->getMinorRen()->getName()); + } } diff --git a/test/LunarHourTest.php b/test/LunarHourTest.php index 137add5..0ca0938 100644 --- a/test/LunarHourTest.php +++ b/test/LunarHourTest.php @@ -93,4 +93,10 @@ function test9() $this->assertEquals('农历癸卯年', $h->getLunarDay()->getLunarMonth()->getLunarYear()->__toString()); $this->assertEquals('癸卯', $h->getLunarDay()->getLunarMonth()->getLunarYear()->getSixtyCycle()->getName()); } + + function test28() + { + $h = LunarHour::fromYmdHms(2024, 9, 7, 10, 0, 0); + $this->assertEquals('留连', $h->getMinorRen()->getName()); + } }