withinboredom/time
Composer 安装命令:
composer require withinboredom/time
包简介
Converting time to units
README 文档
README
Identity
This library uses some tricks to intern values so that two times are always equal to one another, no matter the distance in time or space.
use Withinboredom\Time; use Withinboredom\Time\Unit; $hour = Time::from(Unit::Hours, 1); $minutes = Time::from(Unit::Minutes, 60); echo $hour === $minutes ? 'true' : 'false' // outputs: true
Type Safety
You can ensure nobody will accidentally confuse seconds with milliseconds or minutes with seconds:
function sleep(Time $time): void { \sleep($time->as(Unit::Seconds)); } // Helper functions are included so you can type less code: sleep(Minutes(5));
Conversions and Math
You can easily convert between units and even perform operations, like sorting and arithmetic:
// use the hour constant to get one hour $hour = Hour; $hour = $hour->multiply(10)->add(Minutes(10)); // get 10:10 hours $interval = $hour->toDateInterval(); echo Hours(10) < $hour ? 'true' : 'false'; // output: true
Support for Crell\Serde
You cannot serialize/deserialize/clone Time objects.
However, if you use something like Serde, you can still serialize your value objects:
class CacheItem { public function __construct( #[Field('expiration_in_seconds')] #[TimeAs(Unit::Seconds)] public Time $expiration, ) {} } $serde = new SerdeCommon(handlers: new \Withinboredom\Time\SerdeExporter()); $serde->serialize(new CacheItem(Minutes(5)), 'json');
The above will be serialized (and deserialized) from:
{
"expiration_in_seconds": 300
}
Units
- Nanoseconds
- Microseconds
- Milliseconds
- Minutes
- Hours
- Days
- Weeks
FAQ
Why not months/years?
There are no set days in a month/year, so it’s better to use DateInterval for those types of measures.
Why does this exist?
I don’t like magic numbers.
How performant is this?
The main overhead is in autoloading and function-call overhead. Thus, if realtime performance is a concern, you might want to stick to magic numbers.
Developing
If you wish to create a PR or update the code here:
- Clone the repo
composer installto install test dependenciesyarnto install git hooks for formatting- Open in favorite IDE.
Code Standards
Per coding styles are followed.
withinboredom/time 适用场景与选型建议
withinboredom/time 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.83k 次下载、GitHub Stars 达 15, 最近一次更新时间为 2022 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 withinboredom/time 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 withinboredom/time 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 2.83k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 15
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-03-20