durex v0.2.0 Durex

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

Examples

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

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

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

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

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

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

Supported units

  • ms (for millisecond)
  • s (for second)
  • m (for minute)
  • h (for hour)
  • d (for days)
  • 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/ms.exs

Link to this section Summary

Functions

Parse duration but raise if it fails

Parse duration as milliseconds

parse!(duration) deprecated

Parse duration but raise if it fails

parse(duration) deprecated

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 ms!(duration)
ms!(duration()) :: pos_integer()

Parse duration but raise if it fails

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

Parse duration as milliseconds

Link to this function parse!(duration)
parse!(duration()) :: pos_integer()
This function is deprecated. Use ms!/1 instead.

Parse duration but raise if it fails

Link to this function parse(duration)
parse(duration()) :: {:ok, pos_integer()} | :error
This function is deprecated. Use ms/1 instead.

Parse duration as milliseconds