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>
13 // Check to see if two events overlap. Returns nonzero if they do.
14 int ical_ctdl_is_overlap(
15 struct icaltimetype t1start,
16 struct icaltimetype t1end,
17 struct icaltimetype t2start,
18 struct icaltimetype t2end
20 if (icaltime_is_null_time(t1start)) return(0);
21 if (icaltime_is_null_time(t2start)) return(0);
23 // if either event lacks end time, assume end = start
24 if (icaltime_is_null_time(t1end)) {
25 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
28 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
30 // The end date is non-inclusive so adjust it by one
31 // day because our test is inclusive, note that a day is
32 // not too much because we are talking about all day events.
34 // If start = end we assume that nevertheless the whole day is meant.
36 icaltime_adjust(&t1end, -1, 0, 0, 0);
40 #ifdef DEBUG_ICAL_OVERLAP
41 syslog(LOG_DEBUG, "Comparing t1start=%s,t1end=%s to t2start=%s,t2end=%s",
42 icaltime_as_ical_string_r(t1start),
43 icaltime_as_ical_string_r(t1end),
44 icaltime_as_ical_string_r(t2start),
45 icaltime_as_ical_string_r(t2end)
50 if (icaltime_is_null_time(t2end)) {
51 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
54 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
55 icaltime_adjust(&t2end, -1, 0, 0, 0);
59 // First, check for all-day events
60 if (t1start.is_date || t2start.is_date) {
61 // If event 1 ends before event 2 starts, there is no match.
62 if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
64 // If event 2 ends before event 1 starts, there is no match.
65 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
67 // Otherwise there is at least one day of overlap, so yes we have a match.
71 // Now check for overlaps using date *and* time.
73 // If event 1 ends before event 2 starts, there is no match.
74 if (icaltime_compare(t1end, t2start) <= 0) return(0);
75 // syslog(LOG_DEBUG, "calendar: first passed");
77 // If event 2 ends before event 1 starts, there is no match.
78 if (icaltime_compare(t2end, t1start) <= 0) return(0);
79 // syslog(LOG_DEBUG, "calendar: second passed");
81 // Otherwise, there is an overlap, so yes we have a match.