ExCycle (ex_cycle v0.3.0)

Documentation for ExCycle.

Summary

Functions

Adds a new rule using a frequency.

Defines a new ExCycle struct.

Creates a stream of occurrences using the from as reference.

Types

@type datetime() :: DateTime.t() | NaiveDateTime.t() | Date.t()
@type t() :: %ExCycle{rules: [ExCycle.Rule.t()]}

Functions

Link to this function

add_rule(cycle, frequency, opts \\ [])

@spec add_rule(t(), ExCycle.Validations.Interval.t(), keyword()) :: t()

Adds a new rule using a frequency.

The frequency could be one of: :secondly, :minutely, :hourly, :daily, :weekly, :monthly or :yearly.

Options

  • :interval: the interval of the frequency (interval: 2 will generate a x + n * 2 result)
  • :hours: set a restricted on hours of the day (hours: [20, 10] will generate every "frequency" at "10:00" and "20:00")

Examples

iex> add_rule(%ExCycle{rules: []}, :daily, interval: 2, hours: [20, 10])
%ExCycle.Rule{
  validations: [
    %ExCycle.Validations.HourOfDay{hours: [10, 20]},
    %ExCycle.Validations.Interval{frequency: :daily, value: 2},
    ...
  ],
  ...
}
@spec new() :: t()

Defines a new ExCycle struct.

Examples

iex> new()
%ExCycle{}
Link to this function

occurrences(cycle, from)

@spec occurrences(t(), datetime()) ::
  Enumerable.t(NaiveDateTime.t() | ExCycle.Span.t())

Creates a stream of occurrences using the from as reference.

Examples

iex> cycle =
...>   new()
...>   |> add_rule(:daily, interval: 2, hours: [20, 10])
...>   |> add_rule(:daily, interval: 1, hours: [15])
...>   |> occurrences(~D[2024-02-29])
...>   |> Enum.take(9)
[
   ~N[2024-02-29 10:00:00],
   ~N[2024-02-29 15:00:00],
   ~N[2024-02-29 20:00:00],
   ~N[2024-03-01 15:00:00],
   ~N[2024-03-02 10:00:00],
   ~N[2024-03-02 15:00:00],
   ~N[2024-03-02 20:00:00],
   ~N[2024-03-03 15:00:00],
   ~N[2024-03-04 10:00:00]
]