ical_ctdl_is_overlap() is now symlinked 3 places.
[citadel.git] / citadel / server / modules / calendar / ical_ctdl_is_overlap.c
1 // Check to see if two events overlap.
2 //
3 // This function is used in Citadel Server and in both generations of WebCit.  Symlink it accordingly.
4 //
5 // Copyright (c) 1987-2024 by the citadel.org team
6 //
7 // This program is open source software.  Use, duplication, or disclosure is subject to the GNU General Public License version 3.
8
9 #include <libical/ical.h>
10 #include <string.h>
11
12 // Check to see if two events overlap.  Returns nonzero if they do.
13 int ical_ctdl_is_overlap(
14         struct icaltimetype t1start,
15         struct icaltimetype t1end,
16         struct icaltimetype t2start,
17         struct icaltimetype t2end
18 ) {
19         if (icaltime_is_null_time(t1start)) return(0);
20         if (icaltime_is_null_time(t2start)) return(0);
21
22         // if either event lacks end time, assume end = start
23         if (icaltime_is_null_time(t1end)) {
24                 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
25         }
26         else {
27                 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
28
29                         // The end date is non-inclusive so adjust it by one
30                         // day because our test is inclusive, note that a day is
31                         // not too much because we are talking about all day events.
32                         //
33                         // If start = end we assume that nevertheless the whole day is meant.
34
35                         icaltime_adjust(&t1end, -1, 0, 0, 0);   
36                 }
37         }
38
39         if (icaltime_is_null_time(t2end)) {
40                 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
41         }
42         else {
43                 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
44                         icaltime_adjust(&t2end, -1, 0, 0, 0);   
45                 }
46         }
47
48         // First, check for all-day events
49         if (t1start.is_date || t2start.is_date) {
50                 // If event 1 ends before event 2 starts, there is no match.
51                 if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
52
53                 // If event 2 ends before event 1 starts, there is no match.
54                 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
55
56                 // Otherwise there is at least one day of overlap, so yes we have a match.
57                 return(1);
58         }
59
60 #ifdef DEBUG_ICAL_OVERLAP
61         syslog(LOG_DEBUG, "Comparing t1start %d:%d t1end %d:%d t2start %d:%d t2end %d:%d",
62                 t1start.hour, t1start.minute, t1end.hour, t1end.minute,
63                 t2start.hour, t2start.minute, t2end.hour, t2end.minute
64         );
65 #endif
66
67         // Now check for overlaps using date *and* time.
68
69         // If event 1 ends before event 2 starts, there is no match.
70         if (icaltime_compare(t1end, t2start) <= 0) return(0);
71         // syslog(LOG_DEBUG, "calendar: first passed");
72
73         // If event 2 ends before event 1 starts, there is no match.
74         if (icaltime_compare(t2end, t1start) <= 0) return(0);
75         // syslog(LOG_DEBUG, "calendar: second passed");
76
77         // Otherwise, there is an overlap, so yes we have a match.
78         return(1);
79 }