Skip to content

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 doing Time.local
  • ACLK::Monotonic - Based on a high resolution monotonic clock, perfect for measuring time; similar to Time.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.1"#

Constructors#

.new(clock : ACLK::Interface | Nil = nil, location : Time::Location | Nil = nil)#

View source

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.

View source

.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.

View source

Methods#

#in_location(location : Time::Location) : self#

Returns a new clock instance set to the provided location.

View source

#now : Time#

Returns the current time as determined by the clock.

View source

#sleep(span : Time::Span) : Nil#

Sleeps for the provided span of time.

View source

#sleep(seconds : Number) : Nil#

Sleeps for the provided amount of seconds.

View source