3 * Function to go through an ical component set and convert all non-UTC
4 * date/time properties to UTC. It also strips out any VTIMEZONE
5 * subcomponents afterwards, because they're irrelevant.
7 * Everything here will work on both a fully encapsulated VCALENDAR component
8 * or any type of subcomponent.
12 #include "webserver.h"
15 * Figure out which time zone needs to be used for timestamps that are
16 * not UTC and do not have a time zone specified.
19 icaltimezone *get_default_icaltimezone(void) {
21 icaltimezone *zone = NULL;
22 const char *default_zone_name = ChrPtr(WC->serv_info->serv_default_cal_zone);
25 zone = icaltimezone_get_builtin_timezone(default_zone_name);
28 syslog(LOG_WARNING, "Unable to load '%s' time zone. Defaulting to UTC.\n", default_zone_name);
29 zone = icaltimezone_get_utc_timezone();
32 syslog(LOG_ERR, "Unable to load UTC time zone!\n");
39 * Back end function for ical_dezonify()
41 * We supply this with the master component, the relevant component,
42 * and the property (which will be a DTSTART, DTEND, etc.)
43 * which we want to convert to UTC.
45 void ical_dezonify_backend(icalcomponent * cal, icalcomponent * rcal, icalproperty * prop) {
47 icaltimezone *t = NULL;
49 const char *tzid = NULL;
50 struct icaltimetype TheTime;
51 int utc_declared_as_tzid = 0; /* Component declared 'TZID=GMT' instead of using Z syntax */
53 /* Give me nothing and I will give you nothing in 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 (icaltime_is_utc(TheTime)) {
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);
129 TheTime.zone = icaltimezone_get_utc_timezone();
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());
149 TheTime.zone = 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(rcal, ICAL_ANY_COMPONENT);
184 c != NULL; c = icalcomponent_get_next_component(rcal, ICAL_ANY_COMPONENT)
186 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
187 ical_dezonify_recurse(cal, c);
192 * Now look for DTSTART and DTEND properties
194 for (p = icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
195 p != NULL; p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
197 if ((icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
198 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
199 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
200 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
202 ical_dezonify_backend(cal, rcal, p);
209 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
210 * This function will search any VTIMEZONE subcomponents to learn the
211 * relevant timezone information.
213 void ical_dezonify(icalcomponent * cal) {
214 icalcomponent *vt = NULL;
217 syslog(LOG_DEBUG, "ical_dezonify() started\n");
220 /* Convert all times to UTC */
221 ical_dezonify_recurse(cal, cal);
223 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
224 while (vt = icalcomponent_get_first_component(cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
225 icalcomponent_remove_component(cal, vt);
226 icalcomponent_free(vt);
230 syslog(LOG_DEBUG, "ical_dezonify() completed\n");