Epoch (epoch v1.2.0) View Source

Conversion between Elixir native DateTime.t/0 and epoch formats.

Link to this section Summary

Functions

Accepts a DateTime and converts it to epoch with microseconds.

Returns the current time in epoch format.

Returns the current time in epoch format with millisecond resolution.

Returns seconds since epoch for the given DateTime.t/0.

Converts an epoch with microseconds to a DateTime.t/0

Link to this section Types

Specs

datetime() :: DateTime.t()

Specs

epoch() :: integer()

Specs

epoch_in_ms() :: String.t()

Link to this section Functions

Specs

cast(datetime()) :: epoch_in_ms()

Accepts a DateTime and converts it to epoch with microseconds.

Examples

iex> cast(~U[2020-12-20 02:26:29.000800Z])
"1608431189.000800"

iex> cast(~U[2020-12-20 02:26:29Z])
"1608431189.000000"

Specs

now() :: epoch()

Returns the current time in epoch format.

Specs

now(:microsecond) :: epoch_in_ms()

Returns the current time in epoch format with millisecond resolution.

Specs

seconds_since(datetime()) :: epoch()

Returns seconds since epoch for the given DateTime.t/0.

Examples

iex> seconds_since(~U[2020-12-20 02:26:29.000800Z])
1608431189

iex> seconds_since(~U[2020-12-20 02:26:29Z])
1608431189

Specs

to_datetime(epoch_in_ms() | epoch()) :: datetime()

Converts an epoch with microseconds to a DateTime.t/0

Examples

iex> to_datetime("1608431189.000800")
~U[2020-12-20 02:26:29.000800Z]

iex> to_datetime("1608431189")
~U[2020-12-20 02:26:29Z]

iex> to_datetime(1_608_431_189)
~U[2020-12-20 02:26:29Z]