summaryrefslogtreecommitdiff
path: root/lib/chaos_calendar/ical_occurrences.c
diff options
context:
space:
mode:
authorhukl <contact@smyck.org>2009-04-11 00:32:20 +0200
committerhukl <contact@smyck.org>2009-04-11 00:32:20 +0200
commitfb1b58233df206fff3ec4237caf5fe5576b80a9b (patch)
tree473216f85267f41238fa777f6a48d36745b4d609 /lib/chaos_calendar/ical_occurrences.c
parent44e1eff0155d3da825ae7f4ef9e334a8e231e909 (diff)
removed lib/chaos_calendar as we now have the erdgeist-chaos_calendar gem on github - YAY!
Diffstat (limited to 'lib/chaos_calendar/ical_occurrences.c')
-rw-r--r--lib/chaos_calendar/ical_occurrences.c76
1 files changed, 0 insertions, 76 deletions
diff --git a/lib/chaos_calendar/ical_occurrences.c b/lib/chaos_calendar/ical_occurrences.c
deleted file mode 100644
index 30a8d856..00000000
--- a/lib/chaos_calendar/ical_occurrences.c
+++ /dev/null
@@ -1,76 +0,0 @@
1#include <ruby.h>
2#include <libical/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
8VALUE 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
66VALUE 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}