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 as reported by :os.timestamp/0. Returns {time, value}, where time is { megasecs, seconds, microsecs } and value is what is returned from the function evaluation |
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
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 }.
Return time interval since the first day of year 0 to Epoch.
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 as reported by :os.timestamp/0. Returns {time, value}, where time is { megasecs, seconds, microsecs } and value is what is returned from the function evaluation.
Example
iex> Time.measure(fn -> 2 * 2 end) {{0, 0, 10}, 4}
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(Date.timestamp) :: quantity
Converts a timestamp to it’s value in seconds
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
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)