Oban.Period (Oban v2.21.0)

View Source

Periods represent durations of time as either raw seconds or a unit tuple.

All periods are normalized to seconds internally. The tuple format provides a more expressive way to specify durations in larger units:

# Raw seconds
60

# Unit tuple
{1, :minute}
{5, :minutes}
{2, :hours}

Supported time units are :second, :seconds, :minute, :minutes, :hour, :hours, :day, :days, :week, and :weeks.

Summary

Types

t()

A time duration as seconds or a unit tuple.

Supported time units for period tuples.

Functions

Checks whether the given value is a non-negative integer suitable for seconds.

Checks whether the given value is a valid period, either seconds or a unit tuple.

Convert a period to seconds.

Types

t()

(since 2.20.0)
@type t() :: pos_integer() | {pos_integer(), time_unit()}

A time duration as seconds or a unit tuple.

time_unit()

(since 2.20.0)
@type time_unit() ::
  :second
  | :seconds
  | :minute
  | :minutes
  | :hour
  | :hours
  | :day
  | :days
  | :week
  | :weeks

Supported time units for period tuples.

Both singular and plural forms are accepted, e.g. :minute and :minutes.

Functions

is_seconds(seconds)

(since 2.20.0) (macro)

Checks whether the given value is a non-negative integer suitable for seconds.

is_valid_period(period)

(since 2.20.0) (macro)

Checks whether the given value is a valid period, either seconds or a unit tuple.

to_seconds(seconds)

(since 2.20.0)
@spec to_seconds(t()) :: pos_integer()

Convert a period to seconds.

Examples

iex> Oban.Period.to_seconds(60)
60

iex> Oban.Period.to_seconds({1, :minute})
60

iex> Oban.Period.to_seconds({2, :hours})
7200