4 * Function to go through an ical component set and convert all non-UTC
5 * DTSTART and DTEND properties to UTC. It also strips out any VTIMEZONE
6 * subcomponents afterwards, because they're irrelevant.
13 #include <sys/types.h>
20 #include "citserver.h"
21 #include "sysdep_decls.h"
27 #include "ical_dezonify.h"
30 * Back end function for ical_dezonify()
32 * We supply this with the master component, the relevant component,
33 * and the property (which will be a DTSTART of DTEND)
34 * which we want to convert to UTC.
36 void ical_dezonify_backend(icalcomponent *cal,
43 struct icaltimetype TheTime;
45 /* Give me nothing and I will give you nothing in return. */
46 if (cal == NULL) return;
48 /* Hunt for a TZID parameter in this property. */
49 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
50 if (param == NULL) return;
52 /* Get the stringish name of this TZID. */
53 tzid = icalparameter_get_tzid(param);
54 if (tzid == NULL) return;
56 /* Convert it to an icaltimezone type. */
57 t = icalcomponent_get_timezone(cal, tzid);
58 if (t == NULL) return;
60 /* Now we know the timezone. Convert to UTC. */
62 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
63 TheTime = icalproperty_get_dtstart(prop);
65 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
66 TheTime = icalproperty_get_dtend(prop);
72 /* Remove the property from the component. */
73 icalcomponent_remove_property(rcal, prop);
74 icalproperty_free(prop);
76 /* Do the conversion. */
77 icaltimezone_convert_time(&TheTime,
79 icaltimezone_get_utc_timezone()
82 /* Now add the converted property back in. */
83 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
84 prop = icalproperty_new_dtstart(TheTime);
85 icalcomponent_add_property(rcal, prop);
87 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
88 prop = icalproperty_new_dtend(TheTime);
89 icalcomponent_add_property(rcal, prop);
96 * Recursive portion of ical_dezonify()
98 void ical_dezonify_recur(icalcomponent *cal, icalcomponent *rcal) {
103 * Recurse through all subcomponents *except* VTIMEZONE ones.
105 for (c=icalcomponent_get_first_component(
106 rcal, ICAL_ANY_COMPONENT);
108 c = icalcomponent_get_next_component(
109 rcal, ICAL_ANY_COMPONENT)
111 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
112 ical_dezonify_recur(cal, c);
117 * Now look for DTSTART and DTEND properties
119 for (p=icalcomponent_get_first_property(
120 rcal, ICAL_ANY_PROPERTY);
122 p = icalcomponent_get_next_property(
123 rcal, ICAL_ANY_PROPERTY)
126 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
127 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
129 ical_dezonify_backend(cal, rcal, p);
136 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
137 * This function will search any VTIMEZONE subcomponents to learn the
138 * relevant timezone information.
140 void ical_dezonify(icalcomponent *cal) {
141 icalcomponent *vt = NULL;
143 /* Convert all times to UTC */
144 ical_dezonify_recur(cal, cal);
146 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
147 while (vt = icalcomponent_get_first_component(
148 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
149 icalcomponent_remove_component(cal, vt);
150 icalcomponent_free(vt);
153 lprintf(9, "dezonify:\n%s\n", icalcomponent_as_ical_string(cal));
156 #endif /* HAVE_ICAL_H */