barretstorck/tempus-machina
Composer 安装命令:
composer require barretstorck/tempus-machina
包简介
A PSR-20 compliant PHP clock library
关键字:
README 文档
README
_____ __ __ _ _
|_ _|__ _ __ ___ _ __ _ _ ___ | \/ | __ _ ___| |__ (_)_ __ __ _
| |/ _ \ '_ ` _ \| '_ \| | | / __| | |\/| |/ _` |/ __| '_ \| | '_ \ / _` |
| | __/ | | | | | |_) | |_| \__ \ | | | | (_| | (__| | | | | | | | (_| |
|_|\___|_| |_| |_| .__/ \__,_|___/ |_| |_|\__,_|\___|_| |_|_|_| |_|\__,_|
|_|
(Latin for Time Machine)
Tempus Machina is a PSR-20 compliant
Clock library. It's purpose is to allow developers to treat time as a dependency
that can be passed into code, and therefore mocked for testing, instead of
relying on hard coded calls to time() and similar functions.
Have you ever wanted to make sure your code runs as expected during a daylight savings time transition? What about if the date is February 29th? Or what if there is a leap second? With Tempus Machina you can simulate these scenarios in your test environments.
View on Packagist.org.
Setup
To add Tempus Machina to your project run:
composer require barretstorck/tempus-machina
Available Clocks
There are 3 Clocks that implement Psr\Clock\ClockInterface and are packaged
with Tempus Machina:
1. SystemClock
The SystemClock is the default clock and always returns the device's real system
timestamp. Anything that uses the UsesClockTrait trait will automatically use
the SystemClock by default if no other clocks are given.
$clock = new \BarretStorck\TempusMachina\SystemClock(); $now = $clock->now(); // Returns a DateTimeImmutable object for the system's current time. $timestamp = $now->getTimestamp(); // Returns the system's current Unix timestamp.
2. FrozenClock
The FrozenClock will always provide whatever timestamp it is last given and will not move forward in time. If no timestamp is given then the system's current time will be used by default.
The FrozenClock constructor and set() function can accept any of the following
parameters:
- An integer unix timestamp
- A DateTime formatted string
- An existing DateTimeInterface object
- An existing ClockInterface object
- null or no parameters to use the current system timestamp as a default
// Simulate February 29th 2024. $clock = new \BarretStorck\TempusMachina\FrozenClock('February 29th 2024'); $now1 = $clock->now(); sleep(10); // Wait 10 seconds in real time. $now2 = $clock->now(); // $now2 is still equal to $now1. // Simulate February 29th 2124. $clock->set(4864860000);
3. OffsetClock
The OffsetClock will continue to move forward in real time from whatever timestamp it is last given. If no timestamp is given then the system's current time will be used by default.
The OffsetClock constructor and set() function can accept any of the following
parameters:
- An integer unix timestamp
- A DateInterval formatted string
- A DateTime formatted string
- An existing DateTimeInterface object
- An existing ClockInterface object
- An existing DateInterval object
- null or no parameters to use the current system timestamp as a default
// Simulate March 9th 2025 at 23:59:59 just before daylight savings time begins $clock = new \BarretStorck\TempusMachina\OffsetClock('2025-03-25T23:59:59+00:00'); echo $clock->now()->format(\DateTimeInterface::RFC3339); // Will echo "2025-03-25T23:59:59+00:00" sleep(10); // Wait 10 seconds to allow for real time to pass. echo $clock->now()->format(\DateTimeInterface::RFC3339); // Will echo "2025-03-26T01:09:00+00:00"
Example code
use Psr\Clock\ClockInterface; use BarretStorck\TempusMachina\{UsesClockInterface, UsesClockTrait}; class MyObject implements UsesClockInterface { use UsesClockTrait; public function __construct(null|ClockInterface $clock = null) { // If $clock is null, then the real time SystemClock will be available // by default for any future calls of `getClock()`. $this->setClock($clock); } public function doSomething() { // No longer hard coding `time()` or `new DateTimeImmutable('now')` calls. //$now = time(); //$now = new DateTimeImmutable('now'); // The clock is now available and can be mocked in testing by providing // a FrozenClock or OffsetClock to the MyObject constructor or it's // `setClock()` function. $now = $this->getClock()->now(); } }
barretstorck/tempus-machina 适用场景与选型建议
barretstorck/tempus-machina 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 01 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「time」 「timestamp」 「clock」 「psr20」 「psr-20」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 barretstorck/tempus-machina 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 barretstorck/tempus-machina 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 barretstorck/tempus-machina 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Tools to convert between .NET and PHP date formats
Time provider, providing consistent current date, time, datetime and timezone across the request.
Laravel Eloquent boolean & timestamp flagged attributes behavior.
Timestamp parameter in public file uri
Adds the EDTF data type to Wikibase
The smallest PSR-20 implementation (PHP 7.0 and later)
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 41
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-01-02