Packages

A clock abstraction, with time travel.

Current section

Files

Jump to
bigben src bigben@fake_clock.erl
Raw

src/bigben@fake_clock.erl

-module(bigben@fake_clock).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/bigben/fake_clock.gleam").
-export([new_at/1, new/0, now/1, set_now/2, advance/2]).
-export_type([fake_clock/0]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
?MODULEDOC(" A fake clock for manipulating the flow of time.\n").
-opaque fake_clock() :: {fake_clock,
interior@cell:cell(gleam@time@timestamp:timestamp())}.
-file("src/bigben/fake_clock.gleam", 20).
?DOC(" Returns a new `FakeClock` at the given time.\n").
-spec new_at(gleam@time@timestamp:timestamp()) -> fake_clock().
new_at(Now) ->
{fake_clock, interior@cell:new(Now)}.
-file("src/bigben/fake_clock.gleam", 15).
?DOC(
" Returns a new `FakeClock`.\n"
"\n"
" The current time will be the instant the clock was created.\n"
).
-spec new() -> fake_clock().
new() ->
new_at(gleam@time@timestamp:system_time()).
-file("src/bigben/fake_clock.gleam", 25).
?DOC(" Returns the current time on the given `FakeClock`.\n").
-spec now(fake_clock()) -> gleam@time@timestamp:timestamp().
now(Clock) ->
interior@cell:get(erlang:element(2, Clock)).
-file("src/bigben/fake_clock.gleam", 30).
?DOC(" Sets the current time on the given `FakeClock` to the specified value.\n").
-spec set_now(fake_clock(), gleam@time@timestamp:timestamp()) -> nil.
set_now(Clock, Now) ->
interior@cell:set(erlang:element(2, Clock), Now).
-file("src/bigben/fake_clock.gleam", 35).
?DOC(" Advances the given `FakeClock` by the specified duration.\n").
-spec advance(fake_clock(), gleam@time@duration:duration()) -> nil.
advance(Clock, Duration) ->
interior@cell:update(
erlang:element(2, Clock),
fun(_capture) -> gleam@time@timestamp:add(_capture, Duration) end
).