baldinof/clock 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

baldinof/clock

最新稳定版本:1.0.1

Composer 安装命令:

composer require baldinof/clock

包简介

Simple clock abstraction

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total Downloads

Get current time in a static and testable way.

Installation

You can install the package via composer:

composer require baldinof/clock

Static usage

use Baldinof\Clock\Clock; $now = Clock::now(); assert($now instanceof \DateTimeImmutable);

Why static calls instead of dependency injection?

Static calls are very convenient when defining models:

use Baldinof\Clock\Clock; use Ramsey\Uuid\Uuid; final class User { private $id; private $createdAt; private $name; public function __construct(string $name) { $this->id = Uuid::uuid4(); $this->createdAt = Clock::now(); $this->name = $name; } // Getters... }

Testing

You can use the FrozenClockTrait in your test to freeze the clock a the begining of your tests and manipulate time.

use Baldinof\Clock\Testing\FrozenClockTrait; use Baldinof\Clock\Clock; use PHPUnit\Framework\TestCase; final class UserTest extends TestCase { use FrozenClockTrait; public function test_it_is_initialized_with_current_time() { $user = new User("John"); $this->assertEquals(Clock::now(), $user->createdAt()); } }

You can allso explicitly manipulate the clock

use Baldinof\Clock\Testing\FrozenClockTrait; use Baldinof\Clock\Clock; use PHPUnit\Framework\TestCase; final class UserTest extends TestCase { use FrozenClockTrait; public function my_function() { // Set the clock at a specified time. $this->freezeClock(new \DateTimeImmutable('2000-01-01')); // Add an hour to the clock. $this->modifyClock('+1 h'); // Reset the clock. $this->restoreSystemClock(); } }

Timezones

Out of the box Clock::now() does not pass the timezone argument to the DateTimeImmutable constructor, so the php default timezone is used.

You can change the default timezone in your php.ini.

Otherwise, call date_default_timezone_set() or call Clock::set() in your application bootstrap code:

<?php use Baldinof\Clock\Clock; use Baldinof\Clock\SystemClock; // Change the clock implementation Clock::set(SystemClock::forTimeZone('UTC')); // or change the default timezone date_default_timezone_set('UTC');

Usage with dependency injection

You can get the actual clock instance and registers it in your dependency injection container by calling Clock::get().

For example with Symfony PHP DSL:

use Baldinof\Clock\Clock; use Baldinof\Clock\ClockInterface; return function (ContainerConfigurator $container) { $services = $container->services(); $services ->set(ClockInterface::class) ->factory([Clock::class, 'get']); };

Or with Laravel:

// In a service provider $this->app->instance(ClockInterface::class, Clock::get());

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

This package is inspired by lcobucci/clock and ramsey/uuid for the static usage.

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 29.74k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固