Skip to content

Commit 19245db

Browse files
committed
v1.0.1 童限计算支持自定义,增加元亨利贞的起运算法;增加八字转公历时刻。
1 parent 7014e23 commit 19245db

File tree

9 files changed

+530
-103
lines changed

9 files changed

+530
-103
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Changelog
2+
3+
## [1.0.1] - 2024-02-21
4+
1. 童限计算支持自定义,增加元亨利贞的起运算法。
5+
2. 增加八字转公历时刻。

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
"节气",
3333
"法定假日"
3434
],
35-
"description": "a calendar library"
35+
"description": "Tyme是一个非常强大的日历工具库,可以看作 Lunar 的升级版,拥有更优的设计和扩展性,支持公历和农历、星座、干支、生肖、节气、法定假日等。"
3636
}

src/eightchar/ChildLimit.php

Lines changed: 26 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
namespace com\tyme\eightchar;
44

55

6+
use com\tyme\eightchar\provider\ChildLimitProvider;
7+
use com\tyme\eightchar\provider\impl\DefaultChildLimitProvider;
68
use com\tyme\enums\Gender;
79
use com\tyme\enums\YinYang;
8-
use com\tyme\solar\SolarMonth;
910
use com\tyme\solar\SolarTime;
1011

1112
/**
@@ -16,15 +17,9 @@
1617
class ChildLimit
1718
{
1819
/**
19-
* @var SolarTime 开始(即出生)的公历时刻
20+
* @var ChildLimitProvider|null 童限计算接口
2021
*/
21-
protected SolarTime $startTime;
22-
23-
/**
24-
* @var SolarTime 结束(即开始起运)的公历时刻
25-
*/
26-
protected SolarTime $endTime;
27-
22+
static ?ChildLimitProvider $provider = null;
2823
/**
2924
* @var EightChar 八字
3025
*/
@@ -36,93 +31,39 @@ class ChildLimit
3631
protected Gender $gender;
3732

3833
/**
39-
* @var int 年数
40-
*/
41-
protected int $yearCount;
42-
43-
/**
44-
* @var int 月数
45-
*/
46-
protected int $monthCount;
47-
48-
/**
49-
* @var int 日数
50-
*/
51-
protected int $dayCount;
52-
53-
/**
54-
* @var int 小时数
34+
* @var bool 顺逆
5535
*/
56-
protected int $hourCount;
36+
protected bool $forward;
5737

5838
/**
59-
* @var int 分钟数
39+
* @var ChildLimitInfo 童限信息
6040
*/
61-
protected int $minuteCount;
41+
protected ChildLimitInfo $info;
6242

63-
/**
64-
* @var bool 顺逆
65-
*/
66-
protected bool $forward;
43+
private static function init(): void
44+
{
45+
self::$provider = new DefaultChildLimitProvider();
46+
}
6747

6848
protected function __construct(SolarTime $birthTime, Gender $gender)
6949
{
70-
$this->startTime = $birthTime;
50+
if (null == self::$provider) {
51+
self::init();
52+
}
7153
$this->gender = $gender;
7254
$this->eightChar = $birthTime->getLunarHour()->getEightChar();
7355
// 阳男阴女顺推,阴男阳女逆推
7456
$yang = YinYang::YANG == $this->eightChar->getYear()->getHeavenStem()->getYinYang();
7557
$man = Gender::MAN == $gender;
76-
$forward = ($yang && $man) || (!$yang && !$man);
58+
$this->forward = ($yang && $man) || (!$yang && !$man);
7759
$term = $birthTime->getTerm();
7860
if (!$term->isJie()) {
7961
$term = $term->next(-1);
8062
}
81-
$start = $forward ? $birthTime : $term->getJulianDay()->getSolarTime();
82-
$end = $forward ? $term->next(2)->getJulianDay()->getSolarTime() : $birthTime;
83-
84-
$seconds = $end->subtract($start);
85-
// 3天 = 1年,3天=60*60*24*3秒=259200秒 = 1年
86-
$year = intdiv($seconds, 259200);
87-
$seconds %= 259200;
88-
// 1天 = 4月,1天=60*60*24秒=86400秒 = 4月,85400秒/4=21600秒 = 1月
89-
$month = intdiv($seconds, 21600);
90-
$seconds %= 21600;
91-
// 1时 = 5天,1时=60*60秒=3600秒 = 5天,3600秒/5=720秒 = 1天
92-
$day = intdiv($seconds, 720);
93-
$seconds %= 720;
94-
// 1分 = 2时,60秒 = 2时,60秒/2=30秒 = 1时
95-
$hour = intdiv($seconds, 30);
96-
$seconds %= 30;
97-
// 1秒 = 2分,1秒/2=0.5秒 = 1分
98-
$minute = $seconds * 2;
99-
100-
$this->forward = $forward;
101-
$this->yearCount = $year;
102-
$this->monthCount = $month;
103-
$this->dayCount = $day;
104-
$this->hourCount = $hour;
105-
$this->minuteCount = $minute;
106-
107-
$birthday = $birthTime->getDay();
108-
$birthMonth = $birthday->getMonth();
109-
110-
$d = $birthday->getDay() + $day;
111-
$h = $birthTime->getHour() + $hour;
112-
$mi = $birthTime->getMinute() + $minute;
113-
$h += intdiv($mi, 60);
114-
$mi %= 60;
115-
$d += intdiv($h, 24);
116-
$h %= 24;
117-
118-
$sm = SolarMonth::fromYm($birthMonth->getYear()->getYear() + $year, $birthMonth->getMonth())->next($month);
119-
120-
$dc = $sm->getDayCount();
121-
if ($d > $dc) {
122-
$d -= $dc;
123-
$sm = $sm->next(1);
63+
if ($this->forward) {
64+
$term = $term->next(2);
12465
}
125-
$this->endTime = SolarTime::fromYmdHms($sm->getYear()->getYear(), $sm->getMonth(), $d, $h, $mi, $birthTime->getSecond());
66+
$this->info = self::$provider->getInfo($birthTime, $term);
12667
}
12768

12869
/**
@@ -174,7 +115,7 @@ function isForward(): bool
174115
*/
175116
function getYearCount(): int
176117
{
177-
return $this->yearCount;
118+
return $this->info->getYearCount();
178119
}
179120

180121
/**
@@ -184,7 +125,7 @@ function getYearCount(): int
184125
*/
185126
function getMonthCount(): int
186127
{
187-
return $this->monthCount;
128+
return $this->info->getMonthCount();
188129
}
189130

190131
/**
@@ -194,7 +135,7 @@ function getMonthCount(): int
194135
*/
195136
function getDayCount(): int
196137
{
197-
return $this->dayCount;
138+
return $this->info->getDayCount();
198139
}
199140

200141
/**
@@ -204,7 +145,7 @@ function getDayCount(): int
204145
*/
205146
function getHourCount(): int
206147
{
207-
return $this->hourCount;
148+
return $this->info->getHourCount();
208149
}
209150

210151
/**
@@ -214,7 +155,7 @@ function getHourCount(): int
214155
*/
215156
function getMinuteCount(): int
216157
{
217-
return $this->minuteCount;
158+
return $this->info->getMinuteCount();
218159
}
219160

220161
/**
@@ -224,7 +165,7 @@ function getMinuteCount(): int
224165
*/
225166
function getStartTime(): SolarTime
226167
{
227-
return $this->startTime;
168+
return $this->info->getStartTime();
228169
}
229170

230171
/**
@@ -234,7 +175,7 @@ function getStartTime(): SolarTime
234175
*/
235176
function getEndTime(): SolarTime
236177
{
237-
return $this->endTime;
178+
return $this->info->getEndTime();
238179
}
239180

240181
/**

src/eightchar/ChildLimitInfo.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace com\tyme\eightchar;
4+
5+
6+
use com\tyme\solar\SolarTime;
7+
8+
/**
9+
* 童限信息
10+
* @author 6tail
11+
* @package com\tyme\eightchar
12+
*/
13+
class ChildLimitInfo
14+
{
15+
/**
16+
* @var SolarTime 开始(即出生)的公历时刻
17+
*/
18+
protected SolarTime $startTime;
19+
20+
/**
21+
* @var SolarTime 结束(即开始起运)的公历时刻
22+
*/
23+
protected SolarTime $endTime;
24+
25+
/**
26+
* @var int 年数
27+
*/
28+
protected int $yearCount;
29+
30+
/**
31+
* @var int 月数
32+
*/
33+
protected int $monthCount;
34+
35+
/**
36+
* @var int 日数
37+
*/
38+
protected int $dayCount;
39+
40+
/**
41+
* @var int 小时数
42+
*/
43+
protected int $hourCount;
44+
45+
/**
46+
* @var int 分钟数
47+
*/
48+
protected int $minuteCount;
49+
50+
/**
51+
* 初始化
52+
* @param SolarTime $startTime 开始(即出生)的公历时刻
53+
* @param SolarTime $endTime 结束(即开始起运)的公历时刻
54+
* @param int $yearCount 年数
55+
* @param int $monthCount 月数
56+
* @param int $dayCount 日数
57+
* @param int $hourCount 小时数
58+
* @param int $minuteCount 分钟数
59+
*/
60+
public function __construct(SolarTime $startTime, SolarTime $endTime, int $yearCount, int $monthCount, int $dayCount, int $hourCount, int $minuteCount)
61+
{
62+
$this->startTime = $startTime;
63+
$this->endTime = $endTime;
64+
$this->yearCount = $yearCount;
65+
$this->monthCount = $monthCount;
66+
$this->dayCount = $dayCount;
67+
$this->hourCount = $hourCount;
68+
$this->minuteCount = $minuteCount;
69+
}
70+
71+
72+
/**
73+
* 年数
74+
*
75+
* @return int 年数
76+
*/
77+
function getYearCount(): int
78+
{
79+
return $this->yearCount;
80+
}
81+
82+
/**
83+
* 月数
84+
*
85+
* @return int 月数
86+
*/
87+
function getMonthCount(): int
88+
{
89+
return $this->monthCount;
90+
}
91+
92+
/**
93+
* 日数
94+
*
95+
* @return int 日数
96+
*/
97+
function getDayCount(): int
98+
{
99+
return $this->dayCount;
100+
}
101+
102+
/**
103+
* 小时数
104+
*
105+
* @return int 小时数
106+
*/
107+
function getHourCount(): int
108+
{
109+
return $this->hourCount;
110+
}
111+
112+
/**
113+
* 分钟数
114+
*
115+
* @return int 分钟数
116+
*/
117+
function getMinuteCount(): int
118+
{
119+
return $this->minuteCount;
120+
}
121+
122+
/**
123+
* 开始(即出生)的公历时刻
124+
*
125+
* @return SolarTime 公历时刻
126+
*/
127+
function getStartTime(): SolarTime
128+
{
129+
return $this->startTime;
130+
}
131+
132+
/**
133+
* 结束(即开始起运)的公历时刻
134+
*
135+
* @return SolarTime 公历时刻
136+
*/
137+
function getEndTime(): SolarTime
138+
{
139+
return $this->endTime;
140+
}
141+
142+
}

0 commit comments

Comments
 (0)