Skip to content

Instantly share code, notes, and snippets.

@bcat-eu
Last active October 10, 2016 16:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcat-eu/80a8195fc95e31fb70d8bb223f7554d0 to your computer and use it in GitHub Desktop.
Save bcat-eu/80a8195fc95e31fb70d8bb223f7554d0 to your computer and use it in GitHub Desktop.
Current time helper module for Elixir
defmodule Util.Time do
@moduledoc """
Time helper functions.
"""
@doc """
Get current time as string using Erlangs calendar module.
"""
@spec now_to_string() :: String.t
def now_to_string() do
{{year, month, day}, {hour, minute, second}} = :calendar.local_time()
"#{day}.#{month |> zero_pad}.#{year} #{hour |> zero_pad}:#{minute |> zero_pad}:#{second |> zero_pad}"
end
@spec zero_pad(Integer, Integer) :: String.t
defp zero_pad(number, amount \\ 2) do
number
|> Integer.to_string
|> String.rjust(amount, ?0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment