Question

This is a bit frustrating. I've been working on this for a while now, and I can't seem to get this method to work like it says it does.

#include "icalrecur.h"
#include <time.h> /* for time() */
#include <stdio.h>

int get_occurrences(char* rrule, time_t start, int count)
{
        //char*        rule; /* rule string */
       // *rule = PG_GETARG_CHAR(0);

        time_t   *result[count]; /* output array */

        icalrecur_expand_recurrence(rrule, start, count, *result);

        return (time_t) *result;
}


//time_t *output[5*8];

void main() {
        time_t right_now = time(0);
        char *_rrule = "FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH";
        get_occurrences(_rrule, right_now, 5);
}

I save this test file as ical_recur.h. Then I type in bash:

gcc -I/usr/local/libical/include/libical -L/usr/local/libical/lib/ -lical -o hello ical_recur.c

To include the libical.a libraries. The include/libical directory has icalrecur.h in it, so I really don't even need to be including the whole ical library.

~: ./hello
Segmentation fault

Anytime I change around any pointers, it starts complaining about something during compilation. Can anyone get this to work?? Source files are from Marketcircle on github.

Was it helpful?

Solution

Looking at the documentation it seems that you have an unwanted extra level of indirection - you need to change:

    time_t   *result[count]; /* output array */

    icalrecur_expand_recurrence(rrule, start, count, *result);

to:

    time_t   result[count]; /* output array */

    icalrecur_expand_recurrence(rrule, start, count, result);

Also you're passing a read-only string literal to a function which expects a char * - this should at least give you a compiler warning (hint: always use gcc -Wall ..., read the warnings carefully, understand them and fix them). main() should look more like this:

int main() {
    time_t right_now = time(0);
    char _rrule[] = "FREQ=WEEKLY;INTERVAL=2;COUNT=8;WKST=SU;BYDAY=TU,TH";
    get_occurrences(_rrule, right_now, 5);
    return 0;
}

Some more problems:

  • this line doesn't do anything useful, but it's not clear what you're trying to achieve:

    char _size = (char)(((int)'0') + sizeof(result));
    

    all those casts are a "code smell" and this should tell you that you're doing something very wrong here

  • your function is defined as returning an int, but you're trying to cast your array to a time_t and return that, which also makes no sense - again, turn on compiler warnings - let the compiler help you find and fix your mistakes.

OTHER TIPS

Now you can use this extension for postgresql.

Example usage:

SELECT * FROM
     unnest(
         rrule_get_occurrences('FREQ=WEEKLY;INTERVAL=1;WKST=MO;UNTIL=20200101T045102Z;BYDAY=SA;BYHOUR=10;BYMINUTE=51;BYSECOND=2'::rrule,
             '2019-12-07 10:51:02+00'::timestamp with time zone)
     );

          unnest
 ------------------------
  2019-12-07 10:51:02+00
  2019-12-14 10:51:02+00
  2019-12-21 10:51:02+00
  2019-12-28 10:51:02+00
 (4 rows)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top