snowhite v2.1.3 Snowhite.Helpers.Timing View Source
Helper module to write milliseconds in a readable way. The goal of this module is help you quickly remember what milliseconds time you set instead of having to decode it yourself. Let's be honest, it's easier to understand 1h 30m and 10seconds than 5410000m than 5410000ms.
It basically parses a syntax similar to 1h30m10s to a clock format ({1, 30, 10}). Then it can easily convert such clock to milliseconds. To quickly get ms out of a time string, you can use the sigil ~d()
(for duration) to help you.
Link to this section Summary
Functions
Converts a given unit of time to milliseconds
Converts a clock tuple to milliseconds.
Same as parse!/1
but returns an ok tuple or an error tuple
Parses a clock string to a clock tuple. Raises if the format doesn't match. It supports any combination in any order of xh
, xm
and xs
.
Most of the time you will rely on the sigil as it makes it easier to declare both compile time and runtime milliseconds.
Returns a zero clock {0, 0, 0}
Link to this section Types
Link to this section Functions
Specs
as_ms(integer(), time_unit()) :: milliseconds()
Converts a given unit of time to milliseconds
Examples
iex> alias Snowhite.Helpers.Timing
iex> Timing.as_ms(10, :hours)
36000000
iex> Timing.as_ms(10, :minutes)
600000
iex> Timing.as_ms(10, :seconds)
10000
Specs
clock_to_ms(clock()) :: milliseconds()
Converts a clock tuple to milliseconds.
Examples
iex> alias Snowhite.Helpers.Timing
iex> Timing.clock_to_ms({10, 3, 20})
36200000
iex> Timing.clock_to_ms({0, 0, 1})
1000
Specs
Same as parse!/1
but returns an ok tuple or an error tuple
Examples
iex> alias Snowhite.Helpers.Timing
iex> Timing.parse!("1h20m")
{:ok, {1, 20, 0}{}
iex> Timing.parse!("1x")
{:error, :invalid_format}
Specs
Parses a clock string to a clock tuple. Raises if the format doesn't match. It supports any combination in any order of xh
, xm
and xs
.
Examples
iex> alias Snowhite.Helpers.Timing
iex> Timing.parse!("1h20m")
{1, 20, 0}
iex> Timing.parse!("1x")
ArgumentError "Expected clock format, received `1x`"
Specs
sigil_d(String.t(), list()) :: milliseconds()
Most of the time you will rely on the sigil as it makes it easier to declare both compile time and runtime milliseconds.
Exmaples
iex> import Snowhite.Helpers.Timing
iex> ~d(1h30m10s)
5410000
iex> ~d(1h)
3600000
iex> ~d(1h30m)
5400000
iex> ~d(15m10s)
910000
iex> ~d(1s)
1000
Specs
zero() :: clock()
Returns a zero clock {0, 0, 0}