-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
106 changed files
with
9,986 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
.idea/ | ||
vendor/ | ||
composer.lock | ||
composer.phar | ||
.phpunit.result.cache | ||
.DS_Store | ||
package.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Tyme [![License](https://img.shields.io/badge/license-MIT-4EB1BA.svg?style=flat-square)](https://github.com/6tail/tyme4php/blob/master/LICENSE) | ||
|
||
Tyme是一个非常强大的日历工具库,可以看作 [Lunar](https://6tail.cn/calendar/api.html "https://6tail.cn/calendar/api.html") 的升级版,拥有更优的设计和扩展性,支持公历和农历、星座、干支、生肖、节气、法定假日等。 | ||
|
||
|
||
> 基于php8.1开发。 | ||
## composer | ||
|
||
composer require 6tail/tyme4php | ||
<?php | ||
use com\tyme\solar\SolarDay; | ||
$solarDay = SolarDay::fromYmd(1986, 5, 29); | ||
// 1986年5月29日 | ||
echo $solarDay; | ||
// 农历丙寅年四月廿一 | ||
echo $solarDay->getLunarDay(); | ||
|
||
## 单文件版本 | ||
|
||
1. 下载本源代码,执行<code>tools/build-standalone.php</code>,可在tools目录下生成<code>Tyme.php</code>单文件。 | ||
2. 可在 [Releases](https://github.com/6tail/tyme4php/releases) 中下载对应版本的<code>Tyme.php</code>单文件。 | ||
|
||
|
||
<?php | ||
require 'Tyme.php'; | ||
use com\tyme\solar\SolarDay; | ||
$solarDay = SolarDay::fromYmd(1986, 5, 29); | ||
// 1986年5月29日 | ||
echo $solarDay; | ||
// 农历丙寅年四月廿一 | ||
echo $solarDay->getLunarDay(); | ||
|
||
## 文档 | ||
|
||
请移步至 [https://6tail.cn/tyme.html](https://6tail.cn/tyme.html "https://6tail.cn/tyme.html") | ||
|
||
## Star History | ||
|
||
[![Star History Chart](https://api.star-history.com/svg?repos=6tail/tyme4php&type=Date)](https://star-history.com/#6tail/tyme4php&Date) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
{ | ||
"name": "6tail/tyme4php", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "6tail", | ||
"email": "[email protected]", | ||
"homepage": "https://6tail.cn" | ||
} | ||
], | ||
"minimum-stability": "stable", | ||
"require": { | ||
"php": ">=8.1", | ||
"ext-bcmath": "*" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"com\\tyme\\": "src/" | ||
} | ||
}, | ||
"homepage": "https://github.com/6tail/tyme4php", | ||
"keywords": [ | ||
"公历", | ||
"农历", | ||
"星座", | ||
"干支", | ||
"生肖", | ||
"节气", | ||
"法定假日" | ||
], | ||
"description": "a calendar library" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace com\tyme; | ||
|
||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* 传统文化抽象 | ||
* @author 6tail | ||
* @package com\tyme | ||
*/ | ||
abstract class AbstractCulture implements Culture | ||
{ | ||
function __toString(): string | ||
{ | ||
return $this->getName(); | ||
} | ||
|
||
/** | ||
* @param mixed $o 对象 | ||
* @return bool true/false | ||
*/ | ||
function equals(mixed $o): bool | ||
{ | ||
return $o instanceof Culture && $this->__toString() == $o->__toString(); | ||
} | ||
|
||
/** | ||
* 转换为不超范围的索引 | ||
* | ||
* @param int|null $index 索引 | ||
* @param string|null $name 名称 | ||
* @param int|null $size 数量 | ||
* @return int 索引,从0开始 | ||
*/ | ||
protected function indexOf(int $index = null, string $name = null, int $size = null): int | ||
{ | ||
if ($index !== null && $size !== null) { | ||
$i = $index % $size; | ||
if ($i < 0) { | ||
$i += $size; | ||
} | ||
return $i; | ||
} | ||
throw new InvalidArgumentException(sprintf('invalid name: %s, size: %d', $name, $size)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace com\tyme; | ||
|
||
|
||
/** | ||
* 带天索引的传统文化抽象 | ||
* @author 6tail | ||
* @package com\tyme | ||
*/ | ||
abstract class AbstractCultureDay extends AbstractCulture | ||
{ | ||
/** | ||
* @var AbstractCulture 传统文化 | ||
*/ | ||
protected AbstractCulture $culture; | ||
|
||
/** | ||
* @var int 天索引 | ||
*/ | ||
protected int $dayIndex; | ||
|
||
protected function __construct(AbstractCulture $culture, int $dayIndex) | ||
{ | ||
$this->culture = $culture; | ||
$this->dayIndex = $dayIndex; | ||
} | ||
|
||
/** | ||
* 天索引 | ||
* | ||
* @return int 索引 | ||
*/ | ||
function getDayIndex(): int | ||
{ | ||
return $this->dayIndex; | ||
} | ||
|
||
protected function getCulture(): Culture | ||
{ | ||
return $this->culture; | ||
} | ||
|
||
function __toString(): string | ||
{ | ||
return sprintf('%s第%d天', $this->culture, $this->dayIndex + 1); | ||
} | ||
|
||
function getName(): string | ||
{ | ||
return $this->culture->getName(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace com\tyme; | ||
|
||
|
||
/** | ||
* 抽象Tyme | ||
* @author 6tail | ||
* @package com\tyme | ||
*/ | ||
abstract class AbstractTyme extends AbstractCulture implements Tyme | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
namespace com\tyme; | ||
|
||
|
||
/** | ||
* 传统文化(民俗) | ||
* @author 6tail | ||
* @package com\tyme | ||
*/ | ||
interface Culture | ||
{ | ||
/** | ||
* 名称 | ||
* @return string 名称 | ||
*/ | ||
function getName(): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace com\tyme; | ||
|
||
|
||
use InvalidArgumentException; | ||
|
||
/** | ||
* 可轮回的Tyme | ||
* @author 6tail | ||
* @package com\tyme | ||
*/ | ||
abstract class LoopTyme extends AbstractTyme | ||
{ | ||
|
||
/** | ||
* @var string[] 名称列表 | ||
*/ | ||
protected array $names; | ||
|
||
/** | ||
* @var int 索引,从0开始 | ||
*/ | ||
protected int $index; | ||
|
||
/** | ||
* 初始化 | ||
* | ||
* @param string[] $names 名称列表 | ||
* @param int|null $index 索引,支持负数,自动轮转 | ||
* @param string|null $name 名称 | ||
*/ | ||
protected function __construct(array $names, int $index = null, string $name = null) | ||
{ | ||
$this->names = $names; | ||
if ($index !== null) { | ||
$this->index = $this->indexOf($index); | ||
} else if ($name !== null) { | ||
$this->index = $this->indexOf(null, $name); | ||
} | ||
} | ||
|
||
/** | ||
* 名称 | ||
* | ||
* @return string 名称 | ||
*/ | ||
function getName(): string | ||
{ | ||
return $this->names[$this->index]; | ||
} | ||
|
||
/** | ||
* 索引 | ||
* | ||
* @return int 索引,从0开始 | ||
*/ | ||
function getIndex(): int | ||
{ | ||
return $this->index; | ||
} | ||
|
||
/** | ||
* 数量 | ||
* | ||
* @return int 数量 | ||
*/ | ||
function getSize(): int | ||
{ | ||
return count($this->names); | ||
} | ||
|
||
protected function indexOf(int $index = null, string $name = null, int $size = null): int | ||
{ | ||
if ($index !== null) { | ||
if ($size === null) { | ||
return parent::indexOf($index, null, $this->getSize()); | ||
} else { | ||
return parent::indexOf($index, null, $size); | ||
} | ||
} else if ($name !== null) { | ||
// 传了name,则忽略size | ||
for ($i = 0, $j = $this->getSize(); $i < $j; $i++) { | ||
if ($this->names[$i] == $name) { | ||
return $i; | ||
} | ||
} | ||
throw new InvalidArgumentException(sprintf('illegal name: %d', $name)); | ||
} | ||
throw new InvalidArgumentException('need index or name'); | ||
} | ||
|
||
/** | ||
* 推移后的索引 | ||
* | ||
* @param int $n 推移步数 | ||
* @return int 索引,从0开始 | ||
*/ | ||
protected function nextIndex(int $n): int | ||
{ | ||
return $this->indexOf($this->index + $n); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
<?php | ||
|
||
namespace com\tyme; | ||
|
||
|
||
/** | ||
* Tyme | ||
* @author 6tail | ||
* @package com\tyme | ||
*/ | ||
interface Tyme extends Culture | ||
{ | ||
/** | ||
* 推移 | ||
* @param int $n 推移步数 | ||
* @return Tyme Tyme | ||
*/ | ||
function next(int $n): Tyme; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace com\tyme\culture; | ||
|
||
|
||
use com\tyme\LoopTyme; | ||
|
||
/** | ||
* 动物 | ||
* @author 6tail | ||
* @package com\tyme\culture | ||
*/ | ||
class Animal 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)); | ||
} | ||
} |
Oops, something went wrong.