Skip to content

OffsetClock

A clock that ticks in real time but reports a fixed offset from another clock — “the client’s clock is ten minutes fast”, “this node’s clock has * drifted 30 seconds behind the cluster”.

Unlike FrozenClock, time still passes between two reads; only the offset is under test control.

The offset is applied to every reading, monotonic included: a constant shift cancels out of any duration measured as the difference of two readings, so offsetting it too keeps OffsetClock::monotonic() internally consistent with OffsetClock::microtime() rather than needing a separate flag for “offset wall-clock only”.

final class OffsetClock implements ClockInterface

ImplementsClockInterface
SourceSupport/Clock/OffsetClock.php

public function __construct(ClockInterface $inner, float $offsetSeconds = 0.0): mixed

ParameterTypeDescription
$innerClockInterface
$offsetSecondsfloat

Returns mixed

MethodDescription
microtime(): floatWall-clock Unix timestamp with microsecond precision.
monotonic(): floatSeconds on a monotonic clock: immune to wall-clock steps, so only ever meaningful as the difference between two readings.
now(): DateTimeImmutableThe current wall-clock time.
offset(): float
setOffset(float $offsetSeconds): void
unixTimestamp(): intWall-clock Unix timestamp in whole seconds.

public function microtime(): float

Wall-clock Unix timestamp with microsecond precision.

Replaces a direct microtime(true) call.

Returns float

public function monotonic(): float

Seconds on a monotonic clock: immune to wall-clock steps, so only ever meaningful as the difference between two readings.

Replaces a direct hrtime(true) call (or a microtime(true) one used for a duration rather than a point in time).

Returns float

public function now(): DateTimeImmutable

The current wall-clock time.

Returns DateTimeImmutable

public function offset(): float

Returns float

public function setOffset(float $offsetSeconds): void

ParameterTypeDescription
$offsetSecondsfloat

public function unixTimestamp(): int

Wall-clock Unix timestamp in whole seconds.

Replaces a direct time() call.

Returns int