4 * Function to go through an ical component set and convert all non-UTC
5 * date/time properties to UTC. It also strips out any VTIMEZONE
6 * subcomponents afterwards, because they're irrelevant.
14 #include <sys/types.h>
21 #include "citserver.h"
22 #include "sysdep_decls.h"
26 #ifdef CITADEL_WITH_CALENDAR_SERVICE
28 #include "ical_dezonify.h"
32 * Figure out which time zone needs to be used for timestamps that are
33 * not UTC and do not have a time zone specified.
35 * FIXME - most sites are not in New York :)
37 icaltimezone *get_default_icaltimezone(void) {
39 char *location = NULL;
40 icaltimezone *zone = NULL;
42 location = "America/New_York";
44 zone = icaltimezone_get_builtin_timezone(location);
47 zone = icaltimezone_get_utc_timezone();
54 * Back end function for ical_dezonify()
56 * We supply this with the master component, the relevant component,
57 * and the property (which will be a DTSTART, DTEND, etc.)
58 * which we want to convert to UTC.
60 void ical_dezonify_backend(icalcomponent *cal,
64 icaltimezone *t = NULL;
67 struct icaltimetype TheTime;
69 /* Give me nothing and I will give you nothing in return. */
70 if (cal == NULL) return;
72 /* Hunt for a TZID parameter in this property. */
73 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
75 /* Get the stringish name of this TZID. */
77 tzid = icalparameter_get_tzid(param);
79 /* Convert it to an icaltimezone type. */
81 t = icalcomponent_get_timezone(cal, tzid);
86 /* Now we know the timezone. Convert to UTC. */
88 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
89 TheTime = icalproperty_get_dtstart(prop);
91 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
92 TheTime = icalproperty_get_dtend(prop);
94 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
95 TheTime = icalproperty_get_due(prop);
97 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
98 TheTime = icalproperty_get_exdate(prop);
104 lprintf(CTDL_DEBUG, " * Was: %s\n", icaltime_as_ical_string(TheTime));
105 if (TheTime.is_utc) {
106 lprintf(CTDL_DEBUG, " * This property is ALREADY UTC.\n");
109 /* Do the conversion. */
111 lprintf(CTDL_DEBUG, " * Timezone prop found. Converting to UTC.\n");
114 lprintf(CTDL_DEBUG, " * Converting default timezone to UTC.\n");
118 t = get_default_icaltimezone();
121 icaltimezone_convert_time(&TheTime,
123 icaltimezone_get_utc_timezone()
128 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
129 lprintf(CTDL_DEBUG, " * Now: %s\n", icaltime_as_ical_string(TheTime));
131 /* Now add the converted property back in. */
132 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
133 icalproperty_set_dtstart(prop, TheTime);
135 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
136 icalproperty_set_dtend(prop, TheTime);
138 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
139 icalproperty_set_due(prop, TheTime);
141 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
142 icalproperty_set_exdate(prop, TheTime);
148 * Recursive portion of ical_dezonify()
150 void ical_dezonify_recur(icalcomponent *cal, icalcomponent *rcal) {
155 * Recurse through all subcomponents *except* VTIMEZONE ones.
157 for (c=icalcomponent_get_first_component(
158 rcal, ICAL_ANY_COMPONENT);
160 c = icalcomponent_get_next_component(
161 rcal, ICAL_ANY_COMPONENT)
163 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
164 ical_dezonify_recur(cal, c);
169 * Now look for DTSTART and DTEND properties
171 for (p=icalcomponent_get_first_property(
172 rcal, ICAL_ANY_PROPERTY);
174 p = icalcomponent_get_next_property(
175 rcal, ICAL_ANY_PROPERTY)
178 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
179 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
180 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
181 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
183 ical_dezonify_backend(cal, rcal, p);
190 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
191 * This function will search any VTIMEZONE subcomponents to learn the
192 * relevant timezone information.
194 void ical_dezonify(icalcomponent *cal) {
195 icalcomponent *vt = NULL;
197 lprintf(CTDL_DEBUG, "ical_dezonify() started\n");
199 /* Convert all times to UTC */
200 ical_dezonify_recur(cal, cal);
202 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
203 while (vt = icalcomponent_get_first_component(
204 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
205 icalcomponent_remove_component(cal, vt);
206 icalcomponent_free(vt);
209 lprintf(CTDL_DEBUG, "ical_dezonify() completed\n");
213 #endif /* CITADEL_WITH_CALENDAR_SERVICE */