ExDuration v0.1.2 ExDuration View Source

Formatting durations into human readable hours, mintes, seconds and sub second strings, like 12h3m, 45μs, 67ms and 8.9s.

Link to this section Summary

Functions

Format the duration between datetime1 and datetime2

Format duration

Parses duration into units

Parses duration into units

Format the duration since start

Link to this section Functions

Format the duration between datetime1 and datetime2.

Examples

iex> dt1 = %DateTime{year: 2000, month: 1, day: 1, zone_abbr: "CET",
...>                 hour: 10, minute: 10, second: 10, microsecond: {100100, 6},
...>                 utc_offset: 3600, std_offset: 0, time_zone: "Europe/Stockholm"}
iex> dt2 = %DateTime{year: 2000, month: 1, day: 1, zone_abbr: "CET",
...>                 hour: 0, minute: 0, second: 0, microsecond: {0, 0},
...>                 utc_offset: 3600, std_offset: 0, time_zone: "Europe/Stockholm"}
iex> ExDuration.between(dt1, dt2)
"10h10m10.1001s"
Link to this function

format(duration, unit) View Source
format(
  duration :: integer(),
  :microsecond | :millisecond | :second | :minute | :hour
) :: String.t()

Format duration

Examples

iex> ExDuration.format(100, :microsecond)
"100μs"
iex> ExDuration.format(50, :millisecond)
"50ms"
iex> ExDuration.format(25, :second)
"25s"
iex> ExDuration.format(12, :minute)
"12m"
iex> ExDuration.format(6, :hour)
"6h"
iex> ExDuration.format(22345050100, :microsecond)
"6h12m25.0501s"
Link to this function

parse(duration, unit) View Source (since 0.1.1)
parse(
  String.t(),
  unit :: :microsecond | :millisecond | :second | :minute | :hour
) :: {:ok, integer()} | :error

Parses duration into units.

Returns {:ok, integer} or :error if parsing duration fails.

Format: hms(.ss)

  • h - hours as integer
  • m - minutes as integer
  • s - seconds as integer
  • ss - sub seconds (optional, maximum six digits)

Format: [ms|μs]

  • ms - milliseconds as integer
  • μs - microseconds as integer

Examples

iex> ExDuration.parse("1h2m3.456789s", :second)
{:ok, 3723}

iex> ExDuration.parse("5m", :millisecond)
{:ok, 300000}

iex> ExDuration.parse("100ms", :millisecond)
{:ok, 100}

iex> ExDuration.parse("", :hour)
:error
Link to this function

parse!(duration, unit) View Source (since 0.1.1)
parse!(
  String.t(),
  unit :: :microsecond | :millisecond | :second | :minute | :hour
) :: integer()

Parses duration into units.

Raises ArgumentError if parsing duration fails.

Examples

iex> ExDuration.parse!("1s", :millisecond)
1000

iex> ExDuration.parse!("", :millisecond)
** (ArgumentError) invalid duration format

iex> ExDuration.parse!("1s", :millis)
** (ArgumentError) argument error
Link to this function

since(start, unit \\ :microsecond) View Source
since(start :: integer(), :microsecond | :millisecond | :second) :: String.t()

Format the duration since start.

Examples

iex> start = :os.system_time(:second)
...> ExDuration.since(start, :second)
"0s"