Can't get NaiveDateTimes' from_erl/1

Hey, guys!

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.

What I’m doing wrong, guys??

Well if you get a naive_datetime type from postgres why not just call NaiveDatetime.to_string/1 on it? :slight_smile:
There are functions for proper iso format and all too. :slight_smile:

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.

So searching for this method and can’t find it )

Ahh, you are not using Ecto’s type converters, so yeah have to do it manually, and yep it exists:

iex(2)> {:ok, dt} = NaiveDateTime.from_erl({{2018, 02, 02}, {04, 40, 20}})
{:ok, ~N[2018-02-02 04:40:20]}
iex(3)> NaiveDateTime.to_string(dt)
"2018-02-02 04:40:20"

:slight_smile:

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.

from_erl is arity 1, you are calling it with 3 args in your code.

You need to pass in just a single nested tuple.

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.

How exactly do you retrieve the Erlang time? It seems to be illformed.

Sorry, I should know better than to answer before the 3rd cup of coffee…

1 Like

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]

If you are using ecto timestamps() then it should already be in naive_datetime format, you should not be getting it in erl format. o.O

Weird… OK, guys, thanks for help anyway. I’ll add a separate field in db for date and will manage it “manually” ))