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:
- https://hex.pm/packages/month
- https://hexdocs.pm/month/Month.html
- https://github.com/heresydev/month
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!