For some reason modern ecto (2.1.x) is returning timestamps as
{{2017, 6, 17}, {14, 0, 0, 0}}
when select()
is used.
e.g.
# schema
schema "table" do
field :date, :utc_datetime
...
# query
query |> select([t], {t.date}) |> Repo.all
#> {{2017, 6, 17}, {14, 0, 0, 0}}
are there an easy way to convert it to native (naive or not) datetime?
NaiveDateTime.from_erl
accepts almost the same format.
iex(101)> NaiveDateTime.from_erl {{2017, 6, 17}, {14, 0, 0, 0}}
** (FunctionClauseError) no function clause matching in NaiveDateTime.from_erl/2
(elixir) lib/calendar.ex:1214: NaiveDateTime.from_erl({{2017, 6, 17}, {14, 0, 0, 0}}, {0, 0})
iex(101)> NaiveDateTime.from_erl {{2017, 6, 17}, {14, 0, 0}}
{:ok, ~N[2017-06-17 14:00:00]}
also, i can cast to Ecto.DateTime
(but there are no easy way to cast from Ecto.DateTime
to DateTime
)
iex(103)> Ecto.DateTime.cast! {{2017, 6, 17}, {14, 0, 0, 0}}
# #Ecto.DateTime<2017-06-17 14:00:00>
So⊠any thoughts?
P.S. real query is using fragment("date_trunc('hour', ?)", t.date)
and not just t.date
, maybe itâs important