ExCycle.Rule (ex_cycle v0.3.0)

Representation of a RRule following RFC 5455 (not fully compatible for the moment).

Summary

Functions

Initializes the rule starting from the from datetime.

Defines a new Rule struct.

Returns the next dates that match validations.

Types

@type t() :: %ExCycle.Rule{
  count: non_neg_integer() | nil,
  duration: Duration.t() | nil,
  state: ExCycle.State.t() | nil,
  timezone: String.t() | nil,
  until: Date.t() | nil,
  validations: [ExCycle.Validations.any_validation(), ...]
}

Functions

Link to this function

init(rule, from)

@spec init(t(), ExCycle.datetime()) :: t()

Initializes the rule starting from the from datetime.

Examples

iex> init(%Rule{}, ~D[2024-01-01])
%ExCycle{}

iex> init(%Rule{}, ~N[2024-01-01 10:00:00])
%ExCycle{}
Link to this function

new(frequency, opts \\ [])

Defines a new Rule struct.

Options

  • duration: A simple Duration struct.
  • count (not implemented yet).
  • until (not implemented yet).
  • interval: base interval of the recurren rule (daily, weekly ....).
  • start_at: Reference date to used to every recurrent event generated.
  • timezone: TimeZone to use when generating recurrent dates.

Examples

iex> new(:daily, hours: [20, 10])
%ExCycle.Rule{
  validations: [
    %ExCycle.Validations.HourOfDay{hours: [10, 20]},
    %ExCycle.Validations.Interval{frequency: :daily, value: 1},
    %ExCycle.Validations.DateValidation{},
    %ExCycle.Validations.Lock{unit: :minute},
    %ExCycle.Validations.Lock{unit: :second}
  ],
  timezone: nil,
  count: nil,
  until: nil,
  state: %ExCycle.State{}
}

iex> new(:daily, hours: [20, 10], starts_at: ~N[2024-04-22 17:00:00], timezone: "America/Montreal")
%ExCycle.Rule{
  validations: [
    %ExCycle.Validations.HourOfDay{hours: [10, 20]},
    %ExCycle.Validations.Interval{frequency: :daily, value: 1},
    %ExCycle.Validations.DateValidation{},
    %ExCycle.Validations.Lock{unit: :minute},
    %ExCycle.Validations.Lock{unit: :second}
  ],
  timezone: "America/Montreal",
  count: nil,
  until: nil,
  state: %ExCycle.State{starts_at: ~N[2024-04-22 17:00:00]}
}
@spec next(t()) :: t()

Returns the next dates that match validations.

Examples

iex> rule =
...>   Rule.new(:daily, interval: 2, hours: [20, 10])
...>   |> Map.put(:state, ExCycle.State.new(~D[2024-04-04]))
...> rule = Rule.next(rule)
%Rule{state: %ExCycle.State{next: ~N[2024-04-04 10:00:00]}}