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
Convert timestamp in the form { megasecs, seconds, microsecs } to the specified time units
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 }
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 }
Return time interval since the first day of year 0 to Epoch
Converts the given input value and unit to an Erlang timestamp
Evaluates fun() and measures the elapsed time
Evaluates apply(fun, args). Otherwise works like measure/1
Evaluates apply(module, fun, args). Otherwise works like measure/1
Time interval since Epoch
Converts an hour between 0..24 to {1..12, :am/:pm}
Converts an hour between 1..12 in either am or pm, to value between 0..24
Converts a timestamp to it's value in days
Converts a timestamp to it's value in hours
Converts a timestamp to it's value in minutes
Converts a timestamp to it's value in milliseconds
Converts a timestamp to it's value in seconds
Converts a timestamp to it's value in microseconds
Converts a timestamp to it's value in weeks
Return a timestamp representing a time lapse of length 0
Types
Functions
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).
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 }.
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 }.
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}
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
Specs
measure((... -> any), [any]) :: {Date.timestamp, any}
Evaluates apply(fun, args). Otherwise works like measure/1
Specs
measure(module, atom, [any]) :: {Date.timestamp, any}
Evaluates apply(module, fun, args). Otherwise works like measure/1
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 }.
Converts an hour between 0..24 to {1..12, :am/:pm}
Examples
iex> Timex.Time.to_12hour_clock(23)
{11, :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
Specs
to_days(Date.timestamp) :: quantity
Converts a timestamp to it's value in days
Specs
to_days({quantity, quantity, quantity}, :hms) :: quantity
to_days(quantity, :weeks) :: quantity
to_days(quantity, :days) :: quantity
to_days(quantity, :hours) :: quantity
to_days(quantity, :mins) :: quantity
to_days(quantity, :secs) :: quantity
to_days(quantity, :msecs) :: quantity
to_days(quantity, :usecs) :: quantity
Specs
to_hours(Date.timestamp) :: quantity
Converts a timestamp to it's value in hours
Specs
to_hours({quantity, quantity, quantity}, :hms) :: quantity
to_hours(quantity, :weeks) :: quantity
to_hours(quantity, :days) :: quantity
to_hours(quantity, :hours) :: quantity
to_hours(quantity, :mins) :: quantity
to_hours(quantity, :secs) :: quantity
to_hours(quantity, :msecs) :: quantity
to_hours(quantity, :usecs) :: quantity
Specs
to_mins(Date.timestamp) :: quantity
Converts a timestamp to it's value in minutes
Specs
to_mins({quantity, quantity, quantity}, :hms) :: quantity
to_mins(quantity, :weeks) :: quantity
to_mins(quantity, :days) :: quantity
to_mins(quantity, :hours) :: quantity
to_mins(quantity, :mins) :: quantity
to_mins(quantity, :secs) :: quantity
to_mins(quantity, :msecs) :: quantity
to_mins(quantity, :usecs) :: quantity
Specs
to_msecs(Date.timestamp) :: quantity
Converts a timestamp to it's value in milliseconds
Specs
to_msecs({quantity, quantity, quantity}, :hms) :: quantity
to_msecs(quantity, :weeks) :: quantity
to_msecs(quantity, :days) :: quantity
to_msecs(quantity, :hours) :: quantity
to_msecs(quantity, :mins) :: quantity
to_msecs(quantity, :secs) :: quantity
to_msecs(quantity, :msecs) :: quantity
to_msecs(quantity, :usecs) :: quantity
Specs
to_secs({quantity, quantity, quantity}, :hms) :: quantity
to_secs(quantity, :weeks) :: quantity
to_secs(quantity, :days) :: quantity
to_secs(quantity, :hours) :: quantity
to_secs(quantity, :mins) :: quantity
to_secs(quantity, :secs) :: quantity
to_secs(quantity, :msecs) :: quantity
to_secs(quantity, :usecs) :: quantity
Specs
to_usecs(Date.timestamp) :: quantity
Converts a timestamp to it's value in microseconds
Specs
to_usecs({quantity, quantity, quantity}, :hms) :: quantity
to_usecs(quantity, :weeks) :: quantity
to_usecs(quantity, :days) :: quantity
to_usecs(quantity, :hours) :: quantity
to_usecs(quantity, :mins) :: quantity
to_usecs(quantity, :secs) :: quantity
to_usecs(quantity, :msecs) :: quantity
to_usecs(quantity, :usecs) :: quantity
Specs
to_weeks(Date.timestamp) :: quantity
Converts a timestamp to it's value in weeks
Specs
to_weeks({quantity, quantity, quantity}, :hms) :: quantity
to_weeks(quantity, :weeks) :: quantity
to_weeks(quantity, :days) :: quantity
to_weeks(quantity, :hours) :: quantity
to_weeks(quantity, :mins) :: quantity
to_weeks(quantity, :secs) :: quantity
to_weeks(quantity, :msecs) :: quantity
to_weeks(quantity, :usecs) :: quantity