1 // Check to see if two events overlap.
3 // This function is used in Citadel Server and in both generations of WebCit. Symlink it accordingly.
5 // Copyright (c) 1987-2024 by the citadel.org team
7 // This program is open source software. Use, duplication, or disclosure is subject to the GNU General Public License version 3.
9 #include <libical/ical.h>
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
19 if (icaltime_is_null_time(t1start)) return(0);
20 if (icaltime_is_null_time(t2start)) return(0);
22 // if either event lacks end time, assume end = start
23 if (icaltime_is_null_time(t1end)) {
24 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
27 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
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.
33 // If start = end we assume that nevertheless the whole day is meant.
35 icaltime_adjust(&t1end, -1, 0, 0, 0);
39 if (icaltime_is_null_time(t2end)) {
40 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
43 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
44 icaltime_adjust(&t2end, -1, 0, 0, 0);
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);
53 // If event 2 ends before event 1 starts, there is no match.
54 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
56 // Otherwise there is at least one day of overlap, so yes we have a match.
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
67 // Now check for overlaps using date *and* time.
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");
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");
77 // Otherwise, there is an overlap, so yes we have a match.