RazgrizHsu
Timex compare
Hi, all,
i’m trying compare with timex variables,
but i don’t know why this code work like that..
dts = Timex.parse!( "2019-01-01T00:00:00.000Z", "{ISO:Extended:Z}" )
now = Timex.parse!( "2019-01-01T00:00:00.100Z", "{ISO:Extended:Z}" )
dt = Timex.shift( dts, seconds: 5 )
if ( dt <= now ) do
raise ( "why? dt already shift seconds... dt[#{ inspect dt }] now[#{ inspect now }]" )
else
:ok
end
I thought that such a comparison should be the opposite result…
anyone can help me to understand this? thanks
Marked As Solved
NobbZ
You need to use Timex.compare/2/3 instead of Kernel.<=/2.
Also Liked
NobbZ
I tend to always use the regular comparison operators against 0.
Timex.compare(start, end) <= 0
This does match semantics very well.
kip
The built in comparison operators aren’t the right tool for this job. You probably want to use Timex.compare/3.
For an explanation of why this is the case you might find this useful. Especially this quote:
Maps are compared by size, then by keys in ascending term order, then by values in key order. In the specific case of maps’ key ordering, integers are always considered to be less than floats.








