I always struggle to read code that compares `DateTime`s using
DateTime.compare/2, so I've been playing with a more readable API for it.
I've come up with this, that feels pretty nice to use:
```
defmodule DateTime do
def is?(a, [before: b]) do
:lt == DateTime.compare(a, b)
end
def is?(a, [after: b]) do
:gt == DateTime.compare(a, b)
end
end
```
Sample:
```
t1 = DateTime.utc_now()
t2 = DateTime.add(t1, 1, :second)
true = DateTime.is?(t1, before: t2)
false = t1
|> DateTime.is?(after: t2)
```
>From some discussion in the Discord, it seems I'm not the only one who
struggles with the :gt/:eq/:lt and argument order in DateTime.compare. It's
also been pointed out that this doesn't follow elixir's stdlib api
conventions, so maybe two separate `DateTime.is_before(a, b)` and
`DateTime.is_after(a, b)` functions would be a better fit.
Thoughts?
--
You received this message because you are subscribed to the Google Groups
"elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/elixir-lang-core/7a881bdc-3233-4d42-86d1-57aeb6e8ff3fn%40googlegroups.com.