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 * Copyright (c) 1987-2015 by the citadel.org team
8 * This program is open source software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 3.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
21 #include <sys/types.h>
27 #include <libical/ical.h>
28 #include <libcitadel.h>
31 #include "citserver.h"
32 #include "sysdep_decls.h"
35 #include "ical_dezonify.h"
37 #include "ctdl_module.h"
41 * Figure out which time zone needs to be used for timestamps that are
42 * not UTC and do not have a time zone specified.
44 icaltimezone *get_default_icaltimezone(void) {
46 icaltimezone *zone = NULL;
47 char *default_zone_name = CtdlGetConfigStr("c_default_cal_zone");
50 zone = icaltimezone_get_builtin_timezone(default_zone_name);
54 "Unable to load '%s' time zone. Defaulting to UTC.\n",
56 zone = icaltimezone_get_utc_timezone();
59 syslog(LOG_ALERT, "Unable to load UTC time zone!\n");
66 * Back end function for ical_dezonify()
68 * We supply this with the master component, the relevant component,
69 * and the property (which will be a DTSTART, DTEND, etc.)
70 * which we want to convert to UTC.
72 void ical_dezonify_backend(icalcomponent *cal,
76 icaltimezone *t = NULL;
78 const char *tzid = NULL;
79 struct icaltimetype TheTime;
80 int utc_declared_as_tzid = 0; /**< Component declared 'TZID=GMT' instead of using Z syntax */
82 /* Give me nothing and I will give you nothing in return. */
83 if (cal == NULL) return;
85 /* Hunt for a TZID parameter in this property. */
86 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
88 /* Get the stringish name of this TZID. */
90 tzid = icalparameter_get_tzid(param);
92 /* Convert it to an icaltimezone type. */
94 /* syslog(LOG_DEBUG, " * Stringy supplied timezone is: '%s'\n", tzid); */
95 if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
96 utc_declared_as_tzid = 1;
97 /* syslog(LOG_DEBUG, " * ...and we handle that internally.\n"); */
100 /* try attached first */
101 t = icalcomponent_get_timezone(cal, tzid);
103 syslog(LOG_DEBUG, " * ...and I %s have tzdata for that zone.\n",
104 (t ? "DO" : "DO NOT")
107 /* then try built-in timezones */
109 t = icaltimezone_get_builtin_timezone(tzid);
112 syslog(LOG_DEBUG, " * Using system tzdata!\n");
121 /* Now we know the timezone. Convert to UTC. */
123 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
124 TheTime = icalproperty_get_dtstart(prop);
126 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
127 TheTime = icalproperty_get_dtend(prop);
129 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
130 TheTime = icalproperty_get_due(prop);
132 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
133 TheTime = icalproperty_get_exdate(prop);
139 /* syslog(LOG_DEBUG, " * Was: %s\n", icaltime_as_ical_string(TheTime)); */
141 if (TheTime.is_utc) {
142 /* syslog(LOG_DEBUG, " * This property is ALREADY UTC.\n"); */
145 else if (utc_declared_as_tzid) {
146 /* syslog(LOG_DEBUG, " * Replacing '%s' TZID with 'Z' suffix.\n", tzid); */
151 /* Do the conversion. */
153 /* syslog(LOG_DEBUG, " * Timezone prop found. Converting to UTC.\n"); */
156 /* syslog(LOG_DEBUG, " * Converting default timezone to UTC.\n"); */
160 t = get_default_icaltimezone();
163 icaltimezone_convert_time(&TheTime,
165 icaltimezone_get_utc_timezone()
170 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
171 /* syslog(LOG_DEBUG, " * Now: %s\n", icaltime_as_ical_string(TheTime)); */
173 /* Now add the converted property back in. */
174 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
175 icalproperty_set_dtstart(prop, TheTime);
177 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
178 icalproperty_set_dtend(prop, TheTime);
180 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
181 icalproperty_set_due(prop, TheTime);
183 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
184 icalproperty_set_exdate(prop, TheTime);
190 * Recursive portion of ical_dezonify()
192 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
197 * Recurse through all subcomponents *except* VTIMEZONE ones.
199 for (c=icalcomponent_get_first_component(
200 rcal, ICAL_ANY_COMPONENT);
202 c = icalcomponent_get_next_component(
203 rcal, ICAL_ANY_COMPONENT)
205 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
206 ical_dezonify_recurse(cal, c);
211 * Now look for DTSTART and DTEND properties
213 for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
215 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
218 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
219 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
220 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
221 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
223 ical_dezonify_backend(cal, rcal, p);
230 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
231 * This function will search any VTIMEZONE subcomponents to learn the
232 * relevant timezone information.
234 void ical_dezonify(icalcomponent *cal) {
235 icalcomponent *vt = NULL;
237 /* syslog(LOG_DEBUG, "ical_dezonify() started\n"); */
239 /* Convert all times to UTC */
240 ical_dezonify_recurse(cal, cal);
242 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
243 while (vt = icalcomponent_get_first_component(
244 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
245 icalcomponent_remove_component(cal, vt);
246 icalcomponent_free(vt);
249 /* syslog(LOG_DEBUG, "ical_dezonify() completed\n"); */