View Source ExCycle.State (ex_cycle v0.7.2)

The ExCycle.State represents the state of the next generated datetime.

Summary

Types

@type datetime() :: Date.t() | DateTime.t() | NaiveDateTime.t()
@type t() :: %ExCycle.State{
  exhausted?: boolean(),
  iteration: non_neg_integer(),
  next: NaiveDateTime.t(),
  origin: NaiveDateTime.t(),
  result: DateTime.t() | NaiveDateTime.t() | ExCycle.Span.t() | nil,
  week_starting_on: :default | atom()
}

Functions

Link to this function

apply_duration(state, duration)

View Source
Link to this function

apply_timezone(state, timezone)

View Source
Link to this function

check_exhaust(state, count)

View Source
@spec init(t(), datetime()) :: t()

Initializes the state.

This function is important when we need the state to jump to the future, compared to the origin date.

This occurred when we used occurences with a date different of the current datetime or the origin datetime specified (aka starts_at in rule).

Examples

iex> ExCycle.State.init(%ExCycle.State{next: ~N[2024-01-01 10:00:00]}, ~D[2024-01-02])
%ExCycle.State{next: ~N[2024-01-02 00:00:00]}

iex> ExCycle.State.init(%ExCycle.State{next: ~N[2024-01-01 10:00:00]}, ~D[2024-01-01])
%ExCycle.State{next: ~N[2024-01-01 10:00:00]}

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

new(origin \\ NaiveDateTime.utc_now())

View Source
@spec new(datetime()) :: t()

Creates a new state. The next dates will be the same as origin.

Examples

iex> new(~D[2024-01-01])
%ExCycle.State{origin: ~N[2024-01-01 00:00:00], next: ~N[2024-01-01 00:00:00]}

iex> new(~N[2024-01-01 10:00:00])
%ExCycle.State{origin: ~N[2024-01-01 10:00:00], next: ~N[2024-01-01 10:00:00]}
@spec reset(t()) :: t()

Resets the state.

By resetting, we means set the origin value equal to next value.

Examples

iex> reset(%ExCycle.State{origin: ~N[2024-03-01 00:00:00], next: ~N[2024-06-01 00:00:00]})
%ExCycle.State{origin: ~N[2024-06-01 00:00:00], next: ~N[2024-06-01 00:00:00]}
Link to this function

set_next(state, datetime)

View Source
@spec set_next(t(), NaiveDateTime.t()) :: t()
Link to this function

set_week_starting_on(state, week_starting_on)

View Source
@spec set_week_starting_on(t(), :default | atom()) :: t()
Link to this function

update_next(datetime_state, fun)

View Source
@spec update_next(t(), (... -> any())) :: t()

update_next/3 is an helper to update the next value.