Skip to content

Commit

Permalink
v1.1.4 新增八字转公历时刻支持晚子时算当天;新增小六壬。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Oct 14, 2024
1 parent 05da16c commit 4bf9e56
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,7 @@
2. 修复:吉神宜趋、凶神宜忌错误。
3. 新增:Lunar的2种起运流派。
4. 新增:支持八字晚子时日柱算当天。

## [1.1.4] - 2024-10-14
1. 新增:八字转公历时刻支持晚子时算当天。
2. 新增:小六壬。
60 changes: 60 additions & 0 deletions src/culture/ren/MinorRen.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace com\tyme\culture\ren;


use com\tyme\culture\Element;
use com\tyme\culture\Luck;
use com\tyme\LoopTyme;

/**
* 小六壬
* @author 6tail
* @package com\tyme\culture\ren
*/
class MinorRen extends LoopTyme
{
static array $NAMES = ['大安', '留连', '速喜', '赤口', '小吉', '空亡'];

protected function __construct(int $index = null, string $name = null)
{
if ($index !== null) {
parent::__construct(self::$NAMES, $index);
} else if ($name !== null) {
parent::__construct(self::$NAMES, null, $name);
}
}

static function fromIndex(int $index): static
{
return new static($index);
}

static function fromName(string $name): static
{
return new static(null, $name);
}

function next(int $n): static
{
return self::fromIndex($this->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]);
}
}
2 changes: 1 addition & 1 deletion src/culture/star/six/SixStar.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use com\tyme\LoopTyme;

/**
* 六曜(孔明六曜星、小六壬
* 六曜(孔明六曜星)
* @author 6tail
* @package com\tyme\culture\star\six
*/
Expand Down
43 changes: 26 additions & 17 deletions src/eightchar/EightChar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 10 additions & 0 deletions src/lunar/LunarDay.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
10 changes: 10 additions & 0 deletions src/lunar/LunarHour.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
12 changes: 11 additions & 1 deletion src/lunar/LunarMonth.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}
Expand Down Expand Up @@ -368,4 +369,13 @@ function getFetus(): FetusMonth
{
return FetusMonth::fromLunarMonth($this);
}

/**
* 小六壬
* @return MinorRen 小六壬
*/
function getMinorRen(): MinorRen
{
return MinorRen::fromIndex(($this->month - 1) % 6);
}
}
19 changes: 19 additions & 0 deletions test/EightCharTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
6 changes: 6 additions & 0 deletions test/LunarDayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
6 changes: 6 additions & 0 deletions test/LunarHourTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 4bf9e56

Please sign in to comment.