Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Datetime compare doesn't always work correctly #279

Closed
Teifion opened this issue Feb 16, 2017 · 1 comment
Closed

Datetime compare doesn't always work correctly #279

Teifion opened this issue Feb 16, 2017 · 1 comment

Comments

@Teifion
Copy link

Teifion commented Feb 16, 2017

I've a set of datetimes which for the most part are comparing correctly. One specific one did not compare correctly.

The first date (a) is in Jan of 2017 while the second date (b) is Feb 2017 yet the first datetime is determined to be greater than the second datetime. I've a set of other datetime compares and all came back correctly.

Code to reproduce:

a = Timex.to_datetime({{2017, 1, 23}, {23, 43, 41}})
b = Timex.to_datetime({{2017, 2, 15}, {13, 26, 25}})
IO.puts a > b
>> true

a = Timex.to_datetime({{2017, 1, 23}, {23, 43, 41}})
b = Timex.to_datetime({{2017, 2, 15}, {13, 26, 25}})
IO.puts a < b
>> false

Contextual/System info

$ iex
Erlang/OTP 19 [erts-8.2.2] [source-1ca84a4] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false]

Interactive Elixir (1.4.1) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> 

mix.exs

      {:timex, "~> 3.0"},
      {:timex_ecto, "~> 3.0"},

Possible test case

  test "datetime compare" do
    a = Timex.to_datetime({{2017, 1, 23}, {23, 43, 41}})
    b = Timex.to_datetime({{2017, 2, 15}, {13, 26, 25}})
    assert a < b
  end
@Teifion Teifion changed the title Greater-than doesn't always evaluate correctly Datetime compare doesn't always work correctly Feb 16, 2017
@bitwalker
Copy link
Owner

Unfortunately you can't use the comparison operators with structs, as the comparison is done using absolute term order, but the order of fields in a struct are not guaranteed. Timex provides Timex.compare/2 and Timex.compare/3 for this purpose (amongst other functions for comparing/diffing two date/time objects).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants