timewrap v0.1.3 Timewrap

Timewrap is a "Time-Wrapper" through which you can access different time-sources, Elixir and Erlang offers you. Other than that you can implement on your own.

Also, Timewrap can do the time-warp, freeze, and unfreeze a Timewrap.Timer.

You can instantiate different Timewrap.Timers, registered and supervised by :name.

The Timewrap.TimeSupervisor is started with the Timewrap.Application and implicitly starts the default timer :default_timer. This one is used whenever you call Timewrap-functions without a timer given as the first argument.

Configuration

config/config.exs

config :timewrap,
  timer: :default,
  unit: :second,
  calendar: Calendar.ISO,
  representation: :unix

Examples:

use Timewrap # imports some handy Timewrap-functions.

With default Timer

Timewrap.freeze_timer()
item1 = %{ time: current_time() }
:timer.sleep(1000)
item2 = %{ time: current_time() }
assert item1.time == item2.time

Transactions with a given and frozen time

with_frozen_timer(~N[1964-08-31 06:00:00Z], fn ->
  ... do something while `current_time` will 
  always return the given timestamp within this
  block...
end )

Start several independent timers

{:ok, today} = new_timer(:today)
{:ok, next_week} = new_timer(:next_week)
freeze_time(:today, ~N[2019-02-11 09:00:00])
freeze_time(:next_week, ~N[2019-02-18 09:00:00])
... do something ...
unfreeze_time(:today)
unfreeze_time(:next_week)

Link to this section Summary

Functions

Get the current_time in the format you've configured

Freeze the current time. All calls to current_time will return the same value until you call unfreeze

Start a new timer-agent. A supervised worker of Timewrap.TimeSupervisor

Release a running timer terminates the process and removes it from supervision

Unfreeze a frozen time. If the Timer is not frozen, this function is a noop

Execute a given block with a timer frozen at the given time

Link to this section Functions

Link to this function

current_time(timer \\ :default_timer)

Get the current_time in the format you've configured.

Configuration

TODO: Describe configuration here.

Examples

iex> Timewrap.current_time() == System.system_time(:second)
true
Link to this function

freeze_time(timer \\ :default_timer, time \\ nil)

Freeze the current time. All calls to current_time will return the same value until you call unfreeze.

Example:

iex> frozen = Timewrap.freeze_time()
iex> :timer.sleep(1001)
iex> assert frozen == Timewrap.current_time()
true
Link to this function

new_timer(name)

Start a new timer-agent. A supervised worker of Timewrap.TimeSupervisor.

Example:

iex> use Timewrap
iex> {:ok, t} = new_timer(:mytime)
iex> assert is_pid(t)
iex> :timer.sleep(100)
iex> Timewrap.release_timer(t)
:ok
Link to this function

release_timer(pid)

Release a running timer terminates the process and removes it from supervision.

Example:

iex> use Timewrap
iex> {:ok, t} = new_timer(:mytime)
iex> :timer.sleep(100)
iex> release_timer(t)
iex> Process.alive?(t)
false
Link to this function

unfreeze_time(timer \\ :default_timer)

Unfreeze a frozen time. If the Timer is not frozen, this function is a noop.

Example:

iex> frozen = Timewrap.freeze_time()
iex> :timer.sleep(1001)
iex> assert frozen == Timewrap.current_time()
iex> reseted = Timewrap.unfreeze_time()
iex> assert reseted == System.system_time(:second)
iex> assert reseted != frozen
true
Link to this function

with_frozen_time(time, fun)

Execute a given block with a timer frozen at the given time

Example

 iex> use Timewrap
 iex> "1964-08-31 06:00:00" 
 iex> |> Timewrap.with_frozen_time(fn() ->
 iex>   assert current_time() == -168372000
 iex> end) 
 true