+
+ do {
+ /* Convert the DTSTART and DTEND properties to an icalperiod. */
+ this_event_period.start = dtstart;
+
+ if (!icaltime_is_null_time(dtend)) {
+ this_event_period.end = dtend;
+ }
+
+ /* Convert the timestamps to UTC. It's ok to do this because we've already expanded
+ * recurrences and this data is never going to get used again.
+ */
+ this_event_period.start = icaltime_convert_to_zone(
+ this_event_period.start,
+ icaltimezone_get_utc_timezone()
+ );
+ this_event_period.end = icaltime_convert_to_zone(
+ this_event_period.end,
+ icaltimezone_get_utc_timezone()
+ );
+
+ /* Now add it. */
+ icalcomponent_add_property(fb, icalproperty_new_freebusy(this_event_period));
+
+ /* Make sure the DTSTART property of the freebusy *list* is set to
+ * the DTSTART property of the *earliest event*.
+ */
+ p = icalcomponent_get_first_property(fb, ICAL_DTSTART_PROPERTY);
+ if (p == NULL) {
+ icalcomponent_set_dtstart(fb, this_event_period.start);
+ }
+ else {
+ if (icaltime_compare(this_event_period.start, icalcomponent_get_dtstart(fb)) < 0) {
+ icalcomponent_set_dtstart(fb, this_event_period.start);
+ }
+ }
+
+ /* Make sure the DTEND property of the freebusy *list* is set to
+ * the DTEND property of the *latest event*.
+ */
+ p = icalcomponent_get_first_property(fb, ICAL_DTEND_PROPERTY);
+ if (p == NULL) {
+ icalcomponent_set_dtend(fb, this_event_period.end);
+ }
+ else {
+ if (icaltime_compare(this_event_period.end, icalcomponent_get_dtend(fb)) > 0) {
+ icalcomponent_set_dtend(fb, this_event_period.end);
+ }
+ }
+
+ if (rrule) {
+ dtstart = icalrecur_iterator_next(ritr);
+ if (!icaltime_is_null_time(dtend)) {
+ dtend = icaltime_add(dtstart, dur);
+ dtend.zone = dtstart.zone;
+ dtend.is_utc = dtstart.is_utc;
+ }
+ ++num_recur;
+ }
+
+ } while ( (rrule) && (!icaltime_is_null_time(dtstart)) && (num_recur < MAX_RECUR) ) ;
+ icalrecur_iterator_free(ritr);