DateTime comparison in query

Hey there,

I have this code:

from(
      ge in query,
      join: e in assoc(ge, :event),
      where: e.start_date <= ^date
    )

As we know this can cause problems because of the date/datetime comparisons as it is described here for example : https://hexdocs.pm/elixir/Date.html

my questions is then how could I use DateTime.compare/2 with e.start_date here as the first argument?

As far as I remember, ecto does translate this into proper comparison on the SQL level, and the DateDtime struct isn’t actually involved in the comparison.

2 Likes

It seems like we have this phantom bug that a counter depending on this sometimes is not correct

@NobbZ - Are you suggesting you can use normal comparison operators in the query and that solutions such as this: https://dockyard.com/blog/2015/11/30/comparing-date-time-in-elixir-with-ecto are pointless?

@benonymus - I’m not even sure what you’re saying

The linked article is 4 years old and probably refers to a version of elixir that is not only deprecated but literally dead.

At the time the article got released, ecto 1.0.7 was current. Today we are at 3.2.2.

The Ecto.DateTime module has been deprecated in 2.2.4.

So I won’t say that article is pointless, I’d just say its not applicable anymore today, but it might have had its worth back the day it has been published.