aboutsummaryrefslogblamecommitdiffstats
path: root/calendar/cal-util/cal-recur.c
blob: 711ef5e64d1ca2e4523692271722f0e9d25e9458 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131










































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 * Evolution calendar recurrence rule functions
 *
 * Copyright (C) 2000 Helix Code, Inc.
 *
 * Author: Damon Chaplin <damon@helixcode.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include <string.h>
#include <cal-util/cal-recur.h>
#include <cal-util/timeutil.h>


/*
 * Introduction to The Recurrence Generation Functions:
 *
 * Note: This is pretty complicated. See the iCalendar spec (RFC 2445) for
 *       the specification of the recurrence rules and lots of examples
 *   (sections 4.3.10 & 4.8.5). We also want to support the older
 *   vCalendar spec, though this should be easy since it is basically a
 *   subset of iCalendar.
 *
 * o An iCalendar event can have any number of recurrence rules specifying
 *   occurrences of the event, as well as dates & times of specific
 *   occurrences. It can also have any number of recurrence rules and
 *   specific dates & times specifying exceptions to the occurrences.
 *   So we first merge all the occurrences generated, eliminating any
 *   duplicates, then we generate all the exceptions and remove these to
 *   form the final set of occurrences.
 *
 * o There are 7 frequencies of occurrences: YEARLY, MONTHLY, WEEKLY, DAILY,
 *   HOURLY, MINUTELY & SECONDLY. The 'interval' property specifies the
 *   multiples of the frequency between each 'set' of occurrences. So for
 *   a YEARLY frequency with an interval of 3, we generate a set of occurrences
 *   for every 3rd year. We use complete years here -  any generated
 *   occurrences that occur before the event's start (or after its end)
 *   are just discarded.
 *
 * o There are 8 frequency modifiers: BYMONTH, BYWEEKNO, BYYEARDAY, BYMONTHDAY,
 *   BYDAY, BYHOUR & BYSECOND. These can either add extra occurrences or
 *   filter out occurrences. For example 'FREQ=YEARLY;BYMONTH=1,2' produces
 *   2 occurrences for each year rather than the default 1. And
 *   'FREQ=DAILY; BYMONTH=1' filters out all occurrences except those in Jan.
 *   If the modifier works on periods which are less than the recurrence
 *   frequency, then extra occurrences are added, else occurrences are
 *   filtered. So we have 2 functions for each modifier - one to expand events
 *   and the other to filter. We use a table of functions for each frequency
 *   which points to the appropriate function to use for each modifier.
 *
 * o Any number of frequency modifiers can be used in a recurrence rule
 *   (though BYWEEKNO can only be used in a YEARLY rule). They are applied in
 *   the order given above.
 *
 * o After the set of occurrences for the frequency interval are generated,
 *   the BYSETPOS property is used to select which of the occurrences are
 *   finally output. If BYSETPOS is not specified then all the occurrences are
 *   output.
 */


#define CAL_OBJ_NUM_FILTERS 8

typedef gboolean (*CalObjFindStartFn) (CalObjTime *event_start,
                       CalObjTime *event_end,
                       Recurrence *recur,
                       CalObjTime *interval_start,
                       CalObjTime *interval_end,
                       CalObjTime *cotime);
typedef gboolean (*CalObjFindNextFn)  (CalObjTime *cotime,
                       CalObjTime *event_end,
                       Recurrence *recur,
                       CalObjTime *interval_end);
typedef GArray* (*CalObjFilterFn)    (Recurrence *recur,
                      GArray     *occs);

typedef struct _CalObjRecurVTable CalObjRecurVTable;
struct _CalObjRecurVTable {
    CalObjFindStartFn find_start_position;
    CalObjFindNextFn find_next_position;
    CalObjFilterFn filters[CAL_OBJ_NUM_FILTERS];
};


static CalObjRecurVTable* cal_obj_get_vtable (Recurrence *recur);
static void cal_obj_sort_occurrences (GArray *occs);
static gint cal_obj_time_compare_func (const void *arg1,
                       const void *arg2);
static void cal_obj_remove_duplicates (GArray *occs);
static GArray* cal_obj_bysetpos_filter (GArray *occs);


