Etop.Utils (Etop v0.5.0)

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.

Get the server's timezone offset in seconds.

Link to this section Functions

Link to this function

center(item, len, char \\ " ")

Specs

center(any(), integer(), String.t()) :: String.t()

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--"
Link to this function

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
Link to this function

pad(string, len, char \\ " ")

Specs

pad(any(), integer(), String.t()) :: String.t()

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"
Link to this function

pad_t(string, len, char \\ " ")

Specs

pad_t(any(), integer(), String.t()) :: String.t()

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"
Link to this function

timezone_offset()

Specs

timezone_offset() :: integer()

Get the server's timezone offset in seconds.