Skip to content

Instantly share code, notes, and snippets.

@ahamez
Created September 15, 2020 15:47
Show Gist options
  • Save ahamez/47708e114ff177c30850a502256832d6 to your computer and use it in GitHub Desktop.
Save ahamez/47708e114ff177c30850a502256832d6 to your computer and use it in GitHub Desktop.
Generate a timestamp in Elixir
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