absl::Clock

An abstract interface representing a Clock, which is an object that can tell you the current time, sleep, and wait for a condition variable.

Synopsis

Declared in <absl/time/clock_interface.h>

class Clock;

Description

This interface allows decoupling code that uses time from the code that creates a point in time. You can use this to your advantage by injecting Clocks into interfaces rather than having implementations call absl::Now() directly.

Implementations of this interface must be thread-safe.

The Clock::GetRealClock() function returns a reference to the global realtime clock.

Example:

bool IsWeekend(Clock& clock) { absl::Time now = clock.TimeNow(); // ... code to check if 'now' is a weekend. }

// Production code. IsWeekend(Clock::GetRealClock());

// Test code: MyTestClock test_clock(SATURDAY); IsWeekend(test_clock);

Member Functions

NameDescription
~Clock [destructor] [virtual]Destroys the clock.
AwaitWithDeadline [virtual]Returns when cond is true or the deadline has passed. Returns true iff cond holds when returning.
Sleep [virtual]Sleeps for the specified duration.
SleepUntil [virtual]Sleeps until the specified time.
TimeNow [virtual]Returns the current time.

Static Member Functions

NameDescription
GetRealClock Returns a reference to the global realtime clock. The returned clock is thread-safe.

Derived Classes

NameDescription
SimulatedClock A simulated clock is a concrete Clock implementation that does not "tick" on its own. Time is advanced by explicit calls to the AdvanceTime() or SetTime() functions.