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 Responses

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

Where Next?

Popular in Discussions Top

mikl
I wanted to capitalize a string, and tried using String.capitalize(). That generally works well, until you try to capitalize a word like...
New
sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
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
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
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 19568 166
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
AstonJ
Can you believe the first professionally published Elixir book was published just 8 years ago? Since then I think we’ve seen more books f...
New
paulanthonywilson
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things. But I don’t think this is ...
New

Other popular topics Top

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
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
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement