The best way to programmatically handle recurrence

Recurrence handling is an essential aspect of scheduling and event management systems.

Edouard Courty
3 min readFeb 6, 2023
Photo by Waldemar on Unsplash

Recurrence handling is an essential aspect of scheduling and event management systems. Working with repetitions of events can be a challenging task for a programmer, and it’s important to have a solution that’s both robust and flexible.

One such solution is the RRULE (Recurrence Rule) and its implementation in the iCalendar format.

RRULE is a specification that outlines how recurring events should be represented in a calendar. It’s defined in RFC-5545, also known as the iCalendar specification, which is a widely accepted standard for representing calendar data.
The RRULE specification provides a way of describing repeating events in a simple and standardized manner, making it easy to implement in a variety of programming languages.

The iCalendar format provides a comprehensive set of options for representing repeating events using the RRULE specification. With its robust set of options, you can specify events that repeat daily, weekly, monthly, or yearly, and also specify start and end dates, exceptions, and more. This format also provides a way of representing other information about an event, such as its time, location, and description.

Suppose you run a subscription-based business and want to generate orders for recurring deliveries of products to your customers.

You can use RRULE to specify the recurrence pattern for the deliveries and generate dates based on that pattern.

Here’s a code example in PHP that demonstrates this, using the rlanvin/php-rrule library:

<?php

// Every month on the 15th day for 12 times
$rrule = "RRULE:FREQ=MONTHLY;COUNT=12;BYMONTHDAY=15;DTSTART=20230201T000000Z";

$orders = [];

$rrule = new RRule($rrule);

/** @var DateTime $occurrence */
foreach ($rrule as $occurrence) {
// Do something
$order = new Order();
$order->setDate($occurrence);

$orders[] = $order;
}
?>

In this example, the RRULE specifies that the deliveries will occur monthly on the 15th day of the month, and will repeat for 12 occurrences starting on February 1st, 2023.

The $rrule variable is an RRule object representing the recurrence rule. The foreach loop iterates over the occurrences generated by the RRule object and generates an order for each occurrence.

In this way, you can use RRULE to generate a series of orders based on a recurring delivery schedule, making it easy to automate your subscription-based business processes.

Here’s a list of some of the most widely used libraries for working with RRULE in the top 10 most used programming languages:

  1. Python: dateutil, rrule
  2. Java: ical4j, biweekly
  3. JavaScript / TypeScript: rrule, rrule.js
  4. PHP: RRule, php-rrule
  5. C#: NCalendar, DDay.iCal
  6. Ruby: Ice_cube, recurrent
  7. Go: rrule-go, RRULE
  8. Swift: DateTools, RRule
  9. Kotlin: ical4j, biweekly

NB: This is not an exhaustive list and there may be other libraries available for each programming language. The popularity and usage of libraries may also vary depending on the specific use case and requirements.

In conclusion, RRULE and the iCalendar format provide a robust and flexible solution for handling recurring events.

Its standardized representation makes it easy to implement in a variety of programming languages, and its comprehensive set of options allows you to represent a wide range of recurring events.

Whether you’re building a scheduling system or integrating with an existing one, RRULE and iCalendar are excellent choices for representing recurring events.

If you liked this story, feel free to check my other ones! I mainly write about programming & tech stuff in general.

If you like my content, follow me for more!

As always, have a good day 🥳

--

--

Edouard Courty

Web Developer & IT Teacher based in Paris - Back-end guru - Co-founder of @IMXrarity