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>
20 #include <libcitadel.h>
23 #include "citserver.h"
24 #include "sysdep_decls.h"
27 #include "ical_dezonify.h"
31 * Figure out which time zone needs to be used for timestamps that are
32 * not UTC and do not have a time zone specified.
34 icaltimezone *get_default_icaltimezone(void) {
36 icaltimezone *zone = NULL;
37 char *default_zone_name = config.c_default_cal_zone;
38 //char *default_zone_name = "America/New_York";
41 zone = icaltimezone_get_builtin_timezone(default_zone_name);
44 CtdlLogPrintf(CTDL_ALERT,
45 "Unable to load '%s' time zone. Defaulting to UTC.\n",
47 zone = icaltimezone_get_utc_timezone();
50 CtdlLogPrintf(1, "Unable to load UTC time zone!\n");
57 * Back end function for ical_dezonify()
59 * We supply this with the master component, the relevant component,
60 * and the property (which will be a DTSTART, DTEND, etc.)
61 * which we want to convert to UTC.
63 void ical_dezonify_backend(icalcomponent *cal,
67 icaltimezone *t = NULL;
69 const char *tzid = NULL;
70 struct icaltimetype TheTime;
71 int utc_declared_as_tzid = 0; /**< Component declared 'TZID=GMT' instead of using Z syntax */
73 /* Give me nothing and I will give you nothing in return. */
74 if (cal == NULL) return;
76 /* Hunt for a TZID parameter in this property. */
77 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
79 /* Get the stringish name of this TZID. */
81 tzid = icalparameter_get_tzid(param);
83 /* Convert it to an icaltimezone type. */
85 /* CtdlLogPrintf(9, " * Stringy supplied timezone is: '%s'\n", tzid); */
86 if ( (!strcasecmp(tzid, "UTC")) || (!strcasecmp(tzid, "GMT")) ) {
87 utc_declared_as_tzid = 1;
88 /* CtdlLogPrintf(9, " * ...and we handle that internally.\n"); */
91 t = icalcomponent_get_timezone(cal, tzid);
92 /* CtdlLogPrintf(9, " * ...and I %s have tzdata for that zone.\n",
100 /* Now we know the timezone. Convert to UTC. */
102 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
103 TheTime = icalproperty_get_dtstart(prop);
105 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
106 TheTime = icalproperty_get_dtend(prop);
108 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
109 TheTime = icalproperty_get_due(prop);
111 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
112 TheTime = icalproperty_get_exdate(prop);
118 /* CtdlLogPrintf(9, " * Was: %s\n", icaltime_as_ical_string(TheTime)); */
120 if (TheTime.is_utc) {
121 /* CtdlLogPrintf(9, " * This property is ALREADY UTC.\n"); */
124 else if (utc_declared_as_tzid) {
125 /* CtdlLogPrintf(9, " * Replacing '%s' TZID with 'Z' suffix.\n", tzid); */
130 /* Do the conversion. */
132 /* CtdlLogPrintf(9, " * Timezone prop found. Converting to UTC.\n"); */
135 /* CtdlLogPrintf(9, " * Converting default timezone to UTC.\n"); */
139 t = get_default_icaltimezone();
142 icaltimezone_convert_time(&TheTime,
144 icaltimezone_get_utc_timezone()
149 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
150 /* CtdlLogPrintf(9, " * Now: %s\n", icaltime_as_ical_string(TheTime)); */
152 /* Now add the converted property back in. */
153 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
154 icalproperty_set_dtstart(prop, TheTime);
156 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
157 icalproperty_set_dtend(prop, TheTime);
159 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
160 icalproperty_set_due(prop, TheTime);
162 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
163 icalproperty_set_exdate(prop, TheTime);
169 * Recursive portion of ical_dezonify()
171 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
176 * Recurse through all subcomponents *except* VTIMEZONE ones.
178 for (c=icalcomponent_get_first_component(
179 rcal, ICAL_ANY_COMPONENT);
181 c = icalcomponent_get_next_component(
182 rcal, ICAL_ANY_COMPONENT)
184 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
185 ical_dezonify_recurse(cal, c);
190 * Now look for DTSTART and DTEND properties
192 for (p=icalcomponent_get_first_property(rcal, ICAL_ANY_PROPERTY);
194 p = icalcomponent_get_next_property(rcal, ICAL_ANY_PROPERTY)
197 (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;
216 /* CtdlLogPrintf(9, "ical_dezonify() started\n"); */
218 /* Convert all times to UTC */
219 ical_dezonify_recurse(cal, cal);
221 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
222 while (vt = icalcomponent_get_first_component(
223 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
224 icalcomponent_remove_component(cal, vt);
225 icalcomponent_free(vt);
228 /* CtdlLogPrintf(9, "ical_dezonify() completed\n"); */