You have to convert the Ecto.DateTime struct to the erlang tuple (via Ecto.DateTime.to_erl/1) and then use :calendar.datetime_to_gregorian_seconds to get the number of gregorian seconds (since day 0) and subtract the unix time in gregorian seconds (62167219200). In one (in)glorious pipeline:
I had this Ecto.DateTime to UNIX timestamp working fine in my app but since I’ve moved to Elixir 1.5 and Phoenix 1.3, this part is no longer working. I must be missing something… See this example, here “w” is loaded with Ecto 2.1.6 and has the typical timestamp.
iex(12)> w.inserted_at |> Ecto.DateTime.to_erl()
** (FunctionClauseError) no function clause matching in Ecto.DateTime.to_erl/1
(ecto) lib/ecto/date_time.ex:608: Ecto.DateTime.to_erl(~N[2017-10-18 22:39:26.000000])