Complete API reference for the Stopwatch module and its submodules.
The main stopwatch module providing timing functionality.
@type state :: :stopped | :running | :paused
@type t :: %Stopwatch{}Creates a new stopwatch instance in the stopped state.
stopwatch = Stopwatch.new()Starts the stopwatch.
{:ok, stopwatch} = Stopwatch.start(stopwatch)Returns {:ok, stopwatch} or {:error, reason}.
Stops the stopwatch and returns the total elapsed time.
{:ok, stopwatch, elapsed_ms} = Stopwatch.stop(stopwatch)Pauses a running stopwatch.
{:ok, stopwatch} = Stopwatch.pause(stopwatch)Resumes a paused stopwatch.
{:ok, stopwatch} = Stopwatch.resume(stopwatch)Records a lap time.
{:ok, stopwatch, lap_time_ms} = Stopwatch.lap(stopwatch)Returns the current elapsed time in milliseconds.
elapsed_ms = Stopwatch.elapsed(stopwatch)Returns all recorded lap times in chronological order.
laps = Stopwatch.get_laps(stopwatch)Returns the complete history of all stopwatch events.
history = Stopwatch.get_history(stopwatch)Resets the stopwatch to initial state.
stopwatch = Stopwatch.reset(stopwatch)Provides various time formatting options.
Formats milliseconds into a human-readable time string.
# Short format (default)
Stopwatch.Formatter.format(125432)
# => "02:05.432"
# Long format
Stopwatch.Formatter.format(125432, :long)
# => "00:02:05.432"
# Compact format
Stopwatch.Formatter.format(125432, :compact)
# => "2m 5.432s"
# Verbose format
Stopwatch.Formatter.format(125432, :verbose)
# => "432 milliseconds, 5 seconds, 2 minutes"Formats milliseconds as decimal seconds.
Stopwatch.Formatter.format_seconds(5432)
# => "5.432"Breaks down milliseconds into components.
{hours, minutes, seconds, ms} = Stopwatch.Formatter.breakdown(3661234)
# => {1, 1, 1, 234}Export stopwatch data to various formats.
Exports stopwatch data to JSON format.
{:ok, json_string} = Stopwatch.Export.to_json(stopwatch)Exports stopwatch data to a JSON file.
{:ok, filename} = Stopwatch.Export.to_json_file(stopwatch, "data.json")Exports lap data to CSV format.
csv_string = Stopwatch.Export.laps_to_csv(stopwatch)Exports lap data to a CSV file.
{:ok, filename} = Stopwatch.Export.laps_to_csv_file(stopwatch, "laps.csv")Exports event history to CSV format.
csv_string = Stopwatch.Export.history_to_csv(stopwatch)Exports event history to a CSV file.
{:ok, filename} = Stopwatch.Export.history_to_csv_file(stopwatch, "history.csv")Configuration management for Stopwatch.
Loads configuration from file or returns defaults.
{:ok, config} = Stopwatch.Config.load()Saves configuration to file.
:ok = Stopwatch.Config.save(config)Returns the default configuration.
config = Stopwatch.Config.defaults()Gets a specific configuration value.
value = Stopwatch.Config.get(config, :default_format, :long)Updates a configuration value.
config = Stopwatch.Config.put(config, :default_format, :short)%{
default_format: :long,
auto_save: false,
auto_save_interval: 60_000,
color_enabled: true,
sound_enabled: false,
history_limit: 1000
}Interactive command-line interface.
start- Start the stopwatchstop- Stop the stopwatch and display total timepause- Pause the stopwatchresume- Resume a paused stopwatchlap- Record a lap timelaps- Display all recorded lapsstatus- Show current stopwatch state and statisticsreset- Reset the stopwatch to initial stateexport- Export data (JSON/CSV)save- Save current sessionload- Load saved sessionhelp- Show help messagequit/exit- Exit the application
# Create and start
stopwatch = Stopwatch.new()
{:ok, stopwatch} = Stopwatch.start(stopwatch)
# Record laps
Process.sleep(1000)
{:ok, stopwatch, lap1} = Stopwatch.lap(stopwatch)
Process.sleep(500)
{:ok, stopwatch, lap2} = Stopwatch.lap(stopwatch)
# Pause and resume
{:ok, stopwatch} = Stopwatch.pause(stopwatch)
Process.sleep(1000) # Time not counted
{:ok, stopwatch} = Stopwatch.resume(stopwatch)
# Stop
{:ok, stopwatch, total} = Stopwatch.stop(stopwatch)
# Format and display
formatted = Stopwatch.Formatter.format(total, :long)
IO.puts("Total time: #{formatted}")# Export to JSON
{:ok, json} = Stopwatch.Export.to_json(stopwatch)
# Export laps to CSV
{:ok, "laps.csv"} = Stopwatch.Export.laps_to_csv_file(stopwatch, "laps.csv")
# Export history to CSV
{:ok, "history.csv"} = Stopwatch.Export.history_to_csv_file(stopwatch, "history.csv")# Get all laps
laps = Stopwatch.get_laps(stopwatch)
# Calculate statistics
lap_times =
laps
|> Enum.chunk_every(2, 1, [0])
|> Enum.map(fn [curr, prev] -> curr - prev end)
fastest = Enum.min(lap_times)
slowest = Enum.max(lap_times)
average = div(Enum.sum(lap_times), length(lap_times))
IO.puts("Fastest: #{Stopwatch.Formatter.format(fastest)}")
IO.puts("Slowest: #{Stopwatch.Formatter.format(slowest)}")
IO.puts("Average: #{Stopwatch.Formatter.format(average)}")All state-changing operations return tagged tuples:
# Success
{:ok, stopwatch} = Stopwatch.start(stopwatch)
# Error
{:error, reason} = Stopwatch.start(already_running_stopwatch)Common error scenarios:
- Starting an already running stopwatch
- Stopping a stopped stopwatch
- Pausing a non-running stopwatch
- Resuming a non-paused stopwatch
- Recording a lap while stopped