Awlexus

Awlexus

Improve DateTime comparisons

Today at work I was looking at some older code and saw some DateTime comparisons and thought to myself that there should be really better ways to compare DateTime, Date and Time with each other.
Generally the things I saw the most where the following cases:

  • is a before b?
  • is a after b?
  • is a between b and c?

My initial idea was that the module could use some functions like DateTime.before?(a, b), DateTime.after?(a, b) and DateTime.between?(a, b, c), but quickly realized that it would be pretty easy to confuse the where to put which datetime, so this is probably not much of an option.
Using the pipe operator would help a bit in this regard, but that I think we can do better.
a |> DateTime.before?(b)

I was reading a bit about infix operators but that doesn’t look too promising either for a couple of reasons

  • The you’d need to import the infix operators before being able to use them.
  • If the list of free available infix operators is still the same, there are no operators I could recommend with this action.

The third way I thought we could use is a mixture from both previously mentioned approaches: DateTime.compare(a, comparator, b)

As for the comparator, I thought something similar to Enum.sort/2 might be a good idea

# simple
DateTime.compare(a, :<, b)
DateTime.compare(a, :<=, b)

# Natural wording?
DateTime.is?(a, :before, b)
DateTime.is?(a, :before_or_same_as, b)

I tried to searching for discussions to this topic on this forum and a little bit on the GitHub repository, but did not find anything recent, so I thought I’d post my thoughts here and maybe get some feedback about this idea. Do you have other approaches that could be considered?

Most Liked

LostKobrakai

LostKobrakai

I don‘t know the core teams actual reasoning, but plain comparison I feel is only really useful for sorting – and that‘s available in 1.11 using Enum.sort(list, DateTime). My other big use case, where it might be useful is anything related to scheduling, where I found the idea of intervals in combination with Allen‘s Interval Algebra much more useful. Given @wojtekmach wrote calendar_interval this could‘ve been a factor (though unlikely the only one).

gregvaughn

gregvaughn

Those all seem like one-liners around DateTime — Elixir v1.10.4

def my_compare(a, :<, b), do: DateTime.compare(a, b) == :lt
def my_compare(a, :<=, b), do: DateTime.compare(a, b) != :gt

Let me know if I’m missing some subtlety though.

al2o3cr

al2o3cr

Your initial idea is decidedly similar to Timex.before?/1 and friends, FWIW

Last Post!

LostKobrakai

LostKobrakai

I don‘t think it‘s actually needed in core. Just stating that there are imo better tools when dealing with comparing datetimes than >/</= comparisons – especially when it comes to scheduling.

Where Next?

Popular in Discussions Top

scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
ricklove
I was just introduced to Elixir and Phoenix. I was told about the 2 million websocket test that was done 2 years ago. From my research, t...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20070 166
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
wmnnd
The Go vs Elixir thread got me thinking: Would it be too hard to implement a simple mechanism for creating Go-style static app binaries f...
New
Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
crispinb
On reading dhh’s latest The One Person Framework it strikes me that Phoenix with LiveView is already pretty much this. However, never hav...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs &amp; Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 130579 1222
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement