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"
30 icaltimezone *get_default_icaltimezone(void) {
32 char *location = NULL;
33 icaltimezone *zone = NULL;
35 location = "America/New_York";
37 zone = icaltimezone_get_builtin_timezone (location);
40 zone = icaltimezone_get_utc_timezone ();
47 * Back end function for ical_dezonify()
49 * We supply this with the master component, the relevant component,
50 * and the property (which will be a DTSTART, DTEND, etc.)
51 * which we want to convert to UTC.
53 void ical_dezonify_backend(icalcomponent *cal,
57 icaltimezone *t = NULL;
60 struct icaltimetype TheTime;
62 /* Give me nothing and I will give you nothing in return. */
63 if (cal == NULL) return;
65 /* Hunt for a TZID parameter in this property. */
66 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
68 /* Get the stringish name of this TZID. */
70 tzid = icalparameter_get_tzid(param);
72 /* Convert it to an icaltimezone type. */
74 t = icalcomponent_get_timezone(cal, tzid);
79 /* Now we know the timezone. Convert to UTC. */
81 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
82 TheTime = icalproperty_get_dtstart(prop);
84 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
85 TheTime = icalproperty_get_dtend(prop);
87 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
88 TheTime = icalproperty_get_due(prop);
90 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
91 TheTime = icalproperty_get_exdate(prop);
97 lprintf(CTDL_DEBUG, " * Was: %s\n", icaltime_as_ical_string(TheTime));
99 lprintf(CTDL_DEBUG, " * This property is ALREADY UTC.\n");
102 /* Do the conversion. */
104 lprintf(CTDL_DEBUG, " * Timezone prop found. Converting to UTC.\n");
107 lprintf(CTDL_DEBUG, " * Converting default timezone to UTC.\n");
111 t = get_default_icaltimezone();
114 icaltimezone_convert_time(&TheTime,
116 icaltimezone_get_utc_timezone()
121 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
122 lprintf(CTDL_DEBUG, " * Now: %s\n", icaltime_as_ical_string(TheTime));
124 /* Now add the converted property back in. */
125 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
126 icalproperty_set_dtstart(prop, TheTime);
128 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
129 icalproperty_set_dtend(prop, TheTime);
131 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
132 icalproperty_set_due(prop, TheTime);
134 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
135 icalproperty_set_exdate(prop, TheTime);
141 * Recursive portion of ical_dezonify()
143 void ical_dezonify_recur(icalcomponent *cal, icalcomponent *rcal) {
148 * Recurse through all subcomponents *except* VTIMEZONE ones.
150 for (c=icalcomponent_get_first_component(
151 rcal, ICAL_ANY_COMPONENT);
153 c = icalcomponent_get_next_component(
154 rcal, ICAL_ANY_COMPONENT)
156 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
157 ical_dezonify_recur(cal, c);
162 * Now look for DTSTART and DTEND properties
164 for (p=icalcomponent_get_first_property(
165 rcal, ICAL_ANY_PROPERTY);
167 p = icalcomponent_get_next_property(
168 rcal, ICAL_ANY_PROPERTY)
171 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
172 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
173 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
174 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
176 ical_dezonify_backend(cal, rcal, p);
183 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
184 * This function will search any VTIMEZONE subcomponents to learn the
185 * relevant timezone information.
187 void ical_dezonify(icalcomponent *cal) {
188 icalcomponent *vt = NULL;
190 lprintf(CTDL_DEBUG, "ical_dezonify() started\n");
192 /* Convert all times to UTC */
193 ical_dezonify_recur(cal, cal);
195 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
196 while (vt = icalcomponent_get_first_component(
197 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
198 icalcomponent_remove_component(cal, vt);
199 icalcomponent_free(vt);
202 lprintf(CTDL_DEBUG, "ical_dezonify() completed\n");
206 #endif /* CITADEL_WITH_CALENDAR_SERVICE */