Created
September 15, 2020 15:47
-
-
Save ahamez/47708e114ff177c30850a502256832d6 to your computer and use it in GitHub Desktop.
Generate a timestamp in Elixir
This file contains hidden or 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 Timestamp do | |
@moduledoc """ | |
Generate a timestamp as an integer using current time. | |
""" | |
@spec make() :: integer() | |
def make() do | |
%{year: y, month: m, day: d, hour: hh, minute: mm, second: ss, microsecond: {us, _}} = | |
NaiveDateTime.utc_now() | |
"#{y - 2000}#{pad(m)}#{pad(d)}#{pad(hh)}#{pad(mm)}#{pad(ss)}#{pad(us, 6)}" | |
|> String.to_integer() | |
end | |
defp pad(value, padding \\ 2) do | |
value | |
|> Integer.to_string() | |
|> String.pad_leading(padding, "0") | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment