Skip to content

Commit

Permalink
v1.1.0 更改getYear()、getMonth()、getDay()的返回类型,非兼容性更新;新增吉神宜趋、凶神宜忌;新增每日宜忌…
Browse files Browse the repository at this point in the history
…、时辰宜忌;新增支持方法扩展;修复农历日获取时辰列表遇闰月报错的问题。
  • Loading branch information
6tail committed Jul 10, 2024
1 parent 4558bbd commit 9ec3d6d
Show file tree
Hide file tree
Showing 35 changed files with 915 additions and 310 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@
3. 新增:六曜。
4. 新增:入梅出梅。
5. 新增:获取农历日当天的时辰列表。

## [1.1.0] - 2024-07-10
1. 注意:此版本更改了getYear()、getMonth()、getDay()的返回类型,非兼容性更新。
2. 新增:吉神宜趋、凶神宜忌。
3. 新增:每日宜忌、时辰宜忌。
4. 新增:支持方法扩展。
5. 修复:农历日获取时辰列表遇闰月报错的问题。
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ Tyme是一个非常强大的日历工具库,可以看作 [Lunar](https://6tail
// 农历丙寅年四月廿一
echo $solarDay->getLunarDay();

// 扩展方法
SolarDay::extend('myMethod', function () {
return sprintf('%04d-%02d-%02d', $this->getYear(), $this->getMonth(), $this->getDay());
});
// 1986-05-29
echo $solarDay->myMethod();

## 单文件版本

> 1. 下载本源代码,执行<code>tools/build-standalone.php</code>,可在tools目录下生成<code>Tyme.php</code>单文件。
Expand Down
2 changes: 2 additions & 0 deletions src/AbstractCulture.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
abstract class AbstractCulture implements Culture
{
use ExtendTrait;

function __toString(): string
{
return $this->getName();
Expand Down
47 changes: 47 additions & 0 deletions src/ExtendTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace com\tyme;

use BadMethodCallException;
use Closure;

/**
* 扩展Trait
* @author 6tail
* @package com\tyme
*/
trait ExtendTrait
{
/**
* @var array 扩展方法列表
*/
protected static array $methods = [];

/**
* 扩展方法
* @param string $methodName 方法名
* @param Closure $function 方法体
* @return void
*/
public static function extend(string $methodName, Closure $function): void
{
static::$methods[$methodName] = $function;
}

/**
* 方法调用
* @param $method string 方法名
* @param $parameters mixed 参数
* @return mixed
* @throws BadMethodCallException
*/
public function __call(string $method, mixed $parameters)
{
if (!isset(static::$methods[$method])) {
throw new BadMethodCallException(sprintf('Method %s not exist in %s', $method, static::class));
}
$function = static::$methods[$method];
$function = $function->bindTo($this, static::class);
return $function(...$parameters);
}
}
83 changes: 83 additions & 0 deletions src/culture/God.php

Large diffs are not rendered by default.

150 changes: 150 additions & 0 deletions src/culture/Taboo.php

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/eightchar/DecadeFortune.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function getEndAge(): int
*/
function getStartLunarYear(): LunarYear
{
return $this->childLimit->getEndTime()->getLunarHour()->getDay()->getMonth()->getYear()->next($this->index * 10);
return $this->childLimit->getEndTime()->getLunarHour()->getLunarDay()->getLunarMonth()->getLunarYear()->next($this->index * 10);
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/eightchar/EightChar.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ function getSolarTimes(int $startYear, int $endYear): array
$term = $term->next($m);
}
$solarTime = $term->getJulianDay()->getSolarTime();
if ($solarTime->getDay()->getMonth()->getYear()->getYear() >= $startYear) {
if ($solarTime->getYear() >= $startYear) {
$mi = 0;
$s = 0;
// 日干支和节令干支的偏移值
$solarDay = $solarTime->getDay();
$solarDay = $solarTime->getSolarDay();
$d = $this->day->next(-$solarDay->getLunarDay()->getSixtyCycle()->getIndex())->getIndex();
if ($d > 0) {
// 从节令推移天数
Expand All @@ -192,8 +192,7 @@ function getSolarTimes(int $startYear, int $endYear): array
$mi = $solarTime->getMinute();
$s = $solarTime->getSecond();
}
$solarMonth = $solarDay->getMonth();
$time = SolarTime::fromYmdHms($solarMonth->getYear()->getYear(), $solarMonth->getMonth(), $solarDay->getDay(), $h, $mi, $s);
$time = SolarTime::fromYmdHms($solarDay->getYear(), $solarDay->getMonth(), $solarDay->getDay(), $h, $mi, $s);
// 验证一下
if ($time->getLunarHour()->getEightChar()->equals($this)) {
$l[] = $time;
Expand Down
2 changes: 1 addition & 1 deletion src/eightchar/Fortune.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ function getAge(): int
*/
function getLunarYear(): LunarYear
{
return $this->childLimit->getEndTime()->getLunarHour()->getDay()->getMonth()->getYear()->next($this->index);
return $this->childLimit->getEndTime()->getLunarHour()->getLunarDay()->getLunarMonth()->getLunarYear()->next($this->index);
}

/**
Expand Down
8 changes: 3 additions & 5 deletions src/eightchar/provider/impl/China95ChildLimitProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,15 @@ function getInfo(SolarTime $birthTime, SolarTerm $term): ChildLimitInfo
$minutes %= 360;
$day = intdiv($minutes, 12);

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

$d = $birthday->getDay() + $day;
$d = $birthTime->getDay() + $day;
$dc = $sm->getDayCount();
if ($d > $dc) {
$d -= $dc;
$sm = $sm->next(1);
}

return new ChildLimitInfo($birthTime, SolarTime::fromYmdHms($sm->getYear()->getYear(), $sm->getMonth(), $d, $birthTime->getHour(), $birthTime->getMinute(), $birthTime->getSecond()), $year, $month, $day, 0, 0);
return new ChildLimitInfo($birthTime, SolarTime::fromYmdHms($sm->getYear(), $sm->getMonth(), $d, $birthTime->getHour(), $birthTime->getMinute(), $birthTime->getSecond()), $year, $month, $day, 0, 0);
}
}
9 changes: 3 additions & 6 deletions src/eightchar/provider/impl/DefaultChildLimitProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,22 @@ function getInfo(SolarTime $birthTime, SolarTerm $term): ChildLimitInfo
// 1秒 = 2分,1秒/2=0.5秒 = 1分
$minute = $seconds * 2;

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

$d = $birthday->getDay() + $day;
$d = $birthTime->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);
$sm = SolarMonth::fromYm($birthTime->getYear() + $year, $birthTime->getMonth())->next($month);

$dc = $sm->getDayCount();
if ($d > $dc) {
$d -= $dc;
$sm = $sm->next(1);
}

return new ChildLimitInfo($birthTime, SolarTime::fromYmdHms($sm->getYear()->getYear(), $sm->getMonth(), $d, $h, $mi, $birthTime->getSecond()), $year, $month, $day, $hour, $minute);
return new ChildLimitInfo($birthTime, SolarTime::fromYmdHms($sm->getYear(), $sm->getMonth(), $d, $h, $mi, $birthTime->getSecond()), $year, $month, $day, $hour, $minute);
}
}
11 changes: 4 additions & 7 deletions src/festival/LunarFestival.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,14 @@ static function fromYmd(int $year, int $month, int $day): ?static
$data = $matches[0][0];
$solarTerm = SolarTerm::fromIndex($year, intval(substr($data, 4, 2)));
$lunarDay = $solarTerm->getJulianDay()->getSolarDay()->getLunarDay();
$lunarMonth = $lunarDay->getMonth();
if ($lunarMonth->getYear()->getYear() == $year && $lunarMonth->getMonth() == $month && $lunarDay->getDay() == $day) {
if ($lunarDay->getYear() == $year && $lunarDay->getMonth() == $month && $lunarDay->getDay() == $day) {
return new static(FestivalType::TERM, $lunarDay, $solarTerm, $data);
}
}
if (preg_match_all('/@\\d{2}2/', static::$DATA, $matches)) {
$lunarDay = LunarDay::fromYmd($year, $month, $day);
$nextDay = $lunarDay->next(1);
if ($nextDay->getMonth()->getMonth() == 1 && $nextDay->getDay() == 1) {
if ($nextDay->getMonth() == 1 && $nextDay->getDay() == 1) {
return new static(FestivalType::EVE, $lunarDay, null, $matches[0][0]);
}
}
Expand All @@ -101,18 +100,16 @@ static function fromYmd(int $year, int $month, int $day): ?static

function next(int $n): static
{
$m = $this->day->getMonth();
$year = $m->getYear()->getYear();
if ($n == 0) {
return static::fromYmd($year, $m->getMonthWithLeap(), $this->day->getDay());
return static::fromYmd($this->day->getYear(), $this->day->getMonth(), $this->day->getDay());
}
$size = count(self::$NAMES);
$t = $this->index + $n;
$offset = $this->indexOf($t, null, $size);
if ($t < 0) {
$t -= $size;
}
return static::fromIndex($year + intdiv($t, $size), $offset);
return static::fromIndex($this->day->getYear() + intdiv($t, $size), $offset);
}

function __toString(): string
Expand Down
6 changes: 2 additions & 4 deletions src/festival/SolarFestival.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,16 @@ static function fromYmd(int $year, int $month, int $day): ?static

function next(int $n): static
{
$m = $this->day->getMonth();
$year = $m->getYear()->getYear();
if ($n == 0) {
return static::fromYmd($year, $m->getMonth(), $this->day->getDay());
return static::fromYmd($this->day->getYear(), $this->day->getMonth(), $this->day->getDay());
}
$size = count(static::$NAMES);
$t = $this->index + $n;
$offset = $this->indexOf($t, null, $size);
if ($t < 0) {
$t -= $size;
}
return static::fromIndex($year + intdiv($t, $size), $offset);
return static::fromIndex($this->day->getYear() + intdiv($t, $size), $offset);
}

function __toString(): string
Expand Down
5 changes: 2 additions & 3 deletions src/holiday/LegalHoliday.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ static function fromYmd(int $year, int $month, int $day): ?static

function next(int $n): ?static
{
$m = $this->day->getMonth();
$year = $m->getYear()->getYear();
$month = $m->getMonth();
$year = $this->day->getYear();
$month = $this->day->getMonth();
if ($n == 0) {
return static::fromYmd($year, $month, $this->day->getDay());
}
Expand Down
Loading

0 comments on commit 9ec3d6d

Please sign in to comment.