PaperTiger.Clock (PaperTiger v0.7.0)
View SourceManages time for PaperTiger. Three modes:
:real- Uses System.system_time(:second):accelerated- Real time × multiplier (1 real sec = N PaperTiger secs):manual- Frozen time, advance viaPaperTiger.advance_time/1
Examples
# Real time (production/PR apps)
config :paper_tiger, time_mode: :real
# Accelerated time (integration tests)
config :paper_tiger,
time_mode: :accelerated,
time_multiplier: 100 # 1 real second = 100 Stripe seconds
# Manual time (unit tests)
config :paper_tiger, time_mode: :manual
# Advance time in tests
PaperTiger.advance_time(days: 30)
PaperTiger.advance_time(seconds: 3600)
Summary
Functions
Advances time by the given amount (manual mode only).
Returns a specification to start this module under a supervisor.
Returns the current clock mode.
Returns the current PaperTiger time as Unix timestamp (seconds).
Resets the clock to current system time.
Changes the time mode dynamically.
Starts the Clock GenServer.
Types
@type mode() :: :real | :accelerated | :manual
@type state() :: %{ mode: mode(), multiplier: pos_integer(), offset: integer(), started_at: integer() }
Functions
Advances time by the given amount (manual mode only).
Examples
PaperTiger.Clock.advance(seconds: 3600)
PaperTiger.Clock.advance(days: 30)
PaperTiger.Clock.advance(86400) # 1 day in seconds
Returns a specification to start this module under a supervisor.
See Supervisor.
@spec get_mode() :: mode()
Returns the current clock mode.
Examples
PaperTiger.Clock.get_mode()
#=> :real
@spec now() :: integer()
Returns the current PaperTiger time as Unix timestamp (seconds).
Behaves differently based on time mode:
:real- Returns actual system time:accelerated- Returns system time × multiplier:manual- Returns frozen time + manual offset
@spec reset() :: :ok
Resets the clock to current system time.
Useful for cleaning up between tests.
Changes the time mode dynamically.
This is useful for tests that need to switch between modes.
Options
:multiplier- Time multiplier for accelerated mode (default: 1):timestamp- Starting timestamp for manual mode (default: current time)
@spec start_link(keyword()) :: GenServer.on_start()
Starts the Clock GenServer.