Download the PHP package fast-forward/clock without Composer
On this page you can find all versions of the php package fast-forward/clock. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download fast-forward/clock
More information about fast-forward/clock
Files in fast-forward/clock
Package clock
Short Description A PSR-20 compliant clock implementation for PHP, with support for time zones and a service provider for easy integration with Fast Forward Container.
License MIT
Homepage https://github.com/php-fast-forward
Informations about the package clock
FastForward\Clock
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 -
FrozenClocklets 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
Requirements:
- PHP 8.3 or higher
fast-forward/container1.6 or higherpsr/clock1.0
๐ ๏ธ Usage
Basic: Get the current time
The simplest way to get the current time is using SystemClock:
With a specific timezone
Using with Fast Forward Container
For larger applications, use the service provider to register the clock in your container:
Freezing time for tests
Use FrozenClock to create deterministic tests:
FrozenClock accepts multiple input formats:
Using FrozenClock with Fast Forward Container in tests
๐งฐ 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
| Parameter | Type | Description |
|---|---|---|
$timezone |
DateTimeZone\|string\|null |
Timezone for the clock (defaults to system default) |
FrozenClock Constructor
| 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
ClockServiceProviderfor automatic registration - PSR-20 - Both
SystemClockandFrozenClockimplementPsr\Clock\ClockInterface - Any PSR-11 Container - Both clocks can be instantiated directly without the service provider
๐ Directory Structure
โ๏ธ Advanced / Customization
Custom timezone via configuration
If you use Fast Forward Config, you can configure the default timezone:
Creating a custom clock
Both clocks are final readonly classes, but you can wrap them:
โ 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
- Repository
- Issue Tracker
- Examples
- Sphinx Documentation
- Fast Forward Container
- PSR-20 Clock
All versions of clock with dependencies
fast-forward/config Version ^1.4
fast-forward/container Version ^1.6
psr/clock Version ^1.0