Do not convert compared times to UTC.
[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 #include <syslog.h>
12
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
19 ) {
20         if (icaltime_is_null_time(t1start)) return(0);
21         if (icaltime_is_null_time(t2start)) return(0);
22
23         // if either event lacks end time, assume end = start
24         if (icaltime_is_null_time(t1end)) {
25                 memcpy(&t1end, &t1start, sizeof(struct icaltimetype));
26         }
27         else {
28                 if (t1end.is_date && icaltime_compare(t1start, t1end)) {
29
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.
33                         //
34                         // If start = end we assume that nevertheless the whole day is meant.
35
36                         icaltime_adjust(&t1end, -1, 0, 0, 0);   
37                 }
38         }
39
40 #ifdef DEBUG_ICAL_OVERLAP
41         syslog(LOG_DEBUG, "Comparing t1start=%s(%s),t1end=%s(%s) to t2start=%s(%s),t2end=%s(%s)",
42                 icaltime_as_ical_string_r(t1start),     icaltime_get_tzid(t1start),
43                 icaltime_as_ical_string_r(t1end),       icaltime_get_tzid(t1end),
44                 icaltime_as_ical_string_r(t2start),     icaltime_get_tzid(t2start),
45                 icaltime_as_ical_string_r(t2end),       icaltime_get_tzid(t2end)
46         );
47 #endif
48
49
50         if (icaltime_is_null_time(t2end)) {
51                 memcpy(&t2end, &t2start, sizeof(struct icaltimetype));
52         }
53         else {
54                 if (t2end.is_date && icaltime_compare(t2start, t2end)) {
55                         icaltime_adjust(&t2end, -1, 0, 0, 0);   
56                 }
57         }
58
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);
63
64                 // If event 2 ends before event 1 starts, there is no match.
65                 if (icaltime_compare_date_only(t2end, t1start) < 0) return(0);
66
67                 // Otherwise there is at least one day of overlap, so yes we have a match.
68                 return(1);
69         }
70
71         // Now check for overlaps using date *and* time.
72
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");
76
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");
80
81         // Otherwise, there is an overlap, so yes we have a match.
82         return(1);
83 }