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;
45 This doesn't even belong here.
46 I'm just keeping it here until I put it somewhere permanent.
50 zones = icaltimezone_get_builtin_timezones();
51 for (i = 0; i < zones->num_elements; i++) {
52 lprintf(CTDL_DEBUG, "%s\n", icaltimezone_get_location(icalarray_element_at(zones, i)));
57 location = "America/New_York";
59 zone = icaltimezone_get_builtin_timezone(location);
62 zone = icaltimezone_get_utc_timezone();
69 * Back end function for ical_dezonify()
71 * We supply this with the master component, the relevant component,
72 * and the property (which will be a DTSTART, DTEND, etc.)
73 * which we want to convert to UTC.
75 void ical_dezonify_backend(icalcomponent *cal,
79 icaltimezone *t = NULL;
82 struct icaltimetype TheTime;
84 /* Give me nothing and I will give you nothing in return. */
85 if (cal == NULL) return;
87 /* Hunt for a TZID parameter in this property. */
88 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
90 /* Get the stringish name of this TZID. */
92 tzid = icalparameter_get_tzid(param);
94 /* Convert it to an icaltimezone type. */
96 t = icalcomponent_get_timezone(cal, tzid);
101 /* Now we know the timezone. Convert to UTC. */
103 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
104 TheTime = icalproperty_get_dtstart(prop);
106 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
107 TheTime = icalproperty_get_dtend(prop);
109 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
110 TheTime = icalproperty_get_due(prop);
112 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
113 TheTime = icalproperty_get_exdate(prop);
119 lprintf(CTDL_DEBUG, " * Was: %s\n", icaltime_as_ical_string(TheTime));
120 if (TheTime.is_utc) {
121 lprintf(CTDL_DEBUG, " * This property is ALREADY UTC.\n");
124 /* Do the conversion. */
126 lprintf(CTDL_DEBUG, " * Timezone prop found. Converting to UTC.\n");
129 lprintf(CTDL_DEBUG, " * Converting default timezone to UTC.\n");
133 t = get_default_icaltimezone();
136 icaltimezone_convert_time(&TheTime,
138 icaltimezone_get_utc_timezone()
143 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
144 lprintf(CTDL_DEBUG, " * Now: %s\n", icaltime_as_ical_string(TheTime));
146 /* Now add the converted property back in. */
147 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
148 icalproperty_set_dtstart(prop, TheTime);
150 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
151 icalproperty_set_dtend(prop, TheTime);
153 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
154 icalproperty_set_due(prop, TheTime);
156 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
157 icalproperty_set_exdate(prop, TheTime);
163 * Recursive portion of ical_dezonify()
165 void ical_dezonify_recur(icalcomponent *cal, icalcomponent *rcal) {
170 * Recurse through all subcomponents *except* VTIMEZONE ones.
172 for (c=icalcomponent_get_first_component(
173 rcal, ICAL_ANY_COMPONENT);
175 c = icalcomponent_get_next_component(
176 rcal, ICAL_ANY_COMPONENT)
178 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
179 ical_dezonify_recur(cal, c);
184 * Now look for DTSTART and DTEND properties
186 for (p=icalcomponent_get_first_property(
187 rcal, ICAL_ANY_PROPERTY);
189 p = icalcomponent_get_next_property(
190 rcal, ICAL_ANY_PROPERTY)
193 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
194 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
195 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
196 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
198 ical_dezonify_backend(cal, rcal, p);
205 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
206 * This function will search any VTIMEZONE subcomponents to learn the
207 * relevant timezone information.
209 void ical_dezonify(icalcomponent *cal) {
210 icalcomponent *vt = NULL;
212 lprintf(CTDL_DEBUG, "ical_dezonify() started\n");
214 /* Convert all times to UTC */
215 ical_dezonify_recur(cal, cal);
217 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
218 while (vt = icalcomponent_get_first_component(
219 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
220 icalcomponent_remove_component(cal, vt);
221 icalcomponent_free(vt);
224 lprintf(CTDL_DEBUG, "ical_dezonify() completed\n");
228 #endif /* CITADEL_WITH_CALENDAR_SERVICE */