承接 fast-forward/clock 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

fast-forward/clock

Composer 安装命令:

composer require fast-forward/clock

包简介

A PSR-20 compliant clock implementation for PHP, with support for time zones and a service provider for easy integration with Fast Forward Container.

README 文档

README

PHP Version Composer Package PSR-20 Tests Coverage License

FastForward\Clock provides PSR-20 compliant clock implementations and seamless integration with Fast Forward Container.

This package offers two clock implementations: SystemClock for production time and FrozenClock for deterministic testing.

✨ Features

  • 🕒 PSR-20 Compliant - Full support for the PSR-20 Clock interface
  • 🧪 Testing Made Easy - FrozenClock lets you freeze time for reliable tests
  • 🌐 Timezone Support - Configure timezones in production and tests
  • 🔌 Container Integration - Automatic service registration via ClockServiceProvider
  • 🎯 Beginner Friendly - Simple API designed for developers new to PSR patterns

📦 Installation

composer require fast-forward/clock

Requirements:

  • PHP 8.3 or higher
  • fast-forward/container 1.6 or higher
  • psr/clock 1.0

🛠️ Usage

Basic: Get the current time

The simplest way to get the current time is using SystemClock:

<?php

declare(strict_types=1);

use FastForward\Clock\SystemClock;

$clock = new SystemClock();

echo $clock->now()->format(DATE_ATOM) . PHP_EOL;

With a specific timezone

<?php

declare(strict_types=1);

use FastForward\Clock\SystemClock;

$clock = new SystemClock('America/Sao_Paulo');

echo $clock->now()->format('Y-m-d H:i:s P') . PHP_EOL;

Using with Fast Forward Container

For larger applications, use the service provider to register the clock in your container:

<?php

declare(strict_types=1);

use FastForward\Clock\ServiceProvider\ClockServiceProvider;
use Psr\Clock\ClockInterface;
use function FastForward\Container\container;

$container = container(new ClockServiceProvider());

$clock = $container->get(ClockInterface::class);

echo $clock->now()->format(DATE_ATOM) . PHP_EOL;

Freezing time for tests

Use FrozenClock to create deterministic tests:

<?php

declare(strict_types=1);

use FastForward\Clock\FrozenClock;

$clock = new FrozenClock('2026-04-07 10:00:00');

echo $clock->now()->format(DATE_ATOM) . PHP_EOL;
// Output: 2026-04-07T10:00:00+00:00

FrozenClock accepts multiple input formats:

<?php

declare(strict_types=1);

use FastForward\Clock\FrozenClock;

// From a DateTimeImmutable
$clock1 = new FrozenClock(new DateTimeImmutable('2026-04-07 10:00:00'));

// From a string
$clock2 = new FrozenClock('next Monday');

// From a timestamp
$clock3 = new FrozenClock(1775640000);

// From another ClockInterface
$clock4 = new FrozenClock(new SystemClock('America/New_York'));

Using FrozenClock with Fast Forward Container in tests

<?php

declare(strict_types=1);

use FastForward\Clock\ServiceProvider\ClockServiceProvider;
use FastForward\Clock\FrozenClock;
use FastForward\Container\ServiceProvider\ArrayServiceProvider;
use Psr\Clock\ClockInterface;
use Psr\Container\ContainerInterface;
use function FastForward\Container\container;

$frozenClock = new FrozenClock('2026-04-07 10:00:00');

$testProvider = new ArrayServiceProvider([
    FrozenClock::class => static fn(ContainerInterface $container): FrozenClock => $frozenClock,
    ClockInterface::class => static fn(ContainerInterface $container): FrozenClock => $container->get(FrozenClock::class),
]);

$container = container($testProvider, new ClockServiceProvider());

$clock = $container->get(ClockInterface::class);

echo $clock->now()->format(DATE_ATOM) . PHP_EOL;

🧰 API Summary

