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>
19 #include <libical/ical.h>
20 #include <libcitadel.h>
23 #include "citserver.h"
24 #include "sysdep_decls.h"
27 #include "ical_dezonify.h"
29 #include "ctdl_module.h"
33 * Figure out which time zone needs to be used for timestamps that are
34 * not UTC and do not have a time zone specified.
36 icaltimezone *get_default_icaltimezone(void) {
38 icaltimezone *zone = NULL;
39 char *default_zone_name = config.c_default_cal_zone;
40 //char *default_zone_name = "America/New_York";
43 zone = icaltimezone_get_builtin_timezone(default_zone_name);
46 CtdlLogPrintf(CTDL_ALERT,
47 "Unable to load '%s' time zone. Defaulting to UTC.\n",
49 zone = icaltimezone_get_utc_timezone();
52 CtdlLogPrintf(1, "Unable to load UTC time zone!\n");
59 * Back end function for ical_dezonify()
61 * We supply this with the master component, the relevant component,
62 * and the property (which will be a DTSTART, DTEND, etc.)
63 * which we want to convert to UTC.
65 void ical_dezonify_backend(icalcomponent *cal,
69 icaltimezone *t = NULL;
71 const char *tzid = NULL;
72 struct icaltimetype TheTime;
73 int utc_declared_as_tzid = 0; /**< Component declared 'TZID=GMT' instead of using Z syntax */
75 /* Give me nothing and I will give you nothing in return. */
76 if (cal == NULL) return;
78 /* Hunt for a TZID parameter in this property. */
79 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
81 /* Get the stringish name of this TZID. */
83 tzid = icalparameter_get_tzid(param);
85 /* Convert it to an icaltimezone type. */
87 /* CtdlLogPrintf(9, " * Stringy supplied timezone is: '%s'\n", tzid); */
88 if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
89 utc_declared_as_tzid = 1;
90 /* CtdlLogPrintf(9, " * ...and we handle that internally.\n"); */
93 /* try attached first */
94 t = icalcomponent_get_timezone(cal, tzid);
96 lprintf(9, " * ...and I %s have tzdata for that zone.\n",
100 /* then try built-in timezones */
102 t = icaltimezone_get_builtin_timezone(tzid);
105 lprintf(9, " * Using system tzdata!\n");
114 /* Now we know the timezone. Convert to UTC. */
116 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
117 TheTime = icalproperty_get_dtstart(prop);
119 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
120 TheTime = icalproperty_get_dtend(prop);
122 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
123 TheTime = icalproperty_get_due(prop);
125 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
126 TheTime = icalproperty_get_exdate(prop);
132 /* CtdlLogPrintf(9, " * Was: %s\n", icaltime_as_ical_string(TheTime)); */
134 if (TheTime.is_utc) {
135 /* CtdlLogPrintf(9, " * This property is ALREADY UTC.\n"); */
138 else if (utc_declared_as_tzid) {
139 /* CtdlLogPrintf(9, " * Replacing '%s' TZID with 'Z' suffix.\n", tzid); */
144 /* Do the conversion. */
146 /* CtdlLogPrintf(9, " * Timezone prop found. Converting to UTC.\n"); */
149 /* CtdlLogPrintf(9, " * Converting default timezone to UTC.\n"); */
153 t = get_default_icaltimezone();
156 icaltimezone_convert_time(&TheTime,
158 icaltimezone_get_utc_timezone()
163 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
164 /* CtdlLogPrintf(9, " * Now: %s\n", icaltime_as_ical_string(TheTime)); */
166 /* Now add the converted property back in. */
167 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
168 icalproperty_set_dtstart(prop, TheTime);
170 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
171 icalproperty_set_dtend(prop, TheTime);
173 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
174 icalproperty_set_due(prop, TheTime);
176 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
177 icalproperty_set_exdate(prop, TheTime);
183 * Recursive portion of ical_dezonify()
185 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
190 * Recurse through all subcomponents *except* VTIMEZONE ones.
192 for (c=icalcomponent_get_first_component(
193 rcal, ICAL_ANY_COMPONENT);
195 c = icalcomponent_get_next_component(
196 rcal, ICAL_ANY_COMPONENT)
198 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
199 ical_dezonify_recurse(cal, c);
204 * Now look for DTSTART and DTEND properties
206 for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
208 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
211 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
212 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
213 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
214 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
216 ical_dezonify_backend(cal, rcal, p);
223 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
224 * This function will search any VTIMEZONE subcomponents to learn the
225 * relevant timezone information.
227 void ical_dezonify(icalcomponent *cal) {
228 icalcomponent *vt = NULL;
230 /* CtdlLogPrintf(9, "ical_dezonify() started\n"); */
232 /* Convert all times to UTC */
233 ical_dezonify_recurse(cal, cal);
235 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
236 while (vt = icalcomponent_get_first_component(
237 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
238 icalcomponent_remove_component(cal, vt);
239 icalcomponent_free(vt);
242 /* CtdlLogPrintf(9, "ical_dezonify() completed\n"); */