First of all, as you stated, you should use NaiveDateTime.compare/2 if you want to compare dates (as explained in the docs). In Ecto however, when you query on dates, the compare function isn’t used, but rather, the date is converted to a SQL-compatible date format and sent to the database (the Elixir comparison isn’t used). Do you also experience the same issue when querying to your database?
Yes, I’m experiencing the same issue, but I think I got what you are saying, the problem is that all the examples I found about the topic compared the dates as I stated before, do you have an example?
def get_future_events() do
now =
NaiveDateTime.utc_now()
|> NaiveDateTime.truncate(:second)
from(
e in Event,
where: e.init_date >= ^now
)
|> Repo.all()
end
field :init_date, :naive_datetime
Just found this topic which could be related? If you want to have microsecond precision work, I think you have to use :naive_datetime_usec but I’m not sure.
which come down to {0, 6} > {0, 0} (first different value in the sorted keys list), hence ~N[2018-08-09 17:34:49.000000] > ~N[2018-08-09 17:34:50]. Note that you shouldn’t rely on these results since they are due to how the maps are currently implemented and the implementation can change.
But on the first struct the second is 49, and on the second 50 (second should be over microseconds on the comparison). I know I must not compare the structs directly, but is almost the only option when you are working with queries.
Maps are ordered by size, two maps with the same size are compared by keys in ascending term order and then by values in key order. In maps key order integers types are considered less than floats types.