diff options
| author | erdgeist <erdgeist@dyn-174.club.berlin.ccc.de> | 2009-04-09 20:11:38 +0200 |
|---|---|---|
| committer | erdgeist <erdgeist@dyn-174.club.berlin.ccc.de> | 2009-04-09 20:11:38 +0200 |
| commit | 8c6b4dcf08268ed2971b06df40c733ba28448582 (patch) | |
| tree | 7fe9b0b5c756e385c846b13a4bde095672774d93 /lib/chaos_calendar/chaos_calendar.c | |
| parent | 2ca3a93bca6ebc8e4639c1dc028eb7804bc1f727 (diff) | |
Make chaos calendar gem
Diffstat (limited to 'lib/chaos_calendar/chaos_calendar.c')
| -rw-r--r-- | lib/chaos_calendar/chaos_calendar.c | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/chaos_calendar/chaos_calendar.c b/lib/chaos_calendar/chaos_calendar.c new file mode 100644 index 00000000..e59c1ed4 --- /dev/null +++ b/lib/chaos_calendar/chaos_calendar.c | |||
| @@ -0,0 +1,76 @@ | |||
| 1 | #include <ruby.h> | ||
| 2 | #include <ical.h> | ||
| 3 | #include <time.h> | ||
| 4 | |||
| 5 | //#define RRULE "FREQ=MONTHLY;BYMONTH=1,2,3,4,5,6,7,8,9,10,11;BYDAY=-1WE;UNTIL=20091105T220000" | ||
| 6 | //#define RRULE "FREQ=DAILY;UNTIL=20991111T220000;VFOO" | ||
| 7 | |||
| 8 | VALUE occurrences( VALUE dtstart, VALUE dtend, char *rrule ) { | ||
| 9 | struct icaltimetype start, end; | ||
| 10 | time_t tt; | ||
| 11 | VALUE tv_sec, occurr = rb_ary_new(); | ||
| 12 | |||
| 13 | /* Get method ID for Time.tv_sec */ | ||
| 14 | ID time_tv_sec = rb_intern( "tv_sec" ); | ||
| 15 | ID time_to_time = rb_intern( "to_time" ); | ||
| 16 | |||
| 17 | if( !rb_respond_to( dtstart, time_tv_sec ) ) { | ||
| 18 | if( rb_respond_to( dtstart, time_to_time ) ) | ||
| 19 | dtstart = rb_funcall( dtstart, time_to_time, 0 ); | ||
| 20 | else | ||
| 21 | rb_raise( rb_eTypeError, "Can't convert dtstart into a Time-like object." ); | ||
| 22 | } | ||
| 23 | |||
| 24 | if( !rb_respond_to( dtend, time_tv_sec ) ) { | ||
| 25 | if( rb_respond_to( dtend, time_to_time ) ) | ||
| 26 | dtend = rb_funcall( dtend, time_to_time, 0 ); | ||
| 27 | else | ||
| 28 | rb_raise( rb_eTypeError, "Can't convert dtend into a Time-like object." ); | ||
| 29 | } | ||
| 30 | |||
| 31 | /* Apply .tv_sec to our Time objects (if they are Times ...) */ | ||
| 32 | tv_sec = rb_funcall( dtstart, time_tv_sec, 0 ); | ||
| 33 | tt = NUM2INT( tv_sec ); | ||
| 34 | start = icaltime_from_timet( tt, 0 ); | ||
| 35 | |||
| 36 | tv_sec = rb_funcall( dtend, time_tv_sec, 0 ); | ||
| 37 | tt = NUM2INT( tv_sec ); | ||
| 38 | end = icaltime_from_timet( tt, 0 ); | ||
| 39 | |||
| 40 | icalerror_clear_errno(); | ||
| 41 | icalerror_set_error_state( ICAL_MALFORMEDDATA_ERROR, ICAL_ERROR_NONFATAL); | ||
| 42 | |||
| 43 | struct icalrecurrencetype recur = icalrecurrencetype_from_string( rrule ); | ||
| 44 | if( icalerrno != ICAL_NO_ERROR ) { | ||
| 45 | rb_raise(rb_eArgError, "Malformed RRule"); | ||
| 46 | return Qnil; | ||
| 47 | } | ||
| 48 | |||
| 49 | icalrecur_iterator* ritr = icalrecur_iterator_new( recur, start ); | ||
| 50 | |||
| 51 | while(1) { | ||
| 52 | struct icaltimetype next = icalrecur_iterator_next(ritr); | ||
| 53 | |||
| 54 | if( icaltime_is_null_time(next) || ( icaltime_compare( next, end ) > 0 ) ) { | ||
| 55 | icalrecur_iterator_free(ritr); | ||
| 56 | return occurr; | ||
| 57 | } | ||
| 58 | |||
| 59 | rb_ary_push( occurr, rb_time_new( icaltime_as_timet( next ), 0 ) ); | ||
| 60 | }; | ||
| 61 | |||
| 62 | icalrecur_iterator_free(ritr); | ||
| 63 | return occurr; | ||
| 64 | } | ||
| 65 | |||
| 66 | VALUE duration_to_fixnum( char * duration ) { | ||
| 67 | icalerror_clear_errno(); | ||
| 68 | icalerror_set_error_state( ICAL_MALFORMEDDATA_ERROR, ICAL_ERROR_NONFATAL); | ||
| 69 | |||
| 70 | struct icaldurationtype dur_struct = icaldurationtype_from_string( duration ); | ||
| 71 | |||
| 72 | if( icaldurationtype_is_bad_duration( dur_struct ) ) | ||
| 73 | rb_raise(rb_eArgError, "Malformed Duration"); | ||
| 74 | |||
| 75 | return LONG2FIX(icaldurationtype_as_int( dur_struct )); | ||
| 76 | } | ||
