Duration v0.1.1 Duration View Source

Convenient module to play with ISO 8601:2004 durations and Timex.shift/2.

Link to this section Summary

Functions

Loads a Duration.t.

Parse a duration string to a Duration.t.

Converts a Duration.t into Timex.shift_options, wich can be used with Timex.shift/2.

Link to this section Types

Link to this type

t()

View Source
t() :: %Duration{
  days: pos_integer(),
  hours: non_neg_integer(),
  minutes: non_neg_integer(),
  months: pos_integer(),
  seconds: non_neg_integer(),
  years: non_neg_integer()
}

Link to this section Functions

Link to this function

new(val)

View Source
new(Duration.t()) :: {:ok, Duration.t()} | {:error, atom()}
new(String.t()) :: {:ok, Duration.t()} | {:error, atom()}
new(list()) :: {:ok, Duration.t()} | {:error, atom()}

Loads a Duration.t.

Link to this function

parse(value)

View Source
parse(String.t()) :: {:ok, Duration.t()} | {:error, atom()}

Parse a duration string to a Duration.t.

Examples

iex> Duration.parse("PT3S")
{:ok, %Duration{seconds: 3}}
Link to this function

to_timex_options(duration, direction \\ :forward)

View Source

Converts a Duration.t into Timex.shift_options, wich can be used with Timex.shift/2.

Examples

Go forward

iex > Duration.to_timex_options(%Duration{years: 1})
{:ok, [days: 0, hours: 0, minutes: 0, months: 0, seconds: 0, years: 1]}

Go backward

iex > Duration.to_timex_options(%Duration{years: 1}; :backward)
{:ok, [days: 0, hours: 0, minutes: 0, months: 0, seconds: 0, years: -1]}