CalendarRecurrence (calendar_recurrence v0.2.0)

View Source

Stream of recurring dates.

Options:

  • start - The start of the recurrence
  • stop - When to stop the recurrence. Defaults to :never
  • unit - The interval for each recurrence, Defaults to :day
  • step - The count of how many units to apply for each recurrence. Defaults to 1

When the :start is an Elixir DateTime struct with a timezone other than "Etc/UTC" the recurrence will be calculated in that timezone, so that the wall clock time is stable even when switching between summer and winter time. That means the time will be the same even when the day has a duration of 23h or 25h.

Examples

iex> recurrence = CalendarRecurrence.new(start: ~D[2018-01-01])
iex> Enum.take(recurrence, 3)
[~D[2018-01-01], ~D[2018-01-02], ~D[2018-01-03]]

iex> recurrence = CalendarRecurrence.new(start: ~N[2018-01-01 12:00:00])
iex> Enum.take(recurrence, 3)
[~N[2018-01-01 12:00:00], ~N[2018-01-02 12:00:00], ~N[2018-01-03 12:00:00]]

iex> recurrence = CalendarRecurrence.new(start: ~U[2018-01-01 12:00:00Z])
iex> Enum.take(recurrence, 3)
[~U[2018-01-01 12:00:00Z], ~U[2018-01-02 12:00:00Z], ~U[2018-01-03 12:00:00Z]]

iex> recurrence = CalendarRecurrence.new(start: ~U[2018-01-01 12:00:00Z], unit: :hour)
iex> Enum.take(recurrence, 3)
[~U[2018-01-01 12:00:00Z], ~U[2018-01-01 13:00:00Z], ~U[2018-01-01 14:00:00Z]]

iex> recurrence = CalendarRecurrence.new(start: ~U[2018-01-01 12:00:00Z], unit: :hour, step: 2)
iex> Enum.take(recurrence, 3)
[~U[2018-01-01 12:00:00Z], ~U[2018-01-01 14:00:00Z], ~U[2018-01-01 16:00:00Z]]

iex> recurrence = CalendarRecurrence.new(start: ~D[2018-01-01], stop: {:count, 3})
iex> Enum.to_list(recurrence)
[~D[2018-01-01], ~D[2018-01-02], ~D[2018-01-03]]

iex> recurrence = CalendarRecurrence.new(start: ~D[2018-01-01], stop: {:until, ~D[2018-01-03]})
iex> Enum.to_list(recurrence)
[~D[2018-01-01], ~D[2018-01-02], ~D[2018-01-03]]

iex> recurrence = CalendarRecurrence.new(start: ~D[2018-01-01], step: fn _ -> 2 end)
iex> Enum.take(recurrence, 3)
[~D[2018-01-01], ~D[2018-01-03], ~D[2018-01-05]]

Summary

Types

date()

stepper()

@type stepper() :: (current :: date() -> pos_integer())

t()

@type t() :: %CalendarRecurrence{
  start: date(),
  step: pos_integer() | stepper(),
  stop: :never | {:until, date()} | {:count, non_neg_integer()},
  unit: unit()
}

unit()

@type unit() :: :day | :hour | :minute | System.time_unit()

Functions

new(opts)

@spec new(keyword()) :: t()