static gboolean cal_obj_yearly_find_start_position (CalObjTime *event_start,
                            CalObjTime *event_end,
                            Recurrence *recur,
                            CalObjTime *interval_start,
                            CalObjTime *interval_end,
                            CalObjTime *cotime);
static gboolean cal_obj_yearly_find_next_position  (CalObjTime *cotime,
                            CalObjTime *event_end,
                            Recurrence *recur,
                            CalObjTime *interval_end);

static gboolean cal_obj_monthly_find_start_position (CalObjTime *event_start,
                             CalObjTime *event_end,
                             Recurrence *recur,
                             CalObjTime *interval_start,
                             CalObjTime *interval_end,
                             CalObjTime *cotime);
static gboolean cal_obj_monthly_find_next_position  (CalObjTime *cotime,
                             CalObjTime *event_end,
                             Recurrence *recur,
                             CalObjTime *interval_end);

static gboolean cal_obj_weekly_find_start_position (CalObjTime *event_start,
                            CalObjTime *event_end,
                            Recurrence *recur,
                            CalObjTime *interval_start,
                            CalObjTime *interval_end,
                            CalObjTime *cotime);
static gboolean cal_obj_weekly_find_next_position  (CalObjTime *cotime,
                            CalObjTime *event_end,
                            Recurrence *recur,
                            CalObjTime *interval_end);

static gboolean cal_obj_daily_find_start_position  (CalObjTime *event_start,
                            CalObjTime *event_end,
                            Recurrence *recur,
                            CalObjTime *interval_start,
                            CalObjTime *interval_end,
                            CalObjTime *cotime);
static gboolean cal_obj_daily_find_next_position   (CalObjTime *cotime,
                            CalObjTime *event_end,
                            Recurrence *recur,
                            CalObjTime *interval_end);

static gboolean cal_obj_hourly_find_start_position (CalObjTime *event_start,
                            CalObjTime *event_end,
                            Recurrence *recur,
                            CalObjTime *interval_start,
                            CalObjTime *interval_end,
                            CalObjTime *cotime);
static gboolean cal_obj_hourly_find_next_position  (CalObjTime *cotime,
                            CalObjTime *event_end,
                            Recurrence *recur,
                            CalObjTime *interval_end);

static gboolean cal_obj_minutely_find_start_position (CalObjTime *event_start,
                              CalObjTime *event_end,
                              Recurrence *recur,
                              CalObjTime *interval_start,
                              CalObjTime *interval_end,
                              CalObjTime *cotime);
static gboolean cal_obj_minutely_find_next_position  (CalObjTime *cotime,
                              CalObjTime *event_end,
                              Recurrence *recur,
                              CalObjTime *interval_end);

static gboolean cal_obj_secondly_find_start_position (CalObjTime *event_start,
                              CalObjTime *event_end,
                              Recurrence *recur,
                              CalObjTime *interval_start,
                              CalObjTime *interval_end,
                              CalObjTime *cotime);
static gboolean cal_obj_secondly_find_next_position  (CalObjTime *cotime,
                              CalObjTime *event_end,
                              Recurrence *recur,
                              CalObjTime *interval_end);

