CloudCastle Clock

🇷🇺 Русский · 🇬🇧 English · 🇩🇪 Deutsch · 🇫🇷 Français · 🇪🇸 Español · 🇮🇹 Italiano

MonotonicClock — monotonic clock

Purpose

Readings never go backwards even when the system clock is set back (NTP correction, manual change): the delta comes from hrtime().

What for and how to use

Interval measurement, rate limiting, timeouts in long-lived processes (daemons, queue workers).

Example

$clock = new MonotonicClock('UTC');
$start = $clock->microtime();
// ... the system clock is moved backwards ...
$elapsed = $clock->microtime() - $start;   // still >= 0

Anti-patterns

Not for wall-clock time: readings diverge from the real clock after it is adjusted.

Lifecycle

stateDiagram-v2
    [*] --> Anchored: new MonotonicClock()
    Anchored --> Reading: now() = anchor + hrtime delta
    Reading --> Reading: monotonic growth
    note right of Reading: system clock jumps\ndo not affect readings

Feature comparison with competitors

Aspect🏆 CloudCastlesymfonycarbonlcobuccibesteergebnis
Resilience to system clock rollback✅✅❌❌❌❌
Nanosecond base (hrtime)✅✅❌❌❌❌
Full clock contract (timestamp/microtime)✅❌❌❌❌❌
Arbitrary output timezone✅✅❌❌❌❌
Real sleep()/sleepUntil() resuming after a signal✅✅❌❌❌❌
Total🏆 540000

← Back to the feature list


🇷🇺 Русский · 🇬🇧 English · 🇩🇪 Deutsch · 🇫🇷 Français · 🇪🇸 Español · 🇮🇹 Italiano