durex v0.1.1 Durex

Parse durations, such as "1s", to its numerical millisecond value, e.g. 1_000.

Examples

iex>Durex.parse "3s"
{:ok, 3_000}

iex>Durex.parse "1h"
{:ok, 3600_000}

# Works with float too
iex>Durex.parse "0.5s"
{:ok, 500}

iex>Durex.parse "1.5h"
{:ok, 5400_000}

# Cannot parse duration less than 1ms | 1.0ms
iex>Durex.parse "0.5ms"
:error

# Fractional durations in ms will be truncated
iex>Durex.parse "1.5ms"
{:ok, 1}

Supported intervals

  • ms (for millisecond)
  • s (for second)
  • m (for minute)
  • h (for hour)
  • w (for week)

Performance Notes

  • Parsing durations which include integers is about 4x faster than their version containing floats. So instead of parsing “0.5s”, use “500ms” for maximum performance.

  • To benchmark, run: $ mix run bench/parse.exs

Link to this section Summary

Functions

Parse duration but raise if it fails

Parse duration as milliseconds

Link to this section Types

Link to this type duration()
duration() :: bitstring()

Link to this section Functions

Link to this function parse!(duration)
parse!(duration()) :: pos_integer()

Parse duration but raise if it fails

Link to this function parse(duration)
parse(duration()) :: {:ok, pos_integer()} | :error

Parse duration as milliseconds