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

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
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

Other popular topics Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 54996 245
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44167 214
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New

We're in Beta

About us Mission Statement