Skip to content

Commit 0dfd460

Browse files
committed
v1.1.9 简化代码;优化流年计算逻辑;优化节气推移。
1 parent 5dff214 commit 0dfd460

File tree

11 files changed

+62
-125
lines changed

11 files changed

+62
-125
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@
6565
2. 修复:月九星计算错误的问题。
6666
3. 修复:九星错别字。
6767
4. 修复:宜忌错别字。
68+
69+
## [1.1.9] - 2025-03-01
70+
1. 优化:简化代码。
71+
2. 优化:流年计算逻辑。
72+
3. 优化:优化节气推移。

src/culture/KitchenGodSteed.php

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class KitchenGodSteed extends AbstractCulture
1515
{
16-
static array $NUMBERS = ["", "", "", "", "", "", "", "", "", "", "十一", "十二"];
16+
static array $NUMBERS = ['', '', '', '', '', '', '', '', '', '', '十一', '十二'];
1717

1818
/**
1919
* 正月初一的干支
@@ -48,7 +48,7 @@ protected function byEarthBranch(int $n): string
4848
*/
4949
function getMouse(): string
5050
{
51-
return sprintf("%s鼠偷粮", $this->byEarthBranch(0));
51+
return sprintf('%s鼠偷粮', $this->byEarthBranch(0));
5252
}
5353

5454
/**
@@ -58,7 +58,7 @@ function getMouse(): string
5858
*/
5959
function getGrass(): string
6060
{
61-
return sprintf("草子%s分", $this->byEarthBranch(0));
61+
return sprintf('草子%s分', $this->byEarthBranch(0));
6262
}
6363

6464
/**
@@ -68,7 +68,7 @@ function getGrass(): string
6868
*/
6969
function getCattle(): string
7070
{
71-
return sprintf("%s牛耕田", $this->byEarthBranch(1));
71+
return sprintf('%s牛耕田', $this->byEarthBranch(1));
7272
}
7373

7474
/**
@@ -78,7 +78,7 @@ function getCattle(): string
7878
*/
7979
function getFlower(): string
8080
{
81-
return sprintf("花收%s分", $this->byEarthBranch(3));
81+
return sprintf('花收%s分', $this->byEarthBranch(3));
8282
}
8383

8484
/**
@@ -88,7 +88,7 @@ function getFlower(): string
8888
*/
8989
function getDragon(): string
9090
{
91-
return sprintf("%s龙治水", $this->byEarthBranch(4));
91+
return sprintf('%s龙治水', $this->byEarthBranch(4));
9292
}
9393

9494
/**
@@ -98,7 +98,7 @@ function getDragon(): string
9898
*/
9999
function getHorse(): string
100100
{
101-
return sprintf("%s马驮谷", $this->byEarthBranch(6));
101+
return sprintf('%s马驮谷', $this->byEarthBranch(6));
102102
}
103103

104104
/**
@@ -108,7 +108,7 @@ function getHorse(): string
108108
*/
109109
function getChicken(): string
110110
{
111-
return sprintf("%s鸡抢米", $this->byEarthBranch(9));
111+
return sprintf('%s鸡抢米', $this->byEarthBranch(9));
112112
}
113113

114114
/**
@@ -118,7 +118,7 @@ function getChicken(): string
118118
*/
119119
function getSilkworm(): string
120120
{
121-
return sprintf("%s姑看蚕", $this->byEarthBranch(9));
121+
return sprintf('%s姑看蚕', $this->byEarthBranch(9));
122122
}
123123

124124
/**
@@ -128,7 +128,7 @@ function getSilkworm(): string
128128
*/
129129
function getPig(): string
130130
{
131-
return sprintf("%s屠共猪", $this->byEarthBranch(11));
131+
return sprintf('%s屠共猪', $this->byEarthBranch(11));
132132
}
133133

134134
/**
@@ -138,7 +138,7 @@ function getPig(): string
138138
*/
139139
function getField(): string
140140
{
141-
return sprintf("甲田%s分", $this->byHeavenStem(0));
141+
return sprintf('甲田%s分', $this->byHeavenStem(0));
142142
}
143143

144144
/**
@@ -148,7 +148,7 @@ function getField(): string
148148
*/
149149
function getCake(): string
150150
{
151-
return sprintf("%s人分饼", $this->byHeavenStem(2));
151+
return sprintf('%s人分饼', $this->byHeavenStem(2));
152152
}
153153

154154
/**
@@ -158,7 +158,7 @@ function getCake(): string
158158
*/
159159
function getGold(): string
160160
{
161-
return sprintf("%s日得金", $this->byHeavenStem(7));
161+
return sprintf('%s日得金', $this->byHeavenStem(7));
162162
}
163163

164164
/**
@@ -168,7 +168,7 @@ function getGold(): string
168168
*/
169169
function getPeopleCakes(): string
170170
{
171-
return sprintf("%s人%s丙", $this->byEarthBranch(2), $this->byHeavenStem(2));
171+
return sprintf('%s人%s丙', $this->byEarthBranch(2), $this->byHeavenStem(2));
172172
}
173173

174174
/**
@@ -178,11 +178,11 @@ function getPeopleCakes(): string
178178
*/
179179
function getPeopleHoes(): string
180180
{
181-
return sprintf("%s人%s锄", $this->byEarthBranch(2), $this->byHeavenStem(3));
181+
return sprintf('%s人%s锄', $this->byEarthBranch(2), $this->byHeavenStem(3));
182182
}
183183

184184
function getName(): string
185185
{
186-
return "灶马头";
186+
return '灶马头';
187187
}
188188
}

src/eightchar/ChildLimit.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,7 @@
77
use com\tyme\eightchar\provider\impl\DefaultChildLimitProvider;
88
use com\tyme\enums\Gender;
99
use com\tyme\enums\YinYang;
10-
use com\tyme\lunar\LunarHour;
1110
use com\tyme\lunar\LunarYear;
12-
use com\tyme\solar\SolarTerm;
1311
use com\tyme\solar\SolarTime;
1412

1513
/**
@@ -209,16 +207,7 @@ function getStartFortune(): Fortune
209207
*/
210208
function getEndLunarYear(): LunarYear
211209
{
212-
$endTime = $this->getEndTime();
213-
$solarYear = $endTime->getYear();
214-
$y = $endTime->getLunarHour()->getLunarDay()->getLunarMonth()->getLunarYear();
215-
if ($y->getYear() < $solarYear) {
216-
// 正月初一在立春之后的,农历年往后推一年
217-
if (LunarHour::fromYmdHms($solarYear, 1, 1, 0, 0, 0)->getSolarTime()->isAfter(SolarTerm::fromIndex($solarYear, 3)->getJulianDay()->getSolarTime())) {
218-
$y = $y->next(1);
219-
}
220-
}
221-
return $y;
210+
return LunarYear::fromYear($this->getStartTime()->getLunarHour()->getYear() + $this->getEndTime()->getYear() - $this->getStartTime()->getYear());
222211
}
223212

224213
}

src/festival/LunarFestival.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,16 +100,9 @@ static function fromYmd(int $year, int $month, int $day): ?static
100100

101101
function next(int $n): static
102102
{
103-
if ($n == 0) {
104-
return static::fromYmd($this->day->getYear(), $this->day->getMonth(), $this->day->getDay());
105-
}
106103
$size = count(self::$NAMES);
107-
$t = $this->index + $n;
108-
$offset = $this->indexOf($t, null, $size);
109-
if ($t < 0) {
110-
$t -= $size;
111-
}
112-
return static::fromIndex($this->day->getYear() + intdiv($t, $size), $offset);
104+
$i = $this->index + $n;
105+
return static::fromIndex(intdiv($this->day->getYear() * $size + $i, $size), $this->indexOf($i, null, $size));
113106
}
114107

115108
function __toString(): string

src/festival/SolarFestival.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,16 +85,9 @@ static function fromYmd(int $year, int $month, int $day): ?static
8585

8686
function next(int $n): static
8787
{
88-
if ($n == 0) {
89-
return static::fromYmd($this->day->getYear(), $this->day->getMonth(), $this->day->getDay());
90-
}
9188
$size = count(static::$NAMES);
92-
$t = $this->index + $n;
93-
$offset = $this->indexOf($t, null, $size);
94-
if ($t < 0) {
95-
$t -= $size;
96-
}
97-
return static::fromIndex($this->day->getYear() + intdiv($t, $size), $offset);
89+
$i = $this->index + $n;
90+
return static::fromIndex(intdiv($this->day->getYear() * $size + $i, $size), $this->indexOf($i, null, $size));
9891
}
9992

10093
function __toString(): string

src/solar/SolarDay.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -122,32 +122,8 @@ function getWeek(): Week
122122
*/
123123
function getConstellation(): Constellation
124124
{
125-
$index = 11;
126125
$y = $this->getMonth() * 100 + $this->day;
127-
if ($y >= 321 && $y <= 419) {
128-
$index = 0;
129-
} else if ($y >= 420 && $y <= 520) {
130-
$index = 1;
131-
} else if ($y >= 521 && $y <= 621) {
132-
$index = 2;
133-
} else if ($y >= 622 && $y <= 722) {
134-
$index = 3;
135-
} else if ($y >= 723 && $y <= 822) {
136-
$index = 4;
137-
} else if ($y >= 823 && $y <= 922) {
138-
$index = 5;
139-
} else if ($y >= 923 && $y <= 1023) {
140-
$index = 6;
141-
} else if ($y >= 1024 && $y <= 1122) {
142-
$index = 7;
143-
} else if ($y >= 1123 && $y <= 1221) {
144-
$index = 8;
145-
} else if ($y >= 1222 || $y <= 119) {
146-
$index = 9;
147-
} else if ($y <= 218) {
148-
$index = 10;
149-
}
150-
return Constellation::fromIndex($index);
126+
return Constellation::fromIndex($y > 1221 || $y < 120 ? 9 : ($y < 219 ? 10 : ($y < 321 ? 11 : ($y < 420 ? 0 : ($y < 521 ? 1 : ($y < 622 ? 2 : ($y < 723 ? 3 : ($y < 823 ? 4 : ($y < 923 ? 5 : ($y < 1024 ? 6 : ($y < 1123 ? 7 : 8)))))))))));
151127
}
152128

153129
function getName(): string

src/solar/SolarHalfYear.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,8 @@ function __toString(): string
7979

8080
function next(int $n): static
8181
{
82-
$i = $this->index;
83-
$y = $this->getYear();
84-
if ($n != 0) {
85-
$i += $n;
86-
$y += intdiv($i, 2);
87-
$i %= 2;
88-
if ($i < 0) {
89-
$i += 2;
90-
$y -= 1;
91-
}
92-
}
93-
return self::fromIndex($y, $i);
82+
$i = $this->index + $n;
83+
return self::fromIndex(intdiv($this->getYear() * 2 + $i, 2), $this->indexOf($i, null, 2));
9484
}
9585

9686
/**

src/solar/SolarMonth.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,8 @@ function __toString(): string
133133

134134
function next(int $n): SolarMonth
135135
{
136-
$m = $this->month;
137-
$y = $this->getYear();
138-
if ($n != 0) {
139-
$m += $n;
140-
$y += intdiv($m, 12);
141-
$m %= 12;
142-
if ($m < 1) {
143-
$m += 12;
144-
$y--;
145-
}
146-
}
147-
return self::fromYm($y, $m);
136+
$i = $this->month - 1 + $n;
137+
return self::fromYm(intdiv($this->getYear() * 12 + $i, 12), $this->indexOf($i, null, 12) + 1);
148138
}
149139

150140
/**

src/solar/SolarSeason.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,8 @@ function __toString(): string
7979

8080
function next(int $n): static
8181
{
82-
$i = $this->index;
83-
$y = $this->getYear();
84-
if ($n != 0) {
85-
$i += $n;
86-
$y += intdiv($i, 4);
87-
$i %= 4;
88-
if ($i < 0) {
89-
$i += 4;
90-
$y -= 1;
91-
}
92-
}
93-
return self::fromIndex($y, $i);
82+
$i = $this->index + $n;
83+
return self::fromIndex(intdiv($this->getYear() * 4 + $i, 4), $this->indexOf($i, null, 4));
9484
}
9585

9686
/**

src/solar/SolarTerm.php

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,36 +16,35 @@ class SolarTerm extends LoopTyme
1616
{
1717
static array $NAMES = ['冬至', '小寒', '大寒', '立春', '雨水', '惊蛰', '春分', '清明', '谷雨', '立夏', '小满', '芒种', '夏至', '小暑', '大暑', '立秋', '处暑', '白露', '秋分', '寒露', '霜降', '立冬', '小雪', '大雪'];
1818

19+
/**
20+
* @var int 年
21+
*/
22+
protected int $year;
23+
1924
/**
2025
* @var float 月
2126
*/
2227
protected float $cursoryJulianDay;
2328

24-
protected function __construct(?int $year = null, ?int $index = null, ?string $name = null, ?float $cursoryJulianDay = null)
29+
protected function __construct(int $year, ?int $index = null, ?string $name = null)
2530
{
26-
$idx = $index;
31+
$y = $year;
2732
if ($index !== null) {
2833
parent::__construct(self::$NAMES, $index);
34+
$size = count(self::$NAMES);
35+
$y = intdiv($year * $size + $index, $size);
2936
} else if ($name != null) {
3037
parent::__construct(self::$NAMES, null, $name);
31-
$idx = $this->index;
3238
}
33-
if ($year !== null) {
34-
$this->initByYear($year, $idx);
35-
} else if ($cursoryJulianDay !== null) {
36-
$this->cursoryJulianDay = $cursoryJulianDay;
37-
}
38-
}
3939

40-
protected function initByYear(int $year, int $offset): void
41-
{
42-
$jd = floor(($year - 2000) * 365.2422 + 180);
40+
$jd = floor(($y - 2000) * 365.2422 + 180);
4341
// 355是2000.12冬至,得到较靠近jd的冬至估计值
4442
$w = floor(($jd - 355 + 183) / 365.2422) * 365.2422 + 355;
4543
if (ShouXingUtil::calcQi($w) > $jd) {
4644
$w -= 365.2422;
4745
}
48-
$this->cursoryJulianDay = ShouXingUtil::calcQi($w + 15.2184 * $offset);
46+
$this->year = $y;
47+
$this->cursoryJulianDay = ShouXingUtil::calcQi($w + 15.2184 * $this->index);
4948
}
5049

5150
static function fromIndex(int $year, int $index): static
@@ -60,7 +59,9 @@ static function fromName(int $year, string $name): static
6059

6160
function next(int $n): SolarTerm
6261
{
63-
return new static(null, $this->nextIndex($n), null, $this->cursoryJulianDay + 15.2184 * $n);
62+
$size = $this->getSize();
63+
$i = $this->index + $n;
64+
return static::fromIndex(intdiv($this->year * $size + $i, $size), $this->indexOf($i));
6465
}
6566

6667
/**
@@ -93,6 +94,16 @@ function getJulianDay(): JulianDay
9394
return JulianDay::fromJulianDay(ShouXingUtil::qiAccurate2($this->cursoryJulianDay) + JulianDay::J2000);
9495
}
9596

97+
/**
98+
* 年
99+
*
100+
* @return int 年
101+
*/
102+
function getYear(): int
103+
{
104+
return $this->year;
105+
}
106+
96107
/**
97108
* 粗略的儒略日
98109
*

0 commit comments

Comments
 (0)