Skip to content

Commit

Permalink
v1.0.1 童限计算支持自定义,增加元亨利贞的起运算法;增加八字转公历时刻。
Browse files Browse the repository at this point in the history
  • Loading branch information
6tail committed Feb 21, 2024
1 parent 7014e23 commit 19245db
Show file tree
Hide file tree
Showing 9 changed files with 530 additions and 103 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Changelog

## [1.0.1] - 2024-02-21
1. 童限计算支持自定义,增加元亨利贞的起运算法。
2. 增加八字转公历时刻。
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@
"节气",
"法定假日"
],
"description": "a calendar library"
"description": "Tyme是一个非常强大的日历工具库,可以看作 Lunar 的升级版,拥有更优的设计和扩展性,支持公历和农历、星座、干支、生肖、节气、法定假日等。"
}
111 changes: 26 additions & 85 deletions src/eightchar/ChildLimit.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@
namespace com\tyme\eightchar;


use com\tyme\eightchar\provider\ChildLimitProvider;
use com\tyme\eightchar\provider\impl\DefaultChildLimitProvider;
use com\tyme\enums\Gender;
use com\tyme\enums\YinYang;
use com\tyme\solar\SolarMonth;
use com\tyme\solar\SolarTime;

/**
Expand All @@ -16,15 +17,9 @@
class ChildLimit
{
/**
* @var SolarTime 开始(即出生)的公历时刻
* @var ChildLimitProvider|null 童限计算接口
*/
protected SolarTime $startTime;

/**
* @var SolarTime 结束(即开始起运)的公历时刻
*/
protected SolarTime $endTime;

static ?ChildLimitProvider $provider = null;
/**
* @var EightChar 八字
*/
Expand All @@ -36,93 +31,39 @@ class ChildLimit
protected Gender $gender;

/**
* @var int 年数
*/
protected int $yearCount;

/**
* @var int 月数
*/
protected int $monthCount;

/**
* @var int 日数
*/
protected int $dayCount;

/**
* @var int 小时数
* @var bool 顺逆
*/
protected int $hourCount;
protected bool $forward;

/**
* @var int 分钟数
* @var ChildLimitInfo 童限信息
*/
protected int $minuteCount;
protected ChildLimitInfo $info;

/**
* @var bool 顺逆
*/
protected bool $forward;
private static function init(): void
{
self::$provider = new DefaultChildLimitProvider();
}

