Month - library focused on working with months, rather than Date/DateTime

At Heresy we have a lot of date logic around months and quarters and forecasting horizons. We started using Elixir’s Date module, naturally, but ended with a lot of functions that deal with the necessary day field of the Date struct.

We wanted to simplify our code a bit and ended up extracting some of that code into month, which now could be found in the usual places:

Here’s a sample:

iex> import Month.Sigils
Month.Sigils

iex> Month.utc_now!()
~M[2019-03]

iex> range = Month.Range.new!(~M[2019-01], ~M[2019-03])
#Month.Range<~M[2019-01], ~M[2019-03]>

iex> range.months
[~M[2019-01], ~M[2019-02], ~M[2019-03]]

iex> Month.Period.shift(range, 3)
#Month.Range<~M[2019-04], ~M[2019-06]>

Everything is built on top of Date so no extra dependencies.

It’s a very specific use case perhaps, but hopefully someone would find it useful! :laughing:

6 Likes

Nice!

Shameless plug, I’ve run into a similar problem and created https://github.com/wojtekmach/calendar_interval which is a little bit more generic:

iex> CalendarInterval.utc_now(:month)
~I"2019-03"

iex> Enum.to_list(~I"2019-01/03")
[~I"2019-01", ~I"2019-02", ~I"2019-03"]
3 Likes