Elixir: NaiveDateTime with "0" microseconds and nil microseconds comparison bug

Created on 12 Aug 2017  路  4Comments  路  Source: elixir-lang/elixir

Environment

  • Elixir & Erlang versions (elixir --version): 1.5.1
  • Operating system: macOS 10.12.5

Current Behaviour

iex> date1 = ~N[2016-07-29 11:00:00]
~N[2016-07-29 11:00:00]
iex> date2 = ~N[2016-07-29 11:00:00.000000]
~N[2016-07-29 11:00:00.000000]
iex> NaiveDateTime.diff(date1, date2)
0
iex> NaiveDateTime.compare(date1, date2)
:eq

Makes sense. But:

iex> date1 < date2
true

Expected Behaviour

I would expect date1 < date2 to be false

Most helpful comment

You cannot use structural comparison (which is <, >, and friends) with
Calendar types. Things like precision will not allow them to be

structurally equal even if they are semantically equal.

Jos茅 Valim
www.plataformatec.com.br
Skype: jv.ptec
Founder and Director of R&D

All 4 comments

@mfclarke I am very new to Elixir, but I read something about that in the "Programming Elixir" book.
It is written there

The Time type contains an hour, minute, second, and fractions of a second.The fraction is stored as a tuple containing microseconds and the number of significant digits.
The fact that time values track the number of significant digits in the seconds field means that ~T[12:34:56.0] is not equal to ~T[12:34:56.00]

Seems like it is expected behaviour.

@ivanovaleksey yes you're right, that makes sense from a struct value equality perspective because they contain different information (one has 000000 microseconds, the other nil microseconds).

But for the sake of greater than / less than comparisons, it makes sense to me that it should be comparing on overall value. Especially given the compare/2 func returns :eq.

Here's another example that demonstrates the (IMHO) expected behaviour, but with a different type:

iex(1)> 2.0000 > 2
false
iex(2)> 2.0000 < 2
false

You cannot use structural comparison (which is <, >, and friends) with
Calendar types. Things like precision will not allow them to be

structurally equal even if they are semantically equal.

Jos茅 Valim
www.plataformatec.com.br
Skype: jv.ptec
Founder and Director of R&D

Ah, so <, > etc are structural comparisons. My mistake! Closing the issue. Many thanks @josevalim and @ivanovaleksey

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Irio picture Irio  路  3Comments

LucianaMarques picture LucianaMarques  路  3Comments

ericmj picture ericmj  路  3Comments

DEvil0000 picture DEvil0000  路  3Comments

Paddy3118 picture Paddy3118  路  3Comments