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)) {
29 // The end date is non-inclusive so adjust it by one day because our test is inclusive.
30 // Note that a day is not too much because we are talking about all day events.
31 // If start==end we assume that it means one day.
32 icaltime_adjust(&t1end, -1, 0, 0, 0);
36 #ifdef DEBUG_ICAL_OVERLAP
37 syslog(LOG_DEBUG, "Comparing t1start=%s(%s),t1end=%s(%s) to t2start=%s(%s),t2end=%s(%s)",
38 icaltime_as_ical_string_r(t1start), icaltime_get_tzid(t1start),
39 icaltime_as_ical_string_r(t1end), icaltime_get_tzid(t1end),
40 icaltime_as_ical_string_r(t2start), icaltime_get_tzid(t2start),
41 icaltime_as_ical_string_r(t2end), icaltime_get_tzid(t2end)
45 if (icaltime_is_null_time(t2end)) {
46 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
49 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
50 icaltime_adjust(&t2end, -1, 0, 0, 0);
54 // First, check for all-day events
55 if (t1start.is_date || t2start.is_date) {
56 // If event 1 ends before event 2 starts, there is no match.
57 if (icaltime_compare_date_only(t1end, t2start) < 0) return(0);
59 // If event 2 ends before event 1 starts, there is no match.
60 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
62 // Otherwise there is at least one day of overlap, so yes we have a match.
66 // Now check for overlaps using date *and* time.
67 // We could probably implement this in terms of icaltime_span_overlaps() but this does the same thing.
69 // If event 1 ends before event 2 starts, there is no match.
70 if (icaltime_compare(t1end, t2start) <= 0) return(0);
72 // If event 2 ends before event 1 starts, there is no match.
73 if (icaltime_compare(t2end, t1start) <= 0) return(0);
75 // Otherwise, there is an overlap, so yes we have a match.