static GArray* cal_obj_bymonth_expand       (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_bymonth_filter       (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_byweekno_expand      (Recurrence *recur,
                         GArray     *occs);
#if 0
/* This isn't used at present. */
static GArray* cal_obj_byweekno_filter      (Recurrence *recur,
                         GArray     *occs);
#endif
static GArray* cal_obj_byyearday_expand     (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_byyearday_filter     (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_bymonthday_expand    (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_bymonthday_filter    (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_byday_expand     (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_byday_filter     (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_byhour_expand        (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_byhour_filter        (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_byminute_expand      (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_byminute_filter      (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_bysecond_expand      (Recurrence *recur,
                         GArray     *occs);
static GArray* cal_obj_bysecond_filter      (Recurrence *recur,
                         GArray     *occs);

static void cal_obj_time_add_days (CalObjTime *cotime,
                   gint        days);


CalObjRecurVTable cal_obj_yearly_vtable = {
    cal_obj_yearly_find_start_position,
    cal_obj_yearly_find_next_position,
    {
        cal_obj_bymonth_expand,
        cal_obj_byweekno_expand,
        cal_obj_byyearday_expand,
        cal_obj_bymonthday_expand,
        cal_obj_byday_expand,
        cal_obj_byhour_expand,
        cal_obj_byminute_expand,
        cal_obj_bysecond_expand
    },
};

CalObjRecurVTable cal_obj_monthly_vtable = {
    cal_obj_monthly_find_start_position,
    cal_obj_monthly_find_next_position,
    {
        cal_obj_bymonth_filter,
        NULL,
        cal_obj_byyearday_filter,
        cal_obj_bymonthday_expand,
        cal_obj_byday_expand,
        cal_obj_byhour_expand,
        cal_obj_byminute_expand,
        cal_obj_bysecond_expand
    },
};

CalObjRecurVTable cal_obj_weekly_vtable = {
    cal_obj_weekly_find_start_position,
    cal_obj_weekly_find_next_position,
    {
        cal_obj_bymonth_filter,
        NULL,
        cal_obj_byyearday_filter,
        cal_obj_bymonthday_filter,
        cal_obj_byday_expand,
        cal_obj_byhour_expand,
        cal_obj_byminute_expand,
        cal_obj_bysecond_expand
    },
};

CalObjRecurVTable cal_obj_daily_vtable = {
    cal_obj_daily_find_start_position,
    cal_obj_daily_find_next_position,
    {
        cal_obj_bymonth_filter,
        NULL,
        cal_obj_byyearday_filter,
        cal_obj_bymonthday_filter,
        cal_obj_byday_filter,
        cal_obj_byhour_expand,
        cal_obj_byminute_expand,
        cal_obj_bysecond_expand
    },
};

CalObjRecurVTable cal_obj_hourly_vtable = {
    cal_obj_hourly_find_start_position,
    cal_obj_hourly_find_next_position,
    {
        cal_obj_bymonth_filter,
        NULL,
        cal_obj_byyearday_filter,
        cal_obj_bymonthday_filter,
        cal_obj_byday_filter,
        cal_obj_byhour_filter,
        cal_obj_byminute_expand,
        cal_obj_bysecond_expand
    },
};

CalObjRecurVTable cal_obj_minutely_vtable = {
    cal_obj_minutely_find_start_position,
    cal_obj_minutely_find_next_position,
    {
        cal_obj_bymonth_filter,
        NULL,
        cal_obj_byyearday_filter,
        cal_obj_bymonthday_filter,
        cal_obj_byday_filter,
        cal_obj_byhour_filter,
        cal_obj_byminute_filter,
        cal_obj_bysecond_expand
    },
};

CalObjRecurVTable cal_obj_secondly_vtable = {
    cal_obj_secondly_find_start_position,
    cal_obj_secondly_find_next_position,
    {
        cal_obj_bymonth_filter,
        NULL,
        cal_obj_byyearday_filter,
        cal_obj_bymonthday_filter,
        cal_obj_byday_filter,
        cal_obj_byhour_filter,
        cal_obj_byminute_filter,
        cal_obj_bysecond_filter
    },
};




/* Returns an unsorted array of time_t's resulting from expanding the
   recurrence within the given interval. Each iCalendar event can have any
   number of recurrence rules specifying occurrences of the event, as well as
   any number of recurrence rules specifying exceptions. */
GArray*
cal_obj_expand_recurrence       (CalObjTime *event_start,
                     CalObjTime *event_end,
                     Recurrence *recur,
                     CalObjTime *interval_start,
                     CalObjTime *interval_end)
{
    CalObjRecurVTable *vtable;
    CalObjTime occ;
    GArray *all_occs, *occs;
    gint filter;

    vtable = cal_obj_get_vtable (recur);

    /* This is the resulting array of CalObjTime elements. */
    all_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));

    /* Get the first period based on the frequency and the interval that
       intersects the interval between start and end. */
    if ((*vtable->find_start_position) (event_start, event_end, recur,
                        interval_start, interval_end,
                        &occ))
        return all_occs;

    /* Loop until the event ends or we go past the end of the required
       interval. */
    for (;;) {

        /* We start with just the one time in the set. */
        occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));
        g_array_append_val (occs, occ);

        /* Generate the set of occurrences for this period. */
        for (filter = 0; filter < CAL_OBJ_NUM_FILTERS; filter++) {
            if (vtable->filters[filter])
                occs = (*vtable->filters[filter]) (recur,
                                   occs);
        }

        /* Sort the occurrences and remove duplicates. */
        cal_obj_sort_occurrences (occs);
        cal_obj_remove_duplicates (occs);

        /* Apply the BYSETPOS property. */
        occs = cal_obj_bysetpos_filter (occs);

        /* Add the occurrences onto the main array. */
        g_array_append_vals (all_occs, occs->data, occs->len);

        /* Skip to the next period, or exit the loop if finished. */
        if ((*vtable->find_next_position) (&occ, event_end, recur,
                           interval_end))
            break;
    }

    return all_occs;
}


/* Returns the function table corresponding to the recurrence frequency. */
static CalObjRecurVTable*
cal_obj_get_vtable (Recurrence *recur)
{
    switch (recur->type) {
    case RECUR_YEARLY:
        return &cal_obj_yearly_vtable;
    case RECUR_MONTHLY:
        return &cal_obj_monthly_vtable;
    case RECUR_WEEKLY:
        return &cal_obj_weekly_vtable;
    case RECUR_DAILY:
        return &cal_obj_daily_vtable;
    case RECUR_HOURLY:
        return &cal_obj_hourly_vtable;
    case RECUR_MINUTELY:
        return &cal_obj_minutely_vtable;
    case RECUR_SECONDLY:
        return &cal_obj_secondly_vtable;
    }
    return NULL;
}


static void
cal_obj_sort_occurrences (GArray *occs)
{
    qsort (occs->data, occs->len, sizeof (CalObjTime),
           cal_obj_time_compare_func);
}


static gint
cal_obj_time_compare_func (const void *arg1,
               const void *arg2)
{
    CalObjTime *cotime1, *cotime2;

    cotime1 = (CalObjTime*) arg1;
    cotime2 = (CalObjTime*) arg2;

    if (cotime1->year < cotime2->year)
        return -1;
    if (cotime1->year > cotime2->year)
        return 1;

    if (cotime1->month < cotime2->month)
        return -1;
    if (cotime1->month > cotime2->month)
        return 1;

    if (cotime1->day < cotime2->day)
        return -1;
    if (cotime1->day > cotime2->day)
        return 1;

    if (cotime1->hour < cotime2->hour)
        return -1;
    if (cotime1->hour > cotime2->hour)
        return 1;

    if (cotime1->minute < cotime2->minute)
        return -1;
    if (cotime1->minute > cotime2->minute)
        return 1;

    if (cotime1->second < cotime2->second)
        return -1;
    if (cotime1->second > cotime2->second)
        return 1;

    return 0;
}


static void
cal_obj_remove_duplicates (GArray *occs)
{
    CalObjTime *occ, *prev_occ = NULL;
    gint len, i, j = 0;

    len = occs->len;
    for (i = 0; i < len; i++) {
        occ = &g_array_index (occs, CalObjTime, i);

        if (!prev_occ
            || cal_obj_time_compare_func (occ, prev_occ) != 0) {
            if (i != j)
                g_array_index (occs, CalObjTime, j)
                    = g_array_index (occs, CalObjTime, i);
            j++;
        }

        prev_occ = occ;
    }

    g_array_set_size (occs, j);
}


static GArray*
cal_obj_bysetpos_filter (GArray *occs)
{

    return occs;
}




static gboolean
cal_obj_yearly_find_start_position (CalObjTime *event_start,
                    CalObjTime *event_end,
                    Recurrence *recur,
                    CalObjTime *interval_start,
                    CalObjTime *interval_end,
                    CalObjTime *cotime)
{


    return FALSE;
}


static gboolean
cal_obj_yearly_find_next_position (CalObjTime *cotime,
                   CalObjTime *event_end,
                   Recurrence *recur,
                   CalObjTime *interval_end)
{
    /* NOTE: The day may now be invalid, e.g. 29th Feb.
       Make sure we remove these eventually. */
    cotime->year += recur->interval;

    if (cotime->year > event_end->year
        || cotime->year > interval_end->year)
        return TRUE;

    return FALSE;
}



static gboolean
cal_obj_monthly_find_start_position (CalObjTime *event_start,
                     CalObjTime *event_end,
                     Recurrence *recur,
                     CalObjTime *interval_start,
                     CalObjTime *interval_end,
                     CalObjTime *cotime)
{


    return FALSE;
}


static gboolean
cal_obj_monthly_find_next_position (CalObjTime *cotime,
                    CalObjTime *event_end,
                    Recurrence *recur,
                    CalObjTime *interval_end)
{
    cotime->month += recur->interval;
    cotime->year += cotime->month / 12;
    cotime->month %= 12;

    if (cotime->year > event_end->year
        || cotime->year > interval_end->year
        || (cotime->year == event_end->year
        && cotime->month > event_end->month)
        || (cotime->year == interval_end->year
        && cotime->month > interval_end->month))
        return TRUE;

    return FALSE;
}



static gboolean
cal_obj_weekly_find_start_position (CalObjTime *event_start,
                    CalObjTime *event_end,
                    Recurrence *recur,
                    CalObjTime *interval_start,
                    CalObjTime *interval_end,
                    CalObjTime *cotime)
{


    return FALSE;
}


static gboolean
cal_obj_weekly_find_next_position (CalObjTime *cotime,
                   CalObjTime *event_end,
                   Recurrence *recur,
                   CalObjTime *interval_end)
{
    cal_obj_time_add_days (cotime, recur->interval);



    return FALSE;
}


static gboolean
cal_obj_daily_find_start_position  (CalObjTime *event_start,
                    CalObjTime *event_end,
                    Recurrence *recur,
                    CalObjTime *interval_start,
                    CalObjTime *interval_end,
                    CalObjTime *cotime)
{


    return FALSE;
}


static gboolean
cal_obj_daily_find_next_position  (CalObjTime *cotime,
                   CalObjTime *event_end,
                   Recurrence *recur,
                   CalObjTime *interval_end)
{

    cal_obj_time_add_days (cotime, recur->interval);


    return FALSE;
}


static gboolean
cal_obj_hourly_find_start_position (CalObjTime *event_start,
                    CalObjTime *event_end,
                    Recurrence *recur,
                    CalObjTime *interval_start,
                    CalObjTime *interval_end,
                    CalObjTime *cotime)
{


    return FALSE;
}


static gboolean
cal_obj_hourly_find_next_position (CalObjTime *cotime,
                   CalObjTime *event_end,
                   Recurrence *recur,
                   CalObjTime *interval_end)
{


    return FALSE;
}


static gboolean
cal_obj_minutely_find_start_position (CalObjTime *event_start,
                      CalObjTime *event_end,
                      Recurrence *recur,
                      CalObjTime *interval_start,
                      CalObjTime *interval_end,
                      CalObjTime *cotime)
{


    return FALSE;
}


static gboolean
cal_obj_minutely_find_next_position (CalObjTime *cotime,
                     CalObjTime *event_end,
                     Recurrence *recur,
                     CalObjTime *interval_end)
{


    return FALSE;
}


static gboolean
cal_obj_secondly_find_start_position (CalObjTime *event_start,
                      CalObjTime *event_end,
                      Recurrence *recur,
                      CalObjTime *interval_start,
                      CalObjTime *interval_end,
                      CalObjTime *cotime)
{


    return FALSE;
}


static gboolean
cal_obj_secondly_find_next_position (CalObjTime *cotime,
                     CalObjTime *event_end,
                     Recurrence *recur,
                     CalObjTime *interval_end)
{


    return FALSE;
}





/* If the BYMONTH rule is specified it expands each occurrence in occs, by
   using each of the months in the bymonth list. */
static GArray*
cal_obj_bymonth_expand      (Recurrence *recur,
                 GArray     *occs)
{
    GArray *new_occs;
    CalObjTime *occ;
    GList *elem;
    gint len, i;

    /* If BYMONTH has not been specified, or the array is empty, just
       return the array. */
    if (!recur->bymonth || occs->len == 0)
        return occs;

    new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));

    len = occs->len;
    for (i = 0; i < len; i++) {
        occ = &g_array_index (occs, CalObjTime, i);

        elem = recur->bymonth;
        while (elem) {
            /* NOTE: The day may now be invalid, e.g. 31st Feb.
               Make sure we remove these eventually. */
            occ->month = GPOINTER_TO_INT (elem->data);
            g_array_append_vals (new_occs, occ, 1);
            elem = elem->next;
        }
    }

    g_array_free (occs, TRUE);

    return new_occs;
}


/* If the BYMONTH rule is specified it filters out all occurrences in occs
   which do not match one of the months in the bymonth list. */
static GArray*
cal_obj_bymonth_filter      (Recurrence *recur,
                 GArray     *occs)
{
    GArray *new_occs;
    CalObjTime *occ;
    guint8 months[12];
    gint mon, len, i;
    GList *elem;

    /* If BYMONTH has not been specified, or the array is empty, just
       return the array. */
    elem = recur->bymonth;
    if (!elem || occs->len == 0)
        return occs;

    /* Create an array of months from bymonths for fast lookup. */
    memset (&months, 0, sizeof (months));
    while (elem) {
        mon = GPOINTER_TO_INT (elem->data);
        months[mon] = 1;
        elem = elem->next;
    }

    new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));

    len = occs->len;
    for (i = 0; i < len; i++) {
        occ = &g_array_index (occs, CalObjTime, i);
        if (months[occ->month])
            g_array_append_vals (new_occs, occ, 1);
    }

    g_array_free (occs, TRUE);

    return new_occs;
}



static GArray*
cal_obj_byweekno_expand     (Recurrence *recur,
                 GArray     *occs)
{

    return occs;
}


#if 0
/* This isn't used at present. */
static GArray*
cal_obj_byweekno_filter     (Recurrence *recur,
                 GArray     *occs)
{

    return occs;
}
#endif


static GArray*
cal_obj_byyearday_expand    (Recurrence *recur,
                 GArray     *occs)
{

    return occs;
}


static GArray*
cal_obj_byyearday_filter    (Recurrence *recur,
                 GArray     *occs)
{

    return occs;
}



static GArray*
cal_obj_bymonthday_expand   (Recurrence *recur,
                 GArray     *occs)
{

    return occs;
}


static GArray*
cal_obj_bymonthday_filter   (Recurrence *recur,
                 GArray     *occs)
{

    return occs;
}



static GArray*
cal_obj_byday_expand        (Recurrence *recur,
                 GArray     *occs)
{

    return occs;
}


static GArray*
cal_obj_byday_filter        (Recurrence *recur,
                 GArray     *occs)
{

    return occs;
}



/* If the BYHOUR rule is specified it expands each occurrence in occs, by
   using each of the hours in the byhour list. */
static GArray*
cal_obj_byhour_expand       (Recurrence *recur,
                 GArray     *occs)
{
    GArray *new_occs;
    CalObjTime *occ;
    GList *elem;
    gint len, i;

    /* If BYHOUR has not been specified, or the array is empty, just
       return the array. */
    if (!recur->byhour || occs->len == 0)
        return occs;

    new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));

    len = occs->len;
    for (i = 0; i < len; i++) {
        occ = &g_array_index (occs, CalObjTime, i);

        elem = recur->byhour;
        while (elem) {
            occ->hour = GPOINTER_TO_INT (elem->data);
            g_array_append_vals (new_occs, occ, 1);
            elem = elem->next;
        }
    }

    g_array_free (occs, TRUE);

    return new_occs;
}


/* If the BYHOUR rule is specified it filters out all occurrences in occs
   which do not match one of the hours in the byhour list. */
static GArray*
cal_obj_byhour_filter       (Recurrence *recur,
                 GArray     *occs)
{
    GArray *new_occs;
    CalObjTime *occ;
    guint8 hours[24];
    gint hour, len, i;
    GList *elem;

    /* If BYHOURUTE has not been specified, or the array is empty, just
       return the array. */
    elem = recur->byhour;
    if (!elem || occs->len == 0)
        return occs;

    /* Create an array of hours from byhour for fast lookup. */
    memset (&hours, 0, sizeof (hours));
    while (elem) {
        hour = GPOINTER_TO_INT (elem->data);
        hours[hour] = 1;
        elem = elem->next;
    }

    new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));

    len = occs->len;
    for (i = 0; i < len; i++) {
        occ = &g_array_index (occs, CalObjTime, i);
        if (hours[occ->hour])
            g_array_append_vals (new_occs, occ, 1);
    }

    g_array_free (occs, TRUE);

    return new_occs;
}



