Month.Period (Month v2.2.0)

Copy Markdown View Source

Represents a period of 1 month or more.

iex> range = Month.Period.new(~M[2019-01], ~M[2019-03])
{:ok, #Month.Period<~M[2019-01], ~M[2019-03]>}
iex> range.months
[~M[2019-01], ~M[2019-02], ~M[2019-03]]

The months field contains all months within the period, inclusive.

If you want a guarantee that the period would cover min 2 months or more, look at Month.Range data structure instead.

Summary

Functions

Helper functions that returns the months between the two given months, inclusive. Please make sure from_month is before to_month.

Creates a new Month.Period using given Months as a start and an end.

Sames as new/2 but returs either result or raises an exception.

Shifts the given period forwards or backwards by given number of months.

Checks if the first period is within the second period (inclusive).

Types

t()

@type t() :: %Month.Period{end: Month.t(), months: [Month.t()], start: Month.t()}

Functions

months(from, to)

Helper functions that returns the months between the two given months, inclusive. Please make sure from_month is before to_month.

new(first, last)

@spec new(Date.t(), Date.t()) :: {:ok, t()} | {:error, String.t()}
@spec new(Month.t(), Month.t()) :: {:ok, t()} | {:error, String.t()}

Creates a new Month.Period using given Months as a start and an end.

Examples

iex> Month.Period.new(~M[2019-01], ~M[2019-03])
{:ok, #Month.Period<~M[2019-01], ~M[2019-03]>}

iex> Month.Period.new(~M[2019-03], ~M[2019-01])
{:ok, #Month.Period<~M[2019-01], ~M[2019-03]>}

new!(first, last)

@spec new!(Date.t(), Date.t()) :: t()
@spec new!(Month.t(), Month.t()) :: t()

Sames as new/2 but returs either result or raises an exception.

shift(period, num_months)

@spec shift(t(), integer()) :: t()
@spec shift(Month.Range.t(), integer()) :: Month.Range.t()

Shifts the given period forwards or backwards by given number of months.

within?(date, period)

@spec within?(t(), t()) :: boolean()
@spec within?(Month.Range.t(), Month.Range.t()) :: boolean()
@spec within?(Date.t(), t()) :: boolean()

Checks if the first period is within the second period (inclusive).