class Athena::Clock
inherits Reference
#
The Athena::Clock
component allows decoupling an application from the system clock.
This allows time to be fixed, aiding in testability of time-sensitive logic.
The component provides Athena::Clock::Interface
with the following built-in implementations:
ACLK::Native
- Interacts with the system clock; same as doingTime.local
ACLK::Monotonic
- Based on a high resolution monotonic clock, perfect for measuring time; similar toTime.monotonic
ACLK::Spec::MockClock
- Can be used in specs to able to freeze and change the current time using either#sleep
or#shift
Usage#
The core Athena::Clock
type can be used to return the current time via a global clock.
# By default, `Athena::Clock` uses the native clock implementation,
# but it can be changed to any other implementation
Athena::Clock.clock = ACLK::Monotonic.new
# Then, obtain a clock instance
clock = ACLK.clock
# Optionally, with in a specific location
berlin_clock = clock.in_location Time::Location.load "Europe/Berlin"
# From here, get the current time as a `Time` instance
now = clock.now # : ::Time
# and sleep for any period of time
clock.sleep 2
Included modules
Athena::Clock::Interface
Constants#
VERSION = "0.1.0"
#
Constructors#
Class methods#
.clock : ACLK::Interface
#
Represents the global clock used by all Athena::Clock
instances.
Note
It is preferable injecting an Athena::Clock::Interface
when possible versus using the global clock getter.
.clock=(clock : ACLK::Interface)
#
Represents the global clock used by all Athena::Clock
instances.
Note
It is preferable injecting an Athena::Clock::Interface
when possible versus using the global clock getter.
Methods#
#in_location(location : Time::Location) : self
#
Returns a new clock instance set to the provided location.