shijith.k

shijith.k

Naive Datetime values comparison using < and > operators

Is it not a right method to compare naive date time values using basic operators like > and < ?

I get wrong answers when I compare two date time values using these operators.

iex(1)> datetime1 = ~N[2019-09-26 01:00:00.000000]

iex(2)> datetime2 = ~N[2020-01-24 00:00:00.000000]

iex(3)> datetime1 > datetime2 
true

What is going wrong here?

Marked As Solved

NobbZ

NobbZ

</2 and >/2 work as expected and documented here.

They do a structural comparison. If you want to do a value comparison of those, please use NaiveDateTime.compare/2

Also Liked

Nicd

Nicd

You can take a peek at the internal structure by using IO.inspect/2 and it’s option structs:

iex(7)> NaiveDateTime.utc_now() |> IO.inspect(structs: false)
%{
  __struct__: NaiveDateTime,
  calendar: Calendar.ISO,
  day: 17,
  hour: 12,
  microsecond: {614456, 6},
  minute: 37,
  month: 9,
  second: 47,
  year: 2019
}

That’s how all structs work in Elixir.

garrison

garrison

Structural comparison of dates was actually used as an example of the new type system when it was introduced! At the moment I don’t believe it can catch this within a function like Enum.min_by() (obviously since it didn’t for you), but there are more type system changes coming in 1.19 and 1.20 which have to do with functions, so perhaps it will eventually?

billylanchantin

billylanchantin

Yes: CompareChain — compare_chain v0.6.0

The OP would be:

import CompareChain

datetime1 = ~N[2019-09-26 01:00:00.000000]
datetime2 = ~N[2020-01-24 00:00:00.000000]
compare?(datetime1 > datetime2, NaiveDateTime) #=> false
NobbZ

NobbZ

From the erlang documentation:

Maps are ordered by size, two maps with the same size are compared by keys in ascending term order and then by values in key order. In maps key order integers types are considered less than floats types.

LostKobrakai

LostKobrakai

This means in structural comparison the :day key has precedence over the :month key. So for any value in :month the higher :day value will sort last.

Where Next?

Popular in Questions Top

JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
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
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
mgjohns61585
Could someone help me? I'm making my first elixir program, number guessing game. I can't figure out how to convert the user's guess from ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
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 42633 214
New

We're in Beta

About us Mission Statement