/* If the BYMINUTE rule is specified it expands each occurrence in occs, by
   using each of the minutes in the byminute list. */
static GArray*
cal_obj_byminute_expand     (Recurrence *recur,
                 GArray     *occs)
{
    GArray *new_occs;
    CalObjTime *occ;
    GList *elem;
    gint len, i;

    /* If BYMINUTE has not been specified, or the array is empty, just
       return the array. */
    if (!recur->byminute || occs->len == 0)
        return occs;

    new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));

    len = occs->len;
    for (i = 0; i < len; i++) {
        occ = &g_array_index (occs, CalObjTime, i);

        elem = recur->byminute;
        while (elem) {
            occ->minute = GPOINTER_TO_INT (elem->data);
            g_array_append_vals (new_occs, occ, 1);
            elem = elem->next;
        }
    }

    g_array_free (occs, TRUE);

    return new_occs;
}


/* If the BYMINUTE rule is specified it filters out all occurrences in occs
   which do not match one of the minutes in the byminute list. */
static GArray*
cal_obj_byminute_filter     (Recurrence *recur,
                 GArray     *occs)
{
    GArray *new_occs;
    CalObjTime *occ;
    guint8 minutes[60];
    gint min, len, i;
    GList *elem;

    /* If BYMINUTE has not been specified, or the array is empty, just
       return the array. */
    elem = recur->byminute;
    if (!elem || occs->len == 0)
        return occs;

    /* Create an array of minutes from byminutes for fast lookup. */
    memset (&minutes, 0, sizeof (minutes));
    while (elem) {
        min = GPOINTER_TO_INT (elem->data);
        minutes[min] = 1;
        elem = elem->next;
    }

    new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));

    len = occs->len;
    for (i = 0; i < len; i++) {
        occ = &g_array_index (occs, CalObjTime, i);
        if (minutes[occ->minute])
            g_array_append_vals (new_occs, occ, 1);
    }

    g_array_free (occs, TRUE);

    return new_occs;
}



/* If the BYSECOND rule is specified it expands each occurrence in occs, by
   using each of the seconds in the bysecond list. */
static GArray*
cal_obj_bysecond_expand     (Recurrence *recur,
                 GArray     *occs)
{
    GArray *new_occs;
    CalObjTime *occ;
    GList *elem;
    gint len, i;

    /* If BYSECOND has not been specified, or the array is empty, just
       return the array. */
    if (!recur->bysecond || occs->len == 0)
        return occs;

    new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));

    len = occs->len;
    for (i = 0; i < len; i++) {
        occ = &g_array_index (occs, CalObjTime, i);

        elem = recur->bysecond;
        while (elem) {
            occ->second = GPOINTER_TO_INT (elem->data);
            g_array_append_vals (new_occs, occ, 1);
            elem = elem->next;
        }
    }

    g_array_free (occs, TRUE);

    return new_occs;
}


/* If the BYSECOND rule is specified it filters out all occurrences in occs
   which do not match one of the seconds in the bysecond list. */
static GArray*
cal_obj_bysecond_filter     (Recurrence *recur,
                 GArray     *occs)
{
    GArray *new_occs;
    CalObjTime *occ;
    guint8 seconds[61];
    gint sec, len, i;
    GList *elem;

    /* If BYSECOND has not been specified, or the array is empty, just
       return the array. */
    elem = recur->bysecond;
    if (!elem || occs->len == 0)
        return occs;

    /* Create an array of seconds from byseconds for fast lookup. */
    memset (&seconds, 0, sizeof (seconds));
    while (elem) {
        sec = GPOINTER_TO_INT (elem->data);
        seconds[sec] = 1;
        elem = elem->next;
    }

    new_occs = g_array_new (FALSE, FALSE, sizeof (CalObjTime));

    len = occs->len;
    for (i = 0; i < len; i++) {
        occ = &g_array_index (occs, CalObjTime, i);
        if (seconds[occ->second])
            g_array_append_vals (new_occs, occ, 1);
    }

    g_array_free (occs, TRUE);

    return new_occs;
}



static void
cal_obj_time_add_days (CalObjTime *cotime,
               gint    days)
{
    guint day, days_in_month;

    /* We use a guint to avoid overflow on the guint8. */
    day = (guint) cotime->day;
    day += days;

    for (;;) {
        days_in_month = time_days_in_month (cotime->year,
                            cotime->month);
        if (day <= days_in_month)
            break;

        cotime->month++;
        if (cotime->month >= 12) {
            cotime->year++;
            cotime->month = 0;
        }

        day -= days_in_month;
    }

    cotime->day = (guint8) day;
}