Etop.Utils (Etop v0.5.2)
Utility helpers for Etop.
Link to this section Summary
Functions
Center the given string in the given length.
Returns the server's local naive datetime with the microsecond field truncated to the given precision (:microsecond, :millisecond or :second).
Pad (leading) the given string with spaces for the given length.
Pad (trailing) the given string with spaces for the given length.
Configurable sort.
Get the server's timezone offset in seconds.
Link to this section Functions
center(item, len, char \\ " ")
Specs
Center the given string in the given length.
Return a string of length >= the given length with the given string centered.
The returned string is padded (leading and trailing) with the given padding (default " ")
Examples
iex> Etop.Utils.center("Test", 8)
" Test "
iex> Etop.Utils.center('Test', 7, "-")
"-Test--"
local_time(datetime \\ NaiveDateTime.utc_now(), precision \\ :second)
Specs
local_time(DateTime.t() | NaiveDateTime.t(), atom()) :: NaiveDateTime.t()
Returns the server's local naive datetime with the microsecond field truncated to the given precision (:microsecond, :millisecond or :second).
Arguments
- datetime (default utc_now)
- precision (default :second)
Examples
iex> datetime = Etop.Utils.local_time()
iex> datetime.year >= 2020
true
iex> datetime = Etop.Utils.local_time(:millisecond)
iex> elem(datetime.microsecond, 1)
3
iex> datetime = NaiveDateTime.utc_now()
iex> expected = NaiveDateTime.add(datetime, Etop.Utils.timezone_offset())
iex> Etop.Utils.local_time(datetime) == %{expected | microsecond: {0, 0}}
true
iex> datetime = NaiveDateTime.utc_now()
iex> expected = NaiveDateTime.add(datetime, Etop.Utils.timezone_offset())
iex> Etop.Utils.local_time(datetime, :microsecond) == expected
true
pad(string, len, char \\ " ")
Specs
Pad (leading) the given string with spaces for the given length.
Examples
iex> Etop.Utils.pad("Test", 8)
" Test"
iex> Etop.Utils.pad("Test", 2)
"Test"
iex> Etop.Utils.pad(100, 4, "0")
"0100"
pad_t(string, len, char \\ " ")
Specs
Pad (trailing) the given string with spaces for the given length.
Examples
iex> Etop.Utils.pad_t("Test", 8)
"Test "
iex> Etop.Utils.pad_t("Test", 2)
"Test"
iex> Etop.Utils.pad_t(10.1, 5, "0")
"10.10"
sort(list, field, opts \\ [])
Configurable sort.
Arguments
list
- the enumerable to be sorted.field
(:reductions_diff) - the field to be sorted on.field_fn
(fn field -> &elem(&1, 1)[field] end) - function to get the field.sorter_fn
(&>/2) -> Sort comparator (default descending)
Examples
iex> data = [one: %{a: 3, b: 2}, two: %{a: 1, b: 3}]
iex> Etop.Utils.sort(data, :b)
[two: %{a: 1, b: 3}, one: %{a: 3, b: 2}]
iex> data = [one: %{a: 3, b: 2}, two: %{a: 1, b: 3}]
iex> Etop.Utils.sort(data, :a, sorter: &<=/2)
[two: %{a: 1, b: 3}, one: %{a: 3, b: 2}]
iex> data = [%{a: 1, b: 2}, %{a: 2, b: 3}]
iex> Etop.Utils.sort(data, :a, mapper: & &1[:a])
[%{a: 2, b: 3}, %{a: 1, b: 2}]
iex> data = [x: %{a: 1, b: 1}, y: %{a: 1, b: 2}, z: %{a: 2, b: 0}]
iex> Etop.Utils.sort(data, :a, secondary: :b)
[z: %{a: 2, b: 0}, y: %{a: 1, b: 2}, x: %{a: 1, b: 1}]
timezone_offset()
Specs
timezone_offset() :: integer()
Get the server's timezone offset in seconds.