Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Clock Laravel Package

symfony/clock

Symfony Clock decouples your app from the system clock. Inject ClockInterface to get deterministic time via now() and control behavior like sleep() and time zones. Ideal for testable, time-sensitive code without relying on global time functions.

View on GitHub
Deep Wiki
Context7

The Symfony Clock Component decouples your code from the system time by relying on a ClockInterface instead of direct calls like new \DateTimeImmutable() or sleep(). This makes time-dependent logic easier to test, more predictable, and configurable (including optional timezone handling).

  • Inject a ClockInterface to standardize time access across services
  • Get current time via now() as a \DateTimeImmutable
  • Pause execution with sleep() using fractional seconds (e.g., 2.5)
  • Swap implementations (e.g., NativeClock) for testing or custom behavior
  • Optionally enforce a timezone with withTimeZone()
Frequently asked questions about Clock
How do I integrate Symfony Clock with Laravel’s service container?
Bind `ClockInterface` in Laravel’s container using `NativeClock` for production or `MockClock` for tests. Example: `$this->app->bind(ClockInterface::class, fn() => new NativeClock());`. Use Laravel’s `needs()` method for auto-wiring: `$this->app->when(YourService::class)->needs(ClockInterface::class)->give(NativeClock::class);`.
Will Symfony Clock work with Laravel’s Carbon library?
Yes, but they serve different purposes. Use `ClockInterface` for time abstraction (e.g., testing, timezone control) and Carbon for date manipulation. For gradual adoption, wrap Carbon in a custom `ClockInterface` implementation like `CarbonClock` to reuse existing logic.
Can I use Symfony Clock in Laravel 9 or older versions?
Symfony Clock requires PHP 8.1+, so Laravel 10+ is officially supported. For older versions, you’d need a polyfill or custom implementation, but the component isn’t optimized for pre-8.1 environments. Test thoroughly if attempting backporting.
How do I mock time for unit tests with Symfony Clock?
Inject `MockClock` in your test container and set a fixed time using `$mockClock->setTime(new DateTimeImmutable('2023-01-01'))`. This ensures deterministic test results. Example: `$this->app->bind(ClockInterface::class, fn() => new MockClock());` in your test setup.
What’s the performance impact of using Symfony Clock in production?
Negligible. `NativeClock` uses PHP’s native `time()` function under the hood, adding zero runtime overhead. The abstraction only affects code that explicitly depends on `ClockInterface`, so existing `Carbon::now()` calls remain unchanged.
How do I handle timezone differences between services in Laravel?
Use `withTimeZone()` on `ClockInterface` to enforce a service-specific timezone (e.g., `$clock->withTimeZone('UTC')`). For consistency, configure a default timezone in Laravel’s `config/app.php` and ensure all `ClockInterface` implementations respect it.
Is Symfony Clock suitable for Laravel queue workers or event listeners?
Absolutely. Decouple time-sensitive logic (e.g., retries, delays) from the system clock to avoid inconsistencies like Lambda cold starts. Inject `ClockInterface` into workers/listeners and use `sleep()` for reliable delays.
How do I migrate legacy code using `time()` or `date()` to Symfony Clock?
Wrap legacy calls in adapters or use Laravel’s Facade pattern to hide `ClockInterface` behind a `Time::now()` facade. Example: `class TimeFacade { public static function now() { return app(ClockInterface::class)->now(); }}`. Gradually replace direct calls.
What are the alternatives to Symfony Clock for Laravel?
For time abstraction, consider `brianium/paratest` (for parallel testing) or custom wrappers around Carbon. However, Symfony Clock is the most robust solution for production-grade time decoupling, especially when paired with Symfony’s ecosystem (e.g., Messenger).
How do I enforce ClockInterface usage in new Laravel code?
Use static analysis tools like PHPStan or Psalm to detect direct `time()`/`date()` calls. Enforce `ClockInterface` in PHPDoc requirements for new services. For stricter adoption, create a custom Laravel rule in `app/Providers/AppServiceProvider` to validate constructor dependencies.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport