Timex.Time

This module provides a friendly API for working with Erlang timestamps, i.e. {megasecs, secs, microsecs}. In addition, it provides an easy way to wrap the measurement of function execution time (via measure).

Summary

abs(arg1)
add(arg1, arg2)
convert(timestamp, type \\ :timestamp)

Convert timestamp in the form { megasecs, seconds, microsecs } to the specified time units

diff(t1, t2, type \\ :timestamp)

Time interval between two timestamps. If the first timestamp comes before the second one in time, the return value will be negative. Timestamp must be in format { megasecs, seconds, microseconds }

elapsed(timestamp, type \\ :timestamp)

Time interval between timestamp and now. If timestamp is after now in time, the return value will be negative. Timestamp must be in format { megasecs, seconds, microseconds }

elapsed(arg1, arg2, type)
epoch(type \\ :timestamp)

Return time interval since the first day of year 0 to Epoch

from(value, atom2)

Converts the given input value and unit to an Erlang timestamp

invert(arg1)
measure(fun)

Evaluates fun() and measures the elapsed time

measure(fun, args)

Evaluates apply(fun, args). Otherwise works like measure/1

measure(module, fun, args)

Evaluates apply(module, fun, args). Otherwise works like measure/1

now(type \\ :timestamp)

Time interval since Epoch

scale(arg1, coef)
sub(arg1, arg2)
to_12hour_clock(hour)

Converts an hour between 0..24 to {1..12, :am/:pm}

to_24hour_clock(hour, am_or_pm)

Converts an hour between 1..12 in either am or pm, to value between 0..24

to_days(timestamp)

Converts a timestamp to it's value in days

to_days(value, atom2)
to_hours(timestamp)

Converts a timestamp to it's value in hours

to_hours(value, atom2)
to_mins(timestamp)

Converts a timestamp to it's value in minutes

to_mins(value, atom2)
to_msecs(ts)

Converts a timestamp to it's value in milliseconds

to_msecs(value, atom2)
to_secs(ts)

Converts a timestamp to it's value in seconds

to_secs(value, atom2)
to_timestamp(value, atom2)
to_usecs(arg1)

Converts a timestamp to it's value in microseconds

to_usecs(value, atom2)
to_weeks(timestamp)

Converts a timestamp to it's value in weeks

to_weeks(value, atom2)
zero()

Return a timestamp representing a time lapse of length 0

Types

units :: :usecs | :msecs | :secs | :mins | :hours | :days | :weeks | :hms

quantity :: float

Functions

abs(arg1)
add(arg1, arg2)
convert(timestamp, type \\ :timestamp)

Convert timestamp in the form { megasecs, seconds, microsecs } to the specified time units.

Supported units: microseconds (:usecs), milliseconds (:msecs), seconds (:secs), minutes (:mins), hours (:hours), days (:days), or weeks (:weeks).

diff(t1, t2, type \\ :timestamp)

Time interval between two timestamps. If the first timestamp comes before the second one in time, the return value will be negative. Timestamp must be in format { megasecs, seconds, microseconds }.

The third argument is an atom indicating the type of time units to return: microseconds (:usecs), milliseconds (:msecs), seconds (:secs), minutes (:mins), or hours (:hours).

When the third argument is omitted, the return value's format is { megasecs, seconds, microsecs }.

elapsed(timestamp, type \\ :timestamp)

Time interval between timestamp and now. If timestamp is after now in time, the return value will be negative. Timestamp must be in format { megasecs, seconds, microseconds }.

The second argument is an atom indicating the type of time units to return: microseconds (:usecs), milliseconds (:msecs), seconds (:secs), minutes (:mins), or hours (:hours).

When the second argument is omitted, the return value's format is { megasecs, seconds, microsecs }.

elapsed(arg1, arg2, type)
epoch(type \\ :timestamp)

Return time interval since the first day of year 0 to Epoch.

from(value, atom2)

Specs:

  • from(integer | Date.time, units) :: Date.timestamp

Converts the given input value and unit to an Erlang timestamp.

Example

iex> Timex.Time.from(1500, :secs)
{0, 1500, 0}
invert(arg1)
measure(fun)

Specs:

  • measure((() -> any)) :: {Date.timestamp, any}

Evaluates fun() and measures the elapsed time.

Returns {timestamp, result}, timestamp is the usual { megasecs, seconds, microsecs }.

Example

iex> {_timestamp, result} = Time.measure(fn -> 2 * 2 end)
...> result == 4
true
measure(fun, args)

Specs:

  • measure((... -> any), [any]) :: {Date.timestamp, any}

Evaluates apply(fun, args). Otherwise works like measure/1

measure(module, fun, args)

Specs:

  • measure(module, atom, [any]) :: {Date.timestamp, any}

Evaluates apply(module, fun, args). Otherwise works like measure/1

now(type \\ :timestamp)

Time interval since Epoch.

The argument is an atom indicating the type of time units to return (see convert/2 for supported values).

When the argument is omitted, the return value's format is { megasecs, seconds, microsecs }.

scale(arg1, coef)
sub(arg1, arg2)
to_12hour_clock(hour)

Converts an hour between 0..24 to {1..12, :am/:pm}

Examples

iex> Timex.Time.to_12hour_clock(23)
{11, :pm}
to_24hour_clock(hour, am_or_pm)

Converts an hour between 1..12 in either am or pm, to value between 0..24

Examples

iex> Timex.Time.to_24hour_clock(7, :pm)
19
to_days(timestamp)

Specs:

Converts a timestamp to it's value in days

to_days(value, atom2)

Specs:

to_hours(timestamp)

Specs:

Converts a timestamp to it's value in hours

to_hours(value, atom2)

Specs:

to_mins(timestamp)

Specs:

Converts a timestamp to it's value in minutes

to_mins(value, atom2)

Specs:

to_msecs(ts)

Specs:

Converts a timestamp to it's value in milliseconds

to_msecs(value, atom2)

Specs:

to_secs(ts)

Specs:

Converts a timestamp to it's value in seconds

to_secs(value, atom2)

Specs:

to_timestamp(value, atom2)
to_usecs(arg1)

Specs:

Converts a timestamp to it's value in microseconds

to_usecs(value, atom2)

Specs:

to_weeks(timestamp)

Specs:

Converts a timestamp to it's value in weeks

to_weeks(value, atom2)

Specs:

zero()

Return a timestamp representing a time lapse of length 0.

Time.convert(Time.zero, :secs)
#=> 0

Can be useful for operations on collections of timestamps. For instance,

Enum.reduce timestamps, Time.zero, Time.add(&1, &2)