Cocktail v0.1.0 Cocktail.Schedule View Source

Struct used to represent a schedule of recurring events.

Use the new/2 function to create a new schedule, and the add_recurrence_rule/2 function to add rules to describe how to repeat.

Currently, Cocktail supports the following types of repeat rules:

  • Weekly - Every week, relative to the schedule’s start time
  • Daily - Every day at the schedule’s start time
  • Hourly - Every hour, starting at the schedule’s start time
  • Minutely - Every minute, starting at the schedule’s start time
  • Secondly - Every second, starting at the schedule’s start time

Once a schedule has been created, you can use occurrences/2 to generate a stream of occurrences, which are either Cocktail.time/0s or Cocktail.Span.t/0s if a duration option was given to the schedule.

Various options can be given to modify the way the repeat rule and schedule behave. See add_recurrence_rule/3 for details on them.

Link to this section Summary

Types

t()

Struct used to represent a schedule of recurring events

Functions

Adds a recurrence rule of the given frequency to a schedule

Parses a string in iCalendar format into a Cocktail.Schedule.t/0

Parses a string of JSON into a Cocktail.Schedule.t/0

Parses JSON-like map into a Cocktail.Schedule.t/0

Creates a new schedule using the given start time and options

Creates a stream of occurrences from the given schedule

Builds an iCalendar format string represenation of a Cocktail.Schedule.t/0

Builds a human readable string represenation of a Cocktail.Schedule.t/0

Link to this section Types

Struct used to represent a schedule of recurring events.

This type is opaque, so its fields shouldn’t be modified directly. Instead, use the functions provided in this module to create and manipulate schedules.

Fields:

  • :start_time - The schedule’s start time
  • :duration - The duration of each occurrence (in seconds)

Link to this section Functions

Link to this function add_recurrence_rule(schedule, frequency, options \\ []) View Source
add_recurrence_rule(t, Cocktail.frequency, Cocktail.rule_options) :: t

Adds a recurrence rule of the given frequency to a schedule.

The frequency can be one of :weekly, :daily, :hourly, :minutely or :secondly.

NOTE: more frequencies are planned to be supported in the future. (e.g. :monthly)

Options

  • :interval - How often to repeat, given the frequency. For example a :daily rule with interval 2 would be “every other day”.
  • :count - The number of times this rule can produce an occurrence. (not yet support)
  • :until - The end date/time after which the rule will no longer produce occurrences.
  • :days - Restrict this rule to specific days. (e.g. [:monday, :wednesday, :friday])
  • :hours - Restrict this rule to certain hours of the day. (e.g. [10, 12, 14])

NOTE: more options are planned to be supported in the future. (e.g. :days_of_month)

Examples

iex> start_time = ~N[2017-01-01 06:00:00]
...> start_time |> new() |> add_recurrence_rule(:daily, interval: 2, hours: [10, 14])
#Cocktail.Schedule<Every 2 days on the 10th and 14th hours of the day>
Link to this function from_i_calendar(i_calendar_string) View Source
from_i_calendar(String.t) :: {:ok, t} | {:error, term}

Parses a string in iCalendar format into a Cocktail.Schedule.t/0.

see Cocktail.Parser.ICalendar.parse/1 for details.

Link to this function from_json(json_string) View Source
from_json(String.t) :: {:ok, t} | {:error, term}

Parses a string of JSON into a Cocktail.Schedule.t/0.

see Cocktail.Parser.JSON.parse/1 for details.

Link to this function from_map(map) View Source
from_map(map) :: {:ok, t} | {:error, term}

Parses JSON-like map into a Cocktail.Schedule.t/0.

see Cocktail.Parser.JSON.parse_map/1 for details.

Creates a new schedule using the given start time and options.

This schdule will be empty and needs recurrence rules added to it before it is useful. Use add_recurrence_rule/3 to add rules to a schedule.

Options

  • :duration - The duration of each event in the schedule (in seconds).

Examples

iex> new(~N[2017-01-01 06:00:00], duration: 3_600)
#Cocktail.Schedule<>
Link to this function occurrences(schedule, start_time \\ nil) View Source
occurrences(t, Cocktail.time | nil) :: Enumerable.t

Creates a stream of occurrences from the given schedule.

An optional start_time can be supplied to not start at the schedule’s start time.

Examples

iex> start_time = ~N[2017-01-01 06:00:00]
...> schedule = start_time |> new() |> add_recurrence_rule(:daily, interval: 2, hours: [10, 14])
...> schedule |> occurrences() |> Enum.take(3)
[~N[2017-01-01 10:00:00],
 ~N[2017-01-01 14:00:00],
 ~N[2017-01-03 10:00:00]]
Link to this function to_i_calendar(schedule) View Source
to_i_calendar(t) :: String.t

Builds an iCalendar format string represenation of a Cocktail.Schedule.t/0.

see Cocktail.Builder.ICalendar.build/1 for details.

Link to this function to_string(schedule) View Source
to_string(t) :: String.t

Builds a human readable string represenation of a Cocktail.Schedule.t/0.

see Cocktail.Builder.String.build/1 for details.