protected function __construct(SolarTime $birthTime, Gender $gender)
{
$this->startTime = $birthTime;
if (null == self::$provider) {
self::init();
}
$this->gender = $gender;
$this->eightChar = $birthTime->getLunarHour()->getEightChar();
// 阳男阴女顺推,阴男阳女逆推
$yang = YinYang::YANG == $this->eightChar->getYear()->getHeavenStem()->getYinYang();
$man = Gender::MAN == $gender;
$forward = ($yang && $man) || (!$yang && !$man);
$this->forward = ($yang && $man) || (!$yang && !$man);
$term = $birthTime->getTerm();
if (!$term->isJie()) {
$term = $term->next(-1);
}
$start = $forward ? $birthTime : $term->getJulianDay()->getSolarTime();
$end = $forward ? $term->next(2)->getJulianDay()->getSolarTime() : $birthTime;

$seconds = $end->subtract($start);
// 3天 = 1年,3天=60*60*24*3秒=259200秒 = 1年
$year = intdiv($seconds, 259200);
$seconds %= 259200;
// 1天 = 4月,1天=60*60*24秒=86400秒 = 4月,85400秒/4=21600秒 = 1月
$month = intdiv($seconds, 21600);
$seconds %= 21600;
// 1时 = 5天,1时=60*60秒=3600秒 = 5天,3600秒/5=720秒 = 1天
$day = intdiv($seconds, 720);
$seconds %= 720;
// 1分 = 2时,60秒 = 2时,60秒/2=30秒 = 1时
$hour = intdiv($seconds, 30);
$seconds %= 30;
// 1秒 = 2分,1秒/2=0.5秒 = 1分
$minute = $seconds * 2;

$this->forward = $forward;
$this->yearCount = $year;
$this->monthCount = $month;
$this->dayCount = $day;
$this->hourCount = $hour;
$this->minuteCount = $minute;

$birthday = $birthTime->getDay();
$birthMonth = $birthday->getMonth();

$d = $birthday->getDay() + $day;
$h = $birthTime->getHour() + $hour;
$mi = $birthTime->getMinute() + $minute;
$h += intdiv($mi, 60);
$mi %= 60;
$d += intdiv($h, 24);
$h %= 24;

$sm = SolarMonth::fromYm($birthMonth->getYear()->getYear() + $year, $birthMonth->getMonth())->next($month);

$dc = $sm->getDayCount();
if ($d > $dc) {
$d -= $dc;
$sm = $sm->next(1);
if ($this->forward) {
$term = $term->next(2);
}
$this->endTime = SolarTime::fromYmdHms($sm->getYear()->getYear(), $sm->getMonth(), $d, $h, $mi, $birthTime->getSecond());
$this->info = self::$provider->getInfo($birthTime, $term);
}

/**
Expand Down Expand Up @@ -174,7 +115,7 @@ function isForward(): bool
*/
function getYearCount(): int
{
return $this->yearCount;
return $this->info->getYearCount();
}

/**
Expand All @@ -184,7 +125,7 @@ function getYearCount(): int
*/
function getMonthCount(): int
{
return $this->monthCount;
return $this->info->getMonthCount();
}

/**
Expand All @@ -194,7 +135,7 @@ function getMonthCount(): int
*/
function getDayCount(): int
{
return $this->dayCount;
return $this->info->getDayCount();
}

/**
Expand All @@ -204,7 +145,7 @@ function getDayCount(): int
*/
function getHourCount(): int
{
return $this->hourCount;
return $this->info->getHourCount();
}

/**
Expand All @@ -214,7 +155,7 @@ function getHourCount(): int
*/
function getMinuteCount(): int
{
return $this->minuteCount;
return $this->info->getMinuteCount();
}

/**
Expand All @@ -224,7 +165,7 @@ function getMinuteCount(): int
*/
function getStartTime(): SolarTime
{
return $this->startTime;
return $this->info->getStartTime();
}

/**
Expand All @@ -234,7 +175,7 @@ function getStartTime(): SolarTime
*/
function getEndTime(): SolarTime
{
return $this->endTime;
return $this->info->getEndTime();
}

/**
Expand Down
142 changes: 142 additions & 0 deletions src/eightchar/ChildLimitInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
<?php

namespace com\tyme\eightchar;


use com\tyme\solar\SolarTime;

/**
* 童限信息
* @author 6tail
* @package com\tyme\eightchar
*/
class ChildLimitInfo
{
/**
* @var SolarTime 开始(即出生)的公历时刻
*/
protected SolarTime $startTime;

/**
* @var SolarTime 结束(即开始起运)的公历时刻
*/
protected SolarTime $endTime;

/**
* @var int 年数
*/
protected int $yearCount;

/**
* @var int 月数
*/
protected int $monthCount;

/**
* @var int 日数
*/
protected int $dayCount;

/**
* @var int 小时数
*/
protected int $hourCount;

/**
* @var int 分钟数
*/
protected int $minuteCount;

/**
* 初始化
* @param SolarTime $startTime 开始(即出生)的公历时刻
* @param SolarTime $endTime 结束(即开始起运)的公历时刻
* @param int $yearCount 年数
* @param int $monthCount 月数
* @param int $dayCount 日数
* @param int $hourCount 小时数
* @param int $minuteCount 分钟数
*/
public function __construct(SolarTime $startTime, SolarTime $endTime, int $yearCount, int $monthCount, int $dayCount, int $hourCount, int $minuteCount)
{
$this->startTime = $startTime;
$this->endTime = $endTime;
$this->yearCount = $yearCount;
$this->monthCount = $monthCount;
$this->dayCount = $dayCount;
$this->hourCount = $hourCount;
$this->minuteCount = $minuteCount;
}


/**
* 年数
*
* @return int 年数
*/
function getYearCount(): int
{
return $this->yearCount;
}

/**
* 月数
*
* @return int 月数
*/
function getMonthCount(): int
{
return $this->monthCount;
}

/**
* 日数
*
* @return int 日数
*/
function getDayCount(): int
{
return $this->dayCount;
}

/**
* 小时数
*
* @return int 小时数
*/
function getHourCount(): int
{
return $this->hourCount;
}

/**
* 分钟数
*
* @return int 分钟数
*/
function getMinuteCount(): int
{
return $this->minuteCount;
}

/**
* 开始(即出生)的公历时刻
*
* @return SolarTime 公历时刻
*/
function getStartTime(): SolarTime
{
return $this->startTime;
}

/**
* 结束(即开始起运)的公历时刻
*
* @return SolarTime 公历时刻
*/
function getEndTime(): SolarTime
{
return $this->endTime;
}

}
Loading

0 comments on commit 19245db

Please sign in to comment.