Class Description
FastForward\Clock\SystemClock PSR-20 clock that returns current system time with optional timezone
FastForward\Clock\FrozenClock PSR-20 clock that returns a fixed time (ideal for testing)
FastForward\Clock\ServiceProvider\ClockServiceProvider Registers clock services in Fast Forward Container

SystemClock Constructor

public function __construct(DateTimeZone|string|null $timezone = null)
Parameter Type Description
$timezone DateTimeZone|string|null Timezone for the clock (defaults to system default)

FrozenClock Constructor

public function __construct(DateTimeInterface|ClockInterface|string|int|float $clock = 'now')
Parameter Type Description
$clock DateTimeInterface|ClockInterface|string|int|float The time to freeze. Accepts DateTimeImmutable, string (relative or absolute), timestamp, or another ClockInterface

🔌 Integration

This package integrates seamlessly with:

  • Fast Forward Container - Use ClockServiceProvider for automatic registration
  • PSR-20 - Both SystemClock and FrozenClock implement Psr\Clock\ClockInterface
  • Any PSR-11 Container - Both clocks can be instantiated directly without the service provider

📁 Directory Structure

src/
├── SystemClock.php              # Production clock implementation
├── FrozenClock.php              # Testing clock implementation
├── ServiceProvider/
│   ├── ClockServiceProvider.php # Container service provider
│   └── Factory/
│       └── DateTimeZoneFactory.php # Timezone factory
tests/
├── SystemClockTest.php
├── FrozenClockTest.php
└── ServiceProvider/
    └── ClockServiceProviderTest.php
examples/
├── 01-system-clock.php
├── 02-system-clock-with-timezone.php
├── 03-frozen-clock.php
├── 04-frozen-clock-inputs.php
├── 05-frozen-clock-container.php
└── 06-psr20-clock-interface.php
docs/
└── ... (Sphinx documentation)

⚙️ Advanced / Customization

Custom timezone via configuration

If you use Fast Forward Config, you can configure the default timezone:

<?php

use FastForward\Config\Config;
use FastForward\Clock\ServiceProvider\ClockServiceProvider;
use function FastForward\Container\container;

$config = new Config([
    DateTimeZone::class => 'America/Sao_Paulo',
]);

$container = container($config, new ClockServiceProvider());

Creating a custom clock

Both clocks are final readonly classes, but you can wrap them:

<?php

declare(strict_types=1);

use FastForward\Clock\FrozenClock;
use Psr\Clock\ClockInterface;

final class StubbedClock implements ClockInterface
{
    public function __construct(private ClockInterface $clock)
    {
    }

    public function now(): \DateTimeImmutable
    {
        return $this->clock->now();
    }

    public static function create(string $time): self
    {
        return new self(new FrozenClock($time));
    }
}

❓ FAQ

Q: What's the difference between SystemClock and FrozenClock?
A: SystemClock returns the current time and is suitable for production. FrozenClock returns a fixed time and is ideal for testing.

Q: Which interface should I use in my application?
A: Use Psr\Clock\ClockInterface for maximum portability. Both implementations satisfy this interface.

Q: How do I freeze time in tests?
A: Use FrozenClock directly, or register it via ArrayServiceProvider before ClockServiceProvider.

Q: Can I use this without Fast Forward Container?
A: Yes! Simply instantiate SystemClock or FrozenClock directly in your code.

Q: Does this work with other PSR-11 containers?
A: Yes. The clocks are standalone classes that don't require any container.

Q: How do I set a specific timezone?
A: Pass a timezone string or DateTimeZone instance to the SystemClock constructor.

🛡 License

This project is licensed under the MIT License.

🤝 Contributing

Issues, documentation improvements, and pull requests are welcome!

🔗 Links

fast-forward/clock 适用场景与选型建议

fast-forward/clock 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 260 次下载、GitHub Stars 达 0, 最近一次更新时间为 2026 年 04 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「container」 「php」 「service provider」 「clock」 「psr-20」 「fast-forward」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 fast-forward/clock 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fast-forward/clock 我们能提供哪些服务?
定制开发 / 二次开发

基于 fast-forward/clock 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 260
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 47
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

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

其他信息

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