I’m trying to convert Postgres timestamp into string to be able to pass it down to clients. Guess, the most appropriate solution is to use NaiveDateTime.from_erl() function to get naive date and parse it as needed, but I’m getting errors on trying to import .from_erl(). Tried with different NaiveDateTimes, both in project and in iEx, but both are keep saying that none of NaiveDateTimes has from_erl() method.
Well if you get a naive_datetime type from postgres why not just call NaiveDatetime.to_string/1 on it?
There are functions for proper iso format and all too.
I’m not getting naive_datetime from postgres. Instead, getting something like this - {{2018, 02, 02}, {04, 40, 20}}. Checked with pgadmin, it says this data is timestamp.
In NaiveDateTime docs some examples are using this kind of data, to be more exact - .from_erl() method gets it and converts to naive datetime.
Thanks @OvermindDL1. I can reproduce your code in iEx. My problem was importing or aliasing some stuffs related to NaiveDateTime, while it’s available by default, w/o any import or alias. Tried the same thing in code (in Phoenix channels), got the same error - No function clause match .from_erl/3 method. It’s adding microseconds tuple, as second argument, and date format, as third arg. Precisely what docs said, yet I’m getting error.
Any ideas what can be the problem?? Or maybe I should just use Ecto’s type converter.
I do BB. Second and third arguments are appended by compiler, I guess, with there default values. I’m sure about that, because when I inspect my struct, log shows just what you said - 2 nested tuples inside a bigger one.
From postgres, @NobbZ, with an ecto query. Adding them with migrations timestamp() function, which automatically adds inserted_in and updated_in fields.
here’s the actual code query = from m in "messages", where: m.item_id == ^item_id and (m.author_id == ^author_id and m.addressedto_id == ^addressed_to_id) or (m.author_id == ^addressed_to_id and m.addressedto_id == ^author_id), select: [m.id, m.author_id, m.body, m.is_red, m.inserted_at]