Skip to content

Instantly share code, notes, and snippets.

@bcat-eu
Last active Oct 10, 2016
Embed
What would you like to do?
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