timex v2.1.5 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
Functions
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
Time interval between timestamp and now. If timestamp is after now in time, the return value will be negative. Timestamp must be in format
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 its value in days
Converts a timestamp to its value in hours
Converts a timestamp to its value in microseconds
Converts a timestamp to its value in milliseconds
Converts a timestamp to its value in minutes
Converts a timestamp to its value in seconds
Converts a timestamp to its value in weeks
Return a timestamp representing a time lapse of length 0
Types
units ::
:microseconds |
:milliseconds |
:seconds |
:minutes |
:hours |
:days |
:weeks |
:hms
Functions
Convert timestamp in the form { megasecs, seconds, microsecs } to the specified time units.
Supported units
- :microseconds
- :milliseconds
- :seconds
- :minutes
- :hours
- :days
- :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, :milliseconds, :seconds, :minutes, or :hours
When the third argument is omitted, the return value’s format is:
{ megasecs, seconds, microsecs }.
Examples
iex> use Timex
...> Time.diff({1457, 136000, 785000}, Time.zero, :days)
16865
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, :milliseconds, :seconds, :minutes, or hours (:hours).
When the second argument is omitted, the return value’s format is
{ megasecs, seconds, microsecs }.
Specs
from(integer | Timex.Types.time, units) :: Timex.Types.timestamp
Converts the given input value and unit to an Erlang timestamp.
Example
iex> Timex.Time.from(1500, :seconds)
{0, 1500, 0}
Specs
measure((() -> any)) :: {Timex.Types.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]) :: {Timex.Types.timestamp, any}
Evaluates apply(fun, args). Otherwise works like measure/1
Specs
measure(module, atom, [any]) :: {Timex.Types.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(Timex.Types.timestamp) :: float
Converts a timestamp to its value in days
Specs
to_days({integer | float, integer | float, integer | float}, :hms) :: float
to_days(integer | float, :weeks) :: float
to_days(integer | float, :days) :: float
to_days(integer | float, :hours) :: float
to_days(integer | float, :minutes) :: float
to_days(integer | float, :seconds) :: float
to_days(integer | float, :milliseconds) :: float
to_days(integer | float, :microseconds) :: float
Specs
to_hours(Timex.Types.timestamp) :: float
Converts a timestamp to its value in hours
Specs
to_hours({integer | float, integer | float, integer | float}, :hms) :: float
to_hours(integer | float, :weeks) :: float
to_hours(integer | float, :days) :: float
to_hours(integer | float, :hours) :: float
to_hours(integer | float, :minutes) :: float
to_hours(integer | float, :seconds) :: float
to_hours(integer | float, :milliseconds) :: float
to_hours(integer | float, :microseconds) :: float
Specs
to_microseconds(Timex.Types.timestamp) :: integer
Converts a timestamp to its value in microseconds
Specs
to_microseconds({integer | float, integer | float, integer | float}, :hms) :: float
to_microseconds(integer | float, :weeks) :: float
to_microseconds(integer | float, :days) :: float
to_microseconds(integer | float, :hours) :: float
to_microseconds(integer | float, :minutes) :: float
to_microseconds(integer | float, :seconds) :: float
to_microseconds(integer | float, :milliseconds) :: float
to_microseconds(integer | float, :microseconds) :: float
Specs
to_milliseconds(Timex.Types.timestamp) :: float
Converts a timestamp to its value in milliseconds
Specs
to_milliseconds({integer | float, integer | float, integer | float}, :hms) :: float
to_milliseconds(integer | float, :weeks) :: float
to_milliseconds(integer | float, :days) :: float
to_milliseconds(integer | float, :hours) :: float
to_milliseconds(integer | float, :minutes) :: float
to_milliseconds(integer | float, :seconds) :: float
to_milliseconds(integer | float, :milliseconds) :: float
to_milliseconds(integer | float, :microseconds) :: float
Specs
to_minutes(Timex.Types.timestamp) :: float
Converts a timestamp to its value in minutes
Specs
to_minutes({integer | float, integer | float, integer | float}, :hms) :: float
to_minutes(integer | float, :weeks) :: float
to_minutes(integer | float, :days) :: float
to_minutes(integer | float, :hours) :: float
to_minutes(integer | float, :minutes) :: float
to_minutes(integer | float, :seconds) :: float
to_minutes(integer | float, :milliseconds) :: float
to_minutes(integer | float, :microseconds) :: float
Specs
to_seconds(Timex.Types.timestamp) :: float
Converts a timestamp to its value in seconds
Specs
to_seconds({integer | float, integer | float, integer | float}, :hms) :: float
to_seconds(integer | float, :weeks) :: float
to_seconds(integer | float, :days) :: float
to_seconds(integer | float, :hours) :: float
to_seconds(integer | float, :minutes) :: float
to_seconds(integer | float, :seconds) :: float
to_seconds(integer | float, :milliseconds) :: float
to_seconds(integer | float, :microseconds) :: float
Specs
to_weeks(Timex.Types.timestamp) :: float
Converts a timestamp to its value in weeks
Specs
to_weeks({integer | float, integer | float, integer | float}, :hms) :: float
to_weeks(integer | float, :weeks) :: float
to_weeks(integer | float, :days) :: float
to_weeks(integer | float, :hours) :: float
to_weeks(integer | float, :minutes) :: float
to_weeks(integer | float, :seconds) :: float
to_weeks(integer | float, :milliseconds) :: float
to_weeks(integer | float, :microseconds) :: float
Return a timestamp representing a time lapse of length 0.
Time.convert(Time.zero, :seconds)
#=> 0
Can be useful for operations on collections of timestamps. For instance,
Enum.reduce(timestamps, Time.zero, Time.add(&1, &2))
Can also be used to represent the timestamp of the start of the UNIX epoch, as all Erlang timestamps are relative to this point.