adamu

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

Showing Posts 1 to 10

c4710n

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 Date struct. Generally, I think it is the moving part, and will change potentially in the future. (although the probability is very low)

qhwa

qhwa

I like the increment version but I think it’s incorrect because the day should be always 1.

I’ll probably write:

def beginning_of_next_month(%Date{year: year, month: 12}),
  do: %{date | year: year + 1, month: 1, day: 1}

def beginning_of_next_month(%Date{month: month}),
  do: %{date | month: month + 1, day: 1}

Simple math seems clearer than public API combos to me.
Anyway, it feels good to have Date.beginning_of_next_month/1 out of the box.

adamu

adamu OP

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 Date struct is bad practice - because it bypasses the checks for invalid dates provided by functions such as Date.new!. Using Date.new! is why the 2nd version receives the year also.

Hmm, so that’s one each so far… :thinking:

trisolaran

trisolaran

Personally, I’d go for:

date
|> Date.end_of_month()
|> Date.add(1)
11
Post #4
soup

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

eksperimental

Even simpler:
~D[2000-01-01] |> Date.end_of_month() |> Date.add(1)

eksperimental

eksperimental

Funny fact, we wrote the same solution.

adamu

adamu OP

Perfect. I completely missed Date.end_of_month :man_facepalming:

trisolaran

trisolaran

hehe true! Sometimes it’s just a matter of who can type faster :slight_smile:

jdumont

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.

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
RemyXRenard
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
velrest
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
samoloth
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
FlyingNoodle
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 Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews