beste/clock
Composer 安装命令:
composer require beste/clock
包简介
A collection of Clock implementations
README 文档
README
A collection of PSR-20 Clock implementations.
Table of Contents
- Installation
- Clocks
SystemClock- Time, as your computer (k)nows itLocalizedClock- A clock in a(nother) time zoneUTCClock- The clock that you should™ useFrozenClock- A clock that stopped moving (perfect for tests)MinuteClock- Who cares about seconds or even less?WrappingClock- Allows wrapping a non-clock with anow()method in a clock
- Running Tests
Installation
composer require beste/clock
Clocks
SystemClock
A System Clock will return a time just as if you would use new DateTimeImmutable(). The time zone of the returned
value is determined by the clock's environment, for example by the time zone that has been configured in your
application, by a previously used date_default_timezone_set() or by the value of date.timezone in the
php.ini. If none of these are explicitly set, it uses the UTC timezone.
# examples/system_clock.php use Beste\Clock\SystemClock; $clock = SystemClock::create(); printf("On your system, the current date and time is %s\n", $clock->now()->format('Y-m-d H:i:s T (P)')); date_default_timezone_set('America/Los_Angeles'); printf("Now it's %s\n", $clock->now()->format('Y-m-d H:i:s T (P)')); date_default_timezone_set('Europe/Berlin'); printf("Now it's %s\n", $clock->now()->format('Y-m-d H:i:s T (P)'));
LocalizedClock
A localized clock is aware of the time zone in which it is located. While the time zone of the SystemClock is
determined from the environment (your PHP configuration), this clock uses the time zone that you initialize it with.
# examples/localized_clock.php use Beste\Clock\LocalizedClock; $berlin = LocalizedClock::in('Europe/Berlin'); $denver = LocalizedClock::in(new DateTimeZone('America/Denver')); printf("Berlin: %s\n", $berlin->now()->format('Y-m-d H:i:s T (P)')); printf("Denver: %s\n", $denver->now()->format('Y-m-d H:i:s T (P)'));
UTCClock
UTC is the abbreviation for Coordinated Universal Time
and a special kind of time zone that is not affected by daylight saving time. It is commonly used for the communication
of time across different systems (e.g. between your PHP application and a database, or between a backend
and a frontend). An UTCClock instance behaves exactly the same as an instance of LocalizedClock::in('UTC').
# examples/utc_clock.php use Beste\Clock\UTCClock; $clock = UTCClock::create(); $anotherTimeZone = 'Africa/Casablanca'; date_default_timezone_set($anotherTimeZone); printf("The system time zone is %s.\n", $anotherTimeZone); printf("The clock's time zone is %s.\n", $clock->now()->getTimezone()->getName());
FrozenClock
A frozen clock doesn't move - the time we set it with will stay the same... unless we change it. That makes the frozen clock perfect for testing the behaviour of your time-based use cases, for example in Unit Tests.
# examples/frozen_clock.php use Beste\Clock\FrozenClock; use Beste\Clock\SystemClock; $frozenClock = FrozenClock::withNowFrom(SystemClock::create()); printf("\nThe clock is frozen at %s", $frozenClock->now()->format('Y-m-d H:i:s T (P)')); printf("\nLet's wait a second…"); sleep(1); printf("\nIt's one second later, but the clock is still frozen at %s", $frozenClock->now()->format('Y-m-d H:i:s T (P)')); $frozenClock->setTo($frozenClock->now()->modify('-5 minutes')); printf("\nAfter turning back the clock 5 minutes, it's %s", $frozenClock->now()->format('Y-m-d H:i:s T (P)'));
MinuteClock
In some cases, microseconds, milliseconds, or even seconds are too precise for some use cases - sometimes it's just enough if something happened in the same minute. Using the minute
# examples/minute_clock.php use Beste\Clock\FrozenClock; use Beste\Clock\MinuteClock; $frozenClock = FrozenClock::at(new DateTimeImmutable('01:23:45')); $clock = MinuteClock::wrapping($frozenClock); printf("For %s, the minute clock returns %s\n", $frozenClock->now()->format('H:i:s'), $clock->now()->format('H:i:s') ); $frozenClock->setTo($frozenClock->now()->modify('+10 seconds')); // 01:23:55 printf("For %s, the minute clock still returns %s\n", $frozenClock->now()->format('H:i:s'), $clock->now()->format('H:i:s') );
WrappingClock
If you already have an object with a now() method returning a DateTimeImmutable object, you can wrap it
in a WrappingClock to make it a "real" Clock.
as a "real" clock.
# examples/wrapping_clock.php use Beste\Clock\WrappingClock; // Create a frozen $now so that we can test the wrapping clock. $now = new DateTimeImmutable('2012-04-24 12:00:00'); // Create an object that is NOT a clock, but has a now() method returning the frozen $now. $clock = new class($now) { private \DateTimeImmutable $now; public function __construct(\DateTimeImmutable $now) { $this->now = $now; } public function now(): \DateTimeImmutable { return $this->now; } }; // We can now wrap the object in a clock. $wrappedClock = WrappingClock::wrapping($clock); assert($now->format(DATE_ATOM) === $wrappedClock->now()->format(DATE_ATOM));
Running tests
composer test
beste/clock 适用场景与选型建议
beste/clock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 26.9M 次下载、GitHub Stars 达 74, 最近一次更新时间为 2021 年 03 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「clock」 「psr20」 「psr-20」 「clock-interface」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 beste/clock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 beste/clock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 beste/clock 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Decouples applications from the system clock
A pre-release of the proposed PSR-20 Clock-Interface
Time provider, providing consistent current date, time, datetime and timezone across the request.
Yet another PSR-20 clock implementation
The smallest PSR-20 implementation (PHP 7.0 and later)
PSR-20 Implementation
统计信息
- 总下载量: 26.9M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 74
- 点击次数: 24
- 依赖项目数: 21
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-03-12