RazgrizHsu

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

NobbZ

You need to use Timex.compare/2/3 instead of Kernel.<=/2.

Also Liked

NobbZ

NobbZ

I tend to always use the regular comparison operators against 0.

Timex.compare(start, end) <= 0

This does match semantics very well.

kip

kip

ex_cldr Core Team

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.

Last Post!

RazgrizHsu

RazgrizHsu

This is indeed a very good suggestion, thank you :smiley:

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54092 488
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New
senggen
Erlang/OTP 25 [erts-13.2.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] 15:22:35.803 [error] gen_event {lager_file_backend...
New

We're in Beta

About us Mission Statement