adamu
I’m having a discussion with a colleague about how to calculate the beginning of the next month given a certain Date using the core library. We have come up with two solutions, but cannot agree on which one is better. Are there any technical reasons to prefer one over the other here? Or better options (without using external libraries)?
Version that relies on using the calendar to calculate the number of days to the next month:
def beginning_of_next_month(%Date{} = date) do
days_in_month = Date.days_in_month(date)
date
|> Date.beginning_of_month()
|> Date.add(days_in_month)
end
Version that increments the month and relies on pattern matching to handle rolling over the year:
def beginning_of_next_month(%Date{year: year, month: 12}),
do: Date.new!(year + 1, 1, 1)
def beginning_of_next_month(%Date{year: year, month: month}),
do: Date.new!(year, month + 1, 1)
- Date.days_in_month/1 + Date.add/2
- Pattern match + Date.new/3
0
voters
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
c4710n
I prefer version 1 - it uses the public API of
Date. It is stable. If it is depracated, compiler will warn you about that.version 2 relies on the structure of
Datestruct. Generally, I think it is the moving part, and will change potentially in the future. (although the probability is very low)qhwa
I like the increment version but I think it’s incorrect because the
dayshould be always1.I’ll probably write:
Simple math seems clearer than public API combos to me.
Anyway, it feels good to have
Date.beginning_of_next_month/1out of the box.adamu
Thanks for the correction, have fixed it. That’ll teach me to re-write things for the purposes of posting here - introduced a bug in the process.
But, for your version, my understanding is that manually updating the
Datestruct is bad practice - because it bypasses the checks for invalid dates provided by functions such asDate.new!. UsingDate.new!is why the 2nd version receives the year also.Hmm, so that’s one each so far…
trisolaran
Personally, I’d go for:
soup
I find the first simpler to understand. I think it may also may handle non Gregorian calendars “for free” where as the pattern matched makes assumptions on the underlying calendar at play? Not something I have actually tested.
Pretty esoteric argument though, in most cases.
eksperimental
Even simpler:
~D[2000-01-01] |> Date.end_of_month() |> Date.add(1)eksperimental
Funny fact, we wrote the same solution.
adamu
Perfect. I completely missed
Date.end_of_monthtrisolaran
hehe true! Sometimes it’s just a matter of who can type faster
jdumont
I’ve had similar debates about how best to find the next date matching a given pattern.
e.g. Today is Wednesday 26 Jan 2022, what is the date of the next (and every following) Friday and Monday?
I found this simple with Swift thanks to some very sophisticated calendar APIs available, but couldn’t find a decent solution that didn’t make any assumptions about the particular calendar in use when I came back to Elixir.
I ended up with a similar solution to @trisolaran where I would move from my current date to a known point and work from there, but the pattern matched function calls got really fiddly to account for all the possible patterns I wanted to calculate. It was never something I was confident in.