dcarbone / gotime
PHP 8.0+的类似Golang的时间类
v0.5.1
2022-01-07 03:18 UTC
Requires
- php: ^8.0
- ext-json: *
Requires (Dev)
- phpunit/phpunit: ^9.5
README
PHP 7.3+的类似Golang的时间类
描述
这个库的目标是尽可能接近(由我决定)PHP中的GoLang Time包的API等效性,因为基本上它比PHP的好。
类
时长
Duration 类设计用来模拟 golang time.Duration 类型。
构建 Duration 类有两种方式
use \DCarbone\Go\Time; $d = new Time\Duration(5 * Time::Second); // produces a Duration with an internal value of 5e9; $d = Time::ParseDuration('5s'); // produces a Duration with an internal value of 5e9;
在内部,“时长”表示为整数,允许许多有趣的操作。
序列化
假设 $dt = new Time\Duration(5 * Time::Second);
日期间隔
DateInterval 实在很糟糕。我已经创建了自有的 DateInterval 和 IntervalSpec 类来减轻这个问题。
这些为 Duration 提供了创建间隔以用于标准 DateTime::add 和 DateTime::sub 方法的功能
$dt = new \DateTime(); echo "{$dt->format('H:i:s')}\n"; $d = new Time\Duration(5 * Time::Second); $dt->add($d->DateInterval()); echo "{$dt->format('H:i:s')}\n"; // 16:03:37 // 16:03:42
时间
Time 类设计用来勉强模拟 golang time.Time 类型。它基本上是 DateTime 带了一些东西。我将其视为“beta”状态。
构建 Time 类有两种基本方式
use DCarbone\Go\Time; // Returns an instance of Time\Time with an internal time of the unix epoch $t = Time::New(); // Returns an instance of Time\Time with an internal time of whenever you constructed it. $t = Time::Now();