2. Working with recurrence

2.1. Date::Set

To create the event in Example 1, you could use the following code.

use Net::ICal;
use Date::Set;

my $today = Date::ICal->new; # no parameters gets 'now'

my $rule = Net::ICal::Recurrence->new (freq => WEEKLY, byday => 'FR');
my $set = Date::Set->event ( rule => $rule->as_ical);
my $dtstart = $set->during (at => $today->ical)->min->string;

my $event = Net::ICal::Event->new (dtstart => $dtstart;
				   summary => 'End of week drinks',
				   rrule => [$rule],

Example 2. Code for “every friday”

Because the DTSTART always marks the first occurence, you have to make sure it is a Friday. As you can see, you can use the Date::Set module to calculate the first occurrence of a rule for you. You just give the rule, and ask for the first occurence after a certain date (“today” here)

If you want to find out when the occurences of a recurring event actually are, for example to fill out the month view of your calendaring application, you have to expand the rules and times listed in the event. Date::Set can also help you with this.

use Date::Set;

# when the event occurs: every friday.
my $set = Date::Set->event ( rule => 'FREQ=WEEKLY;BYDAY=FR');

# in what period we want occurences: december 2002
my $period = Date::Set->period (time => ['20021201', '20021231']);

my @occurs = $set->during (at => $period)->list;
print "occurences on ", join (" ,", @occurs), "\n";

Example 3. Finding the fridays in December 2002

In Example 3 we create a set of events that contains every friday. Then we ask for all the occurences in December 2002, returned as a list.

Of course, you usually have your data in a Net::ICal::Event. That has a occurences method that does all the hard work for you. It takes a Net::ICal::Period as a named parameter, which does the same as $period in Example 3, limit the period for which we want occurences.

The occurences method doesn't just handle the RRULE properties of an event, but also RDATE, EXRULE and EXDATE properties. And it is available for Net::ICal::Todo and Net::ICal::Journal as well.

2.2. ICal::RecurrenceParser

This module helps you create recurrence rules by parsing english expressions like “every weekday” and turning them into iCalendar recurrence rules.

2.3. ICal::PeriodParser

This module helps you create Net::ICal period objects by parsing english expressions like “next week”.

2.4. Tipi::Widget::RecurEditor

This module lets you put a nice recurrence rule editor inside your Gtk application.