Last active
October 10, 2016 16:36
-
-
Save bcat-eu/80a8195fc95e31fb70d8bb223f7554d0 to your computer and use it in GitHub Desktop.
Current time helper module for Elixir
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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