lcobucci/clock
最新稳定版本:3.5.0
Composer 安装命令:
composer require lcobucci/clock
包简介
Yet another clock abstraction
README 文档
README
Yet another clock abstraction...
The purpose is to decouple projects from DateTimeImmutable instantiation so that we can test things properly.
Installation
Package is available on Packagist, you can install it using Composer.
composer require lcobucci/clock
Usage
Make your objects depend on the Lcobucci\Clock\Clock interface and use SystemClock or FrozenClock to retrieve the current time or a specific time (for testing), respectively:
<?php use Lcobucci\Clock\Clock; use Lcobucci\Clock\SystemClock; use Lcobucci\Clock\FrozenClock; function filterData(Clock $clock, array $objects): array { return array_filter( $objects, static function (stdClass $object) use ($clock): bool { return $object->expiresAt > $clock->now(); } ); } // Object that will return the current time based on the given timezone // $clock = SystemClock::fromSystemTimezone(); // $clock = SystemClock::fromUTC(); $clock = new SystemClock(new DateTimeZone('America/Sao_Paulo')); // Test object that always returns a fixed time object $clock = new FrozenClock( new DateTimeImmutable('2017-05-07 18:49:30') ); // Or creating a frozen clock from the current time on UTC // $clock = FrozenClock::fromUTC(); $objects = [ (object) ['expiresAt' => new DateTimeImmutable('2017-12-31 23:59:59')], (object) ['expiresAt' => new DateTimeImmutable('2017-06-30 23:59:59')], (object) ['expiresAt' => new DateTimeImmutable('2017-01-30 23:59:59')], ]; var_dump(filterData($clock, $objects)); // last item will be filtered
统计信息
- 总下载量: 177.69M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 784
- 点击次数: 2
- 依赖项目数: 114
- 推荐数: 6
其他信息
- 授权协议: MIT
- 更新时间: 2026-01-04