vulpes/clock
Composer 安装命令:
composer require vulpes/clock
包简介
PHP FIG PSR-20 Clock implementation.
关键字:
README 文档
README
This repository contains the PHP FIG PSR-20 Clock implementation.
Install
Via Composer Package is available on Packagist, you can install it using Composer.
$ composer require vulpes/clock
Details
Clock\Clock and Clock\FrozenClock are instance of Clock\ClockInterface and Psr\Clock\ClockInterface.
Clock
The clock works from a dateTime string (default: now), so it can take any value that strtotime() can handle,
and that DateTimeImmutable in its constructor can handle when calling Clock::now() function.
FrozenClock
The FrozenClock works from a DateTimeImmutable object, so its value never changes,
it will always return the same value within a timezone when calling the FrozenClock::now() function.
Usage
use Clock\ClockInterface; use Clock\ClockExceptionInterface; use Clock\Clock; use Clock\FrozenClock; // with default parameters, the `Clock::dateTime` parameter will // always be interpreted as a string $clock = new Clock( /* DateTimeInterface|string|int */ dateTime: ClockInterface::NOW, /* DateTimeZone|string */ timeZone: ClockInterface::UTC ); // with default parameters, the `FrozenClock::dateTime` parameter will // always be interpreted as a DateTimeImmutable object $clock = new FrozenClock( /* DateTimeInterface|string|int|null */ dateTime: new DateTimeImmutable( datetime: ClockInterface::NOW, timezone: new DateTimeZone(ClockInterface::UTC) ) ); // Clock and FrozenClock are identical in behavior below in this section $clock->now() // DateTimeImmutable $clock->now()->format('P'); // +01:00 $clock->now()->getTimezone()->getName(); // UTC // Clock used UTC default, so here $clock will be the same as $utcClock $utcClock = $clock->withUTC(); $utcClock->now()->getTimezone()->getName() // UTC $systemTimezoneClock = $clock->withSystemTimezone(); $systemTimezoneClock->now()->getTimezone()->getName() // (system-timezone) $withCustomTZ = $clock->withDateTimeZone('Europe/Vatican'); $withCustomTZ = $clock->withDateTimeZone(new DateTimeZone('Europe/Vatican')); $withCustomTZ->now()->getTimezone()->getName() // Europe/Vatican $with = $clock->with(new DateTime("1989-01-13")); // $with->now()->format("Y-m-d") > "1989-01-13" $with = $clock->with(new DateTimeImmutable("2011-01-13")); // $with->now()->format("Y-m-d") > "2011-01-13" $with = $clock->with('2022-02-02'); // $with->now()->format("Y-m-d") > "2022-02-02" $with = $clock->with(1643756400); // $with->now()->format("Y-m-d") > ~"2022-02-02" $with = $clock->with(1111111111); // $with->now()->getTimestamp() > 1111111111 $withCustomTZ = $clock->withDateTimeZone(new DateTimeZone('Europe/Vatican')); $withCustomTZ->now()->getTimezone()->getName() // Europe/Vatican try { $clock = new Clock(dateTime: 'unknown-or-bad-timezone'); $clock->now(); // ... } catch (ClockExceptionInterface) { // Failed to parse time string... } try { $clock = new Clock(timeZone: 'bad-timezone'); } catch (ClockExceptionInterface) { // Unknown or bad timezone (unknown datetime zone: "bad-timezone") }
Differences between Clock and FrozenClock in usage
use Clock\Clock; use Clock\FrozenClock; $systemClock = new Clock('now'); $frozenClock = new FrozenClock('now'); $systemClock->now()->format('i:s') // 10:02 $frozenClock->now()->format('i:s') // 10:02 // five minutes and 30 seconds later $systemClock->now()->format('i:s') // 15:32 $frozenClock->now()->format('i:s') // 10:02 // BUT if you set Clock::dateTime with a timestamp, it will retain // its value as a string from then on and behave like FrozenClock. $frozenSystemClock = $systemClock->with($frozenClock) $frozenSystemClockc // 10:02 // The clock works from a dateTime string (default: `now`), so it // can take any value that `strtotime()` can handle, and that // `DateTimeImmutable` in its constructor can handle when calling // `Clock::now()` function. $alwaysTomorrow = $systemClock->with('+1 day'); // on 2021-01-01 $alwaysTomorrow->now()->format('Y-m-d') // 2021-01-02 // two days later (on 2021-01-03) - sleep(60 * 60 * 24 * 2) $alwaysTomorrow->now()->format('Y-m-d') // 2021-01-04
vulpes/clock 适用场景与选型建议
vulpes/clock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 84 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 05 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「psr」 「clock」 「system-clock」 「psr-20」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 vulpes/clock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 vulpes/clock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 vulpes/clock 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Provides abstractions of a clock.
Simple PSR-16 cache implementations for WordPress transient the OOP way
Time provider, providing consistent current date, time, datetime and timezone across the request.
Provides abstractions of a clock.
The smallest PSR-20 implementation (PHP 7.0 and later)
PSR-20 Implementation
统计信息
- 总下载量: 84
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 23
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-05-12