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 icaltimezone *zone = NULL;
42 This doesn't even belong here.
43 I'm just keeping it here until I put it somewhere permanent.
47 zones = icaltimezone_get_builtin_timezones();
48 for (i = 0; i < zones->num_elements; i++) {
49 lprintf(CTDL_DEBUG, "%s\n", icaltimezone_get_location(icalarray_element_at(zones, i)));
54 zone = icaltimezone_get_builtin_timezone(config.c_default_cal_zone);
57 zone = icaltimezone_get_utc_timezone();
64 * Back end function for ical_dezonify()
66 * We supply this with the master component, the relevant component,
67 * and the property (which will be a DTSTART, DTEND, etc.)
68 * which we want to convert to UTC.
70 void ical_dezonify_backend(icalcomponent *cal,
74 icaltimezone *t = NULL;
77 struct icaltimetype TheTime;
79 /* Give me nothing and I will give you nothing in return. */
80 if (cal == NULL) return;
82 /* Hunt for a TZID parameter in this property. */
83 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
85 /* Get the stringish name of this TZID. */
87 tzid = icalparameter_get_tzid(param);
89 /* Convert it to an icaltimezone type. */
91 t = icalcomponent_get_timezone(cal, tzid);
96 /* Now we know the timezone. Convert to UTC. */
98 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
99 TheTime = icalproperty_get_dtstart(prop);
101 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
102 TheTime = icalproperty_get_dtend(prop);
104 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
105 TheTime = icalproperty_get_due(prop);
107 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
108 TheTime = icalproperty_get_exdate(prop);
114 lprintf(CTDL_DEBUG, " * Was: %s\n", icaltime_as_ical_string(TheTime));
115 if (TheTime.is_utc) {
116 lprintf(CTDL_DEBUG, " * This property is ALREADY UTC.\n");
119 /* Do the conversion. */
121 lprintf(CTDL_DEBUG, " * Timezone prop found. Converting to UTC.\n");
124 lprintf(CTDL_DEBUG, " * Converting default timezone to UTC.\n");
128 t = get_default_icaltimezone();
131 icaltimezone_convert_time(&TheTime,
133 icaltimezone_get_utc_timezone()
138 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
139 lprintf(CTDL_DEBUG, " * Now: %s\n", icaltime_as_ical_string(TheTime));
141 /* Now add the converted property back in. */
142 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
143 icalproperty_set_dtstart(prop, TheTime);
145 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
146 icalproperty_set_dtend(prop, TheTime);
148 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
149 icalproperty_set_due(prop, TheTime);
151 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
152 icalproperty_set_exdate(prop, TheTime);
158 * Recursive portion of ical_dezonify()
160 void ical_dezonify_recur(icalcomponent *cal, icalcomponent *rcal) {
165 * Recurse through all subcomponents *except* VTIMEZONE ones.
167 for (c=icalcomponent_get_first_component(
168 rcal, ICAL_ANY_COMPONENT);
170 c = icalcomponent_get_next_component(
171 rcal, ICAL_ANY_COMPONENT)
173 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
174 ical_dezonify_recur(cal, c);
179 * Now look for DTSTART and DTEND properties
181 for (p=icalcomponent_get_first_property(
182 rcal, ICAL_ANY_PROPERTY);
184 p = icalcomponent_get_next_property(
185 rcal, ICAL_ANY_PROPERTY)
188 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
189 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
190 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
191 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
193 ical_dezonify_backend(cal, rcal, p);
200 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
201 * This function will search any VTIMEZONE subcomponents to learn the
202 * relevant timezone information.
204 void ical_dezonify(icalcomponent *cal) {
205 icalcomponent *vt = NULL;
207 lprintf(CTDL_DEBUG, "ical_dezonify() started\n");
209 /* Convert all times to UTC */
210 ical_dezonify_recur(cal, cal);
212 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
213 while (vt = icalcomponent_get_first_component(
214 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
215 icalcomponent_remove_component(cal, vt);
216 icalcomponent_free(vt);
219 lprintf(CTDL_DEBUG, "ical_dezonify() completed\n");
223 #endif /* CITADEL_WITH_CALENDAR_SERVICE */