🇷🇺 Русский · 🇬🇧 English · 🇩🇪 Deutsch · 🇫🇷 Français · 🇪🇸 Español · 🇮🇹 Italiano
SystemClock — system clock
Purpose
Production time source: returns real OS time in a fixed timezone. The hot now() path is as cheap as possible; timestamp()/microtime() create no objects at all.
What for and how to use
Inject via ClockInterface into every service that needs “now”: logging, TTLs, schedules. Factories: SystemClock::utc() (cached), SystemClock::system(), SystemClock::fromTimezone($tz).
Example
$clock = SystemClock::utc();
$moment = $clock->now(); // DateTimeImmutable in UTC
$unix = $clock->timestamp(); // int, no date object
$fine = $clock->microtime(); // float with microseconds
Anti-patterns
Do not call statically deep inside business logic — pass the interface, otherwise tests cannot substitute time.
Lifecycle
stateDiagram-v2
[*] --> Created: utc()/system()/fromTimezone()
Created --> Reading: now()/timestamp()/microtime()
Reading --> Reading: repeat
Created --> Frozen: freeze()
Frozen --> [*]
Feature comparison with competitors
| Aspect | 🏆 CloudCastle | symfony | carbon | lcobucci | beste | ergebnis |
|---|---|---|---|---|---|---|
| Fixed explicit timezone | ✅ | ✅ | ✅ | ✅ | ❌ | ✅ |
| Cached UTC instance | ✅ | ❌ | ❌ | ❌ | ✅ | ❌ |
| Timestamp without a date object | ✅ | ❌ | ❌ | ❌ | ❌ | ❌ |
| Real sleep()/sleepUntil() | ✅ | ✅ | ❌ | ❌ | ❌ | ❌ |
| Total | 🏆 4 | 2 | 1 | 1 | 1 | 1 |
🇷🇺 Русский · 🇬🇧 English · 🇩🇪 Deutsch · 🇫🇷 Français · 🇪🇸 Español · 🇮🇹 Italiano