2 * Function to go through an ical component set and convert all non-UTC
3 * date/time properties to UTC. It also strips out any VTIMEZONE
4 * subcomponents afterwards, because they're irrelevant.
6 * Everything here will work on both a fully encapsulated VCALENDAR component
7 * or any type of subcomponent.
11 #include "webserver.h"
14 * Figure out which time zone needs to be used for timestamps that are
15 * not UTC and do not have a time zone specified.
18 icaltimezone *get_default_icaltimezone(void) {
20 icaltimezone *zone = NULL;
21 const char *default_zone_name = ChrPtr(WC->serv_info->serv_default_cal_zone);
24 zone = icaltimezone_get_builtin_timezone(default_zone_name);
27 syslog(LOG_WARNING, "Unable to load '%s' time zone. Defaulting to UTC.\n", default_zone_name);
28 zone = icaltimezone_get_utc_timezone();
31 syslog(LOG_ERR, "Unable to load UTC time zone!\n");
38 * Back end function for ical_dezonify()
40 * We supply this with the master component, the relevant component,
41 * and the property (which will be a DTSTART, DTEND, etc.)
42 * which we want to convert to UTC.
44 void ical_dezonify_backend(icalcomponent *cal,
48 icaltimezone *t = NULL;
50 const char *tzid = NULL;
51 struct icaltimetype TheTime;
52 int utc_declared_as_tzid = 0; /* Component declared 'TZID=GMT' instead of using Z syntax */
54 /* Give me nothing and I will give you nothing in return. */
55 if (cal == NULL) return;
57 /* Hunt for a TZID parameter in this property. */
58 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
60 /* Get the stringish name of this TZID. */
62 tzid = icalparameter_get_tzid(param);
64 /* Convert it to an icaltimezone type. */
67 syslog(LOG_DEBUG, " * Stringy supplied timezone is: '%s'\n", tzid);
69 if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
70 utc_declared_as_tzid = 1;
72 syslog(LOG_DEBUG, " * ...and we handle that internally.\n");
76 /* try attached first */
77 t = icalcomponent_get_timezone(cal, tzid);
79 syslog(LOG_DEBUG, " * ...and I %s have tzdata for that zone.\n",
83 /* then try built-in timezones */
85 t = icaltimezone_get_builtin_timezone(tzid);
88 syslog(LOG_DEBUG, " * Using system tzdata!\n");
97 /* Now we know the timezone. Convert to UTC. */
99 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
100 TheTime = icalproperty_get_dtstart(prop);
102 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
103 TheTime = icalproperty_get_dtend(prop);
105 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
106 TheTime = icalproperty_get_due(prop);
108 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
109 TheTime = icalproperty_get_exdate(prop);
116 syslog(LOG_DEBUG, " * Was: %s\n", icaltime_as_ical_string(TheTime));
119 if (TheTime.is_utc) {
121 syslog(LOG_DEBUG, " * This property is ALREADY UTC.\n");
125 else if (utc_declared_as_tzid) {
127 syslog(LOG_DEBUG, " * Replacing '%s' TZID with 'Z' suffix.\n", tzid);
133 /* Do the conversion. */
136 syslog(LOG_DEBUG, " * Timezone prop found. Converting to UTC.\n");
141 syslog(LOG_DEBUG, " * Converting default timezone to UTC.\n");
146 t = get_default_icaltimezone();
148 icaltimezone_convert_time(&TheTime, t, icaltimezone_get_utc_timezone());
152 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
154 syslog(LOG_DEBUG, " * Now: %s\n", icaltime_as_ical_string(TheTime));
157 /* Now add the converted property back in. */
158 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
159 icalproperty_set_dtstart(prop, TheTime);
161 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
162 icalproperty_set_dtend(prop, TheTime);
164 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
165 icalproperty_set_due(prop, TheTime);
167 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
168 icalproperty_set_exdate(prop, TheTime);
174 * Recursive portion of ical_dezonify()
176 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
181 * Recurse through all subcomponents *except* VTIMEZONE ones.
183 for (c=icalcomponent_get_first_component(
184 rcal, ICAL_ANY_COMPONENT);
186 c = icalcomponent_get_next_component(
187 rcal, ICAL_ANY_COMPONENT)
189 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
190 ical_dezonify_recurse(cal, c);
195 * Now look for DTSTART and DTEND properties
197 for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
199 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
202 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
203 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
204 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
205 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
207 ical_dezonify_backend(cal, rcal, p);
214 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
215 * This function will search any VTIMEZONE subcomponents to learn the
216 * relevant timezone information.
218 void ical_dezonify(icalcomponent *cal) {
219 icalcomponent *vt = NULL;
222 syslog(LOG_DEBUG, "ical_dezonify() started\n");
225 /* Convert all times to UTC */
226 ical_dezonify_recurse(cal, cal);
228 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
229 while (vt = icalcomponent_get_first_component(
230 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
231 icalcomponent_remove_component(cal, vt);
232 icalcomponent_free(vt);
236 syslog(LOG_DEBUG, "ical_dezonify() completed\n");