class Athena::Clock
- Athena::Clock
- Reference
- Object
Overview
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
Defined in:
athena-clock.crinterface.cr
Constant Summary
-
VERSION =
"0.1.1"
Constructors
Class Method Summary
-
.clock : ACLK::Interface
Represents the global clock used by all
Athena::Clock
instances. -
.clock=(clock : ACLK::Interface)
Represents the global clock used by all
Athena::Clock
instances.
Instance Method Summary
-
#in_location(location : Time::Location) : self
Returns a new clock instance set to the provided location.
-
#now : Time
Returns the current time as determined by the clock.
-
#sleep(span : Time::Span) : Nil
Sleeps for the provided span of time.
-
#sleep(seconds : Number) : Nil
Sleeps for the provided amount of seconds.
Instance methods inherited from module Athena::Clock::Interface
in_location(location : Time::Location) : self
in_location,
now : Time
now,
sleep(span : Time::Span) : Nilsleep(seconds : Number) : Nil sleep
Constructor Detail
Class Method Detail
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.
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.
Instance Method Detail
Returns a new clock instance set to the provided location.