Skip to content

Instantly share code, notes, and snippets.

@BjRo
Created April 22, 2016 14:47
Show Gist options
  • Save BjRo/2e38981d8f971c76cba78345a4e3a568 to your computer and use it in GitHub Desktop.
Save BjRo/2e38981d8f971c76cba78345a4e3a568 to your computer and use it in GitHub Desktop.
Dialyzer is our friend again :-)
defmodule Messages.Time do
use Timex
alias Timex.DateTime
@spec timestamp() :: DateTime.t
def timestamp, do: DateTime.now(:utc)
@spec timestamp_iso8601() :: String.t
@dialyzer {:nowarn_function, timestamp_iso8601: 0}
def timestamp_iso8601 do
case DateTime.now(:utc) do
ts = %DateTime{day: _d, hour: _h, millisecond: _ms, minute: _m, month: _mo, second: _s, year: _y} ->
timestamp_iso8601(ts)
_ ->
""
end
end
@spec timestamp_iso8601(Timex.Convertable.t) :: String.t | no_return
def timestamp_iso8601(ts) do
ts
|> Timex.format!("{ISO:Extended}")
|> String.replace("+00:00", "Z")
end
def parse_timestamp(ts) do
{:ok, %DateTime{}} = Timex.parse(ts, "{ISO:Extended}")
end
def to_seconds(ts) do
DateTime.to_secs(ts)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment