Contents
Clock

Overview

The Balau clock infrastructure consists of a base Clock interface and a single SystemClock implementation.

The Clock interface is both a convenient API for clock functions, but more importantly is a way to allow the injection of a test clock implementation into production code. This allows the manipulation of the clock from within test methods in order to simulate changes in time.

The clock API uses both std::chrono and the embedded Hinnant date library in the ThirdParty folder.

Quick start

#include <Balau/System/Clock.hpp>
#include <Balau/System/SystemClock.hpp>

Clock binding

When developing an application, a Clock binding should be added to the application's main injector configuration. Then classes that require the clock API can have the clock injected into them via their injectable constructor.

			// The application's main injector configuration.
			class Configuration : public ApplicationConfiguration {
				public: void configure() const override {
					// The production clock implementation.
					bind<Clock>().toSingleton<SystemClock>();

					// ... more binding declarations ...
				}
			};

			// An injector aware class that requires the clock API.
			class AService {
				std::shared_ptr<Clock> clock;
				// ... more dependencies ...

				BalauInjectConstruct(
					  AService
					, clock
					// ... more injectables ...
				);
			};
		

In order to test the class, a test clock may be created by deriving from either the Clock interface directly or extending the SystemClock class and reimplementing one or more of the methods. An instance of this test clock can then be injected (manually or via a test injector) into the class to be tested.

Clock API

The clock API is currently quite brief. More features will be added in future releases.

The following methods are declared.

Method name Description
now Get the current time point.
today Get the current date.
nanotime Get the current time in nanoseconds since the unix epoch.
millitime Get the current time in milliseconds since the unix epoch.