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 icaltimezone *get_default_icaltimezone(void) {
37 icaltimezone *zone = NULL;
38 char *default_zone_name = config.c_default_cal_zone;
39 //char *default_zone_name = "America/New_York";
42 zone = icaltimezone_get_builtin_timezone(default_zone_name);
46 "Unable to load '%s' time zone. Defaulting to UTC.\n",
48 zone = icaltimezone_get_utc_timezone();
51 lprintf(1, "Unable to load UTC time zone!\n");
58 * Back end function for ical_dezonify()
60 * We supply this with the master component, the relevant component,
61 * and the property (which will be a DTSTART, DTEND, etc.)
62 * which we want to convert to UTC.
64 void ical_dezonify_backend(icalcomponent *cal,
68 icaltimezone *t = NULL;
70 const char *tzid = NULL;
71 struct icaltimetype TheTime;
72 int utc_declared_as_tzid = 0; /**< Component declared 'TZID=GMT' instead of using Z syntax */
74 /* Give me nothing and I will give you nothing in return. */
75 if (cal == NULL) return;
77 /* Hunt for a TZID parameter in this property. */
78 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
80 /* Get the stringish name of this TZID. */
82 tzid = icalparameter_get_tzid(param);
84 /* Convert it to an icaltimezone type. */
86 /* lprintf(9, " * Stringy supplied timezone is: '%s'\n", tzid); */
87 if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
88 utc_declared_as_tzid = 1;
89 /* lprintf(9, " * ...and we handle that internally.\n"); */
92 t = icalcomponent_get_timezone(cal, tzid);
93 /* lprintf(9, " * ...and I %s have tzdata for that zone.\n",
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(9, " * Was: %s\n", icaltime_as_ical_string(TheTime)); */
121 if (TheTime.is_utc) {
122 /* lprintf(9, " * This property is ALREADY UTC.\n"); */
125 else if (utc_declared_as_tzid) {
126 /* lprintf(9, " * Replacing '%s' TZID with 'Z' suffix.\n", tzid); */
131 /* Do the conversion. */
133 /* lprintf(9, " * Timezone prop found. Converting to UTC.\n"); */
136 /* lprintf(9, " * Converting default timezone to UTC.\n"); */
140 t = get_default_icaltimezone();
143 icaltimezone_convert_time(&TheTime,
145 icaltimezone_get_utc_timezone()
150 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
151 /* lprintf(9, " * Now: %s\n", icaltime_as_ical_string(TheTime)); */
153 /* Now add the converted property back in. */
154 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
155 icalproperty_set_dtstart(prop, TheTime);
157 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
158 icalproperty_set_dtend(prop, TheTime);
160 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
161 icalproperty_set_due(prop, TheTime);
163 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
164 icalproperty_set_exdate(prop, TheTime);
170 * Recursive portion of ical_dezonify()
172 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
177 * Recurse through all subcomponents *except* VTIMEZONE ones.
179 for (c=icalcomponent_get_first_component(
180 rcal, ICAL_ANY_COMPONENT);
182 c = icalcomponent_get_next_component(
183 rcal, ICAL_ANY_COMPONENT)
185 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
186 ical_dezonify_recurse(cal, c);
191 * Now look for DTSTART and DTEND properties
193 for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
195 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
198 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
199 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
200 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
201 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
203 ical_dezonify_backend(cal, rcal, p);
210 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
211 * This function will search any VTIMEZONE subcomponents to learn the
212 * relevant timezone information.
214 void ical_dezonify(icalcomponent *cal) {
215 icalcomponent *vt = NULL;
217 /* lprintf(9, "ical_dezonify() started\n"); */
219 /* Convert all times to UTC */
220 ical_dezonify_recurse(cal, cal);
222 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
223 while (vt = icalcomponent_get_first_component(
224 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
225 icalcomponent_remove_component(cal, vt);
226 icalcomponent_free(vt);
229 /* lprintf(9, "ical_dezonify() completed\n"); */
232 #endif /* CITADEL_WITH_CALENDAR_SERVICE */