Find maximum and minimum in two dates

Hi there Elixir friends :vulcan_salute:

In a recent task I was on, I needed to check in two dates which of them is the maximum and which of them was the minimum. I’ve found out that using max and min functions from Kernel would work. Per example:

max(~D[2020-12-18], ~D[2020-12-10]) 
iex> ~D[2020-12-18]

min(~D[2020-12-18], ~D[2020-12-10]) 
iex> ~D[2020-12-10]

Using this solved the problem in a few seconds. When running the tests in my app, I’ve noticed that in some edge cases one or maybe both of the Dates could be nil values, but even with that, no error was being given in max and min. Started to dig out both functions, and I’ve found this.
With the max function everything was being returned as I was expecting when a nil value was encountered, it would return the other value and it would only return nil if both values were nil. This behaviour was correct.

iex> max(~D[2020-12-18], nil)
~D[2020-12-18]
iex> max(~D[2020-12-18], ~D[2020-12-10])
~D[2020-12-18]
iex> max(~D[2020-12-18], nil)           
~D[2020-12-18]
iex> max(nil, ~D[2020-12-10])           
~D[2020-12-10]
iex> max(nil, nil)           
nil

But, when testing the min function, the behaviour was not equal to what max was having.

iex> min(~D[2020-12-18], ~D[2020-12-10])
~D[2020-12-10]
iex> min(~D[2020-12-18], nil)           
nil
iex> min(nil, ~D[2020-12-10])           
nil
iex> min(nil, nil)           
nil

As you can see, when the function receives any parameter that it is nil, simply returns the nil, expecting it to be the minimum between both values, even if one of them is a Date type. This, in my opinion, is not correct behaviour. With this doubt, I talked with a co-worker and he pointed out it could be because of nil being read as Epoch 0. Is theory was probably correct, but today I tried this.

iex> min(~D[1970-01-01], nil)
nil
iex> min(~D[1969-01-01], nil)
nil

With this, I just simply removed the possibility of nil being considered as epoch 0, and now I’m wondering if it makes sense to contribute to Elixir Repo with a possible solution to this. Does anyone already think about this and what was their conclusions and does it make sense the behaviour of min? (or even max just straight return the same as what min returns).

Best regards!

iex(10)> nil < DateTime.utc_now()
true
iex(11)> nil > DateTime.utc_now()
false

Does this help?

It’s correct behaviour. Elixir and Erlang impose a total ordering over ALL terms. You can compare any two terms for a stable Enum.sort. This can sometimes lead to unexpected effects when the total ordering is not topical.

If you need topical comparisons, use the compare behaviour. Specifically for DateTime, there is DateTime.compare/2, and you can use Enum.sort/2 with the module that implements compare as the second parameter.

1 Like

That doesn’t properly compare dates:

iex(3)> max(~D[2020-12-18], ~D[2020-08-21])
~D[2020-08-21]

For comparing dates you should either use Date.compare or Enum.sort(list, Date).

3 Likes

the admonition against using max/min for dates is in the documentation:

1 Like