class Athena::Clock

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:

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

Constant Summary

VERSION = "0.1.1"

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Athena::Clock::Interface

in_location(location : Time::Location) : self in_location, now : Time now, sleep(span : Time::Span) : Nil
sleep(seconds : Number) : Nil
sleep

Constructor Detail

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

[View source]

Class Method Detail

def self.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]
def self.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]

Instance Method Detail

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

Returns a new clock instance set to the provided location.


[View source]
def now : Time #

Returns the current time as determined by the clock.


[View source]
def sleep(span : Time::Span) : Nil #

Sleeps for the provided span of time.


[View source]
def sleep(seconds : Number) : Nil #

Sleeps for the provided amount of seconds.


[View source]