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.
8 * Everything here will work on both a fully encapsulated VCALENDAR component
9 * or any type of subcomponent.
14 #include "webserver.h"
18 * Figure out which time zone needs to be used for timestamps that are
19 * not UTC and do not have a time zone specified.
22 icaltimezone *get_default_icaltimezone(void) {
24 icaltimezone *zone = NULL;
25 char *default_zone_name = serv_info.serv_default_cal_zone;
28 zone = icaltimezone_get_builtin_timezone(default_zone_name);
31 lprintf(1, "Unable to load '%s' time zone. Defaulting to UTC.\n", default_zone_name);
32 zone = icaltimezone_get_utc_timezone();
35 lprintf(1, "Unable to load UTC time zone!\n");
42 * Back end function for ical_dezonify()
44 * We supply this with the master component, the relevant component,
45 * and the property (which will be a DTSTART, DTEND, etc.)
46 * which we want to convert to UTC.
48 void ical_dezonify_backend(icalcomponent *cal,
52 icaltimezone *t = NULL;
54 const char *tzid = NULL;
55 struct icaltimetype TheTime;
56 int utc_declared_as_tzid = 0; /* Component declared 'TZID=GMT' instead of using Z syntax */
58 /* Give me nothing and I will give you nothing in return. */
59 if (cal == NULL) return;
61 /* Hunt for a TZID parameter in this property. */
62 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
64 /* Get the stringish name of this TZID. */
66 tzid = icalparameter_get_tzid(param);
68 /* Convert it to an icaltimezone type. */
70 lprintf(9, " * Stringy supplied timezone is: '%s'\n", tzid);
71 if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
72 utc_declared_as_tzid = 1;
73 lprintf(9, " * ...and we handle that internally.\n");
76 t = icalcomponent_get_timezone(cal, tzid);
77 lprintf(9, " * ...and I %s have tzdata for that zone.\n",
85 /* Now we know the timezone. Convert to UTC. */
87 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
88 TheTime = icalproperty_get_dtstart(prop);
90 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
91 TheTime = icalproperty_get_dtend(prop);
93 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
94 TheTime = icalproperty_get_due(prop);
96 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
97 TheTime = icalproperty_get_exdate(prop);
103 lprintf(9, " * Was: %s\n", icaltime_as_ical_string(TheTime));
105 if (TheTime.is_utc) {
106 lprintf(9, " * This property is ALREADY UTC.\n");
109 else if (utc_declared_as_tzid) {
110 lprintf(9, " * Replacing '%s' TZID with 'Z' suffix.\n", tzid);
115 /* Do the conversion. */
117 lprintf(9, " * Timezone prop found. Converting to UTC.\n");
120 lprintf(9, " * Converting default timezone to UTC.\n");
124 t = get_default_icaltimezone();
126 icaltimezone_convert_time(&TheTime, t, icaltimezone_get_utc_timezone());
130 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
131 lprintf(9, " * Now: %s\n", icaltime_as_ical_string(TheTime));
133 /* Now add the converted property back in. */
134 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
135 icalproperty_set_dtstart(prop, TheTime);
137 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
138 icalproperty_set_dtend(prop, TheTime);
140 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
141 icalproperty_set_due(prop, TheTime);
143 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
144 icalproperty_set_exdate(prop, TheTime);
150 * Recursive portion of ical_dezonify()
152 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
157 * Recurse through all subcomponents *except* VTIMEZONE ones.
159 for (c=icalcomponent_get_first_component(
160 rcal, ICAL_ANY_COMPONENT);
162 c = icalcomponent_get_next_component(
163 rcal, ICAL_ANY_COMPONENT)
165 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
166 ical_dezonify_recurse(cal, c);
171 * Now look for DTSTART and DTEND properties
173 for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
175 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
178 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
179 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
180 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
181 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
183 ical_dezonify_backend(cal, rcal, p);
190 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
191 * This function will search any VTIMEZONE subcomponents to learn the
192 * relevant timezone information.
194 void ical_dezonify(icalcomponent *cal) {
195 icalcomponent *vt = NULL;
197 lprintf(9, "ical_dezonify() started\n");
199 /* Convert all times to UTC */
200 ical_dezonify_recurse(cal, cal);
202 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
203 while (vt = icalcomponent_get_first_component(
204 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
205 icalcomponent_remove_component(cal, vt);
206 icalcomponent_free(vt);
209 lprintf(9, "ical_dezonify() completed\n");