5 * \defgroup IcalDezonify normalize ical dates to UTC
6 * Function to go through an ical component set and convert all non-UTC
7 * date/time properties to UTC. It also strips out any VTIMEZONE
8 * subcomponents afterwards, because they're irrelevant.
10 * Everything here will work on both a fully encapsulated VCALENDAR component
11 * or any type of subcomponent.
13 * \ingroup Calendaring
18 #include "webserver.h"
21 #ifdef WEBCIT_WITH_CALENDAR_SERVICE
25 * Figure out which time zone needs to be used for timestamps that are
26 * not UTC and do not have a time zone specified.
28 * FIXME - most sites are not in New York :)
30 icaltimezone *get_default_icaltimezone(void) {
32 icaltimezone *zone = NULL;
35 zone = icaltimezone_get_builtin_timezone(serv_info.serv_default_cal_zone);
38 zone = icaltimezone_get_utc_timezone();
45 * Back end function for ical_dezonify()
47 * We supply this with the master component, the relevant component,
48 * and the property (which will be a DTSTART, DTEND, etc.)
49 * which we want to convert to UTC.
51 void ical_dezonify_backend(icalcomponent *cal,
55 icaltimezone *t = NULL;
58 struct icaltimetype TheTime;
60 /* Give me nothing and I will give you nothing in return. */
61 if (cal == NULL) return;
63 /* Hunt for a TZID parameter in this property. */
64 param = icalproperty_get_first_parameter(prop, ICAL_TZID_PARAMETER);
66 /* Get the stringish name of this TZID. */
68 tzid = icalparameter_get_tzid(param);
70 /* Convert it to an icaltimezone type. */
72 t = icalcomponent_get_timezone(cal, tzid);
77 /* Now we know the timezone. Convert to UTC. */
79 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
80 TheTime = icalproperty_get_dtstart(prop);
82 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
83 TheTime = icalproperty_get_dtend(prop);
85 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
86 TheTime = icalproperty_get_due(prop);
88 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
89 TheTime = icalproperty_get_exdate(prop);
95 /* lprintf(9, " * Was: %s\n", icaltime_as_ical_string(TheTime)); */
97 /* lprintf(9, " * This property is ALREADY UTC.\n"); */
100 /* Do the conversion. */
102 /* lprintf(9, " * Timezone prop found. Converting to UTC.\n"); */
105 /* lprintf(9, " * Converting default timezone to UTC.\n"); */
109 t = get_default_icaltimezone();
112 icaltimezone_convert_time(&TheTime,
114 icaltimezone_get_utc_timezone()
119 icalproperty_remove_parameter_by_kind(prop, ICAL_TZID_PARAMETER);
120 /* lprintf(9, " * Now: %s\n", icaltime_as_ical_string(TheTime)); */
122 /* Now add the converted property back in. */
123 if (icalproperty_isa(prop) == ICAL_DTSTART_PROPERTY) {
124 icalproperty_set_dtstart(prop, TheTime);
126 else if (icalproperty_isa(prop) == ICAL_DTEND_PROPERTY) {
127 icalproperty_set_dtend(prop, TheTime);
129 else if (icalproperty_isa(prop) == ICAL_DUE_PROPERTY) {
130 icalproperty_set_due(prop, TheTime);
132 else if (icalproperty_isa(prop) == ICAL_EXDATE_PROPERTY) {
133 icalproperty_set_exdate(prop, TheTime);
139 * Recursive portion of ical_dezonify()
141 void ical_dezonify_recurse(icalcomponent *cal, icalcomponent *rcal) {
146 * Recurse through all subcomponents *except* VTIMEZONE ones.
148 for (c=icalcomponent_get_first_component(
149 rcal, ICAL_ANY_COMPONENT);
151 c = icalcomponent_get_next_component(
152 rcal, ICAL_ANY_COMPONENT)
154 if (icalcomponent_isa(c) != ICAL_VTIMEZONE_COMPONENT) {
155 ical_dezonify_recurse(cal, c);
160 * Now look for DTSTART and DTEND properties
162 for (p=icalcomponent_get_first_property(
163 rcal, ICAL_ANY_PROPERTY);
165 p = icalcomponent_get_next_property(
166 rcal, ICAL_ANY_PROPERTY)
169 (icalproperty_isa(p) == ICAL_DTSTART_PROPERTY)
170 || (icalproperty_isa(p) == ICAL_DTEND_PROPERTY)
171 || (icalproperty_isa(p) == ICAL_DUE_PROPERTY)
172 || (icalproperty_isa(p) == ICAL_EXDATE_PROPERTY)
174 ical_dezonify_backend(cal, rcal, p);
181 * Convert all DTSTART and DTEND properties in all subcomponents to UTC.
182 * This function will search any VTIMEZONE subcomponents to learn the
183 * relevant timezone information.
185 void ical_dezonify(icalcomponent *cal) {
186 icalcomponent *vt = NULL;
188 /* lprintf(9, "ical_dezonify() started\n"); */
190 /* Convert all times to UTC */
191 ical_dezonify_recurse(cal, cal);
193 /* Strip out VTIMEZONE subcomponents -- we don't need them anymore */
194 while (vt = icalcomponent_get_first_component(
195 cal, ICAL_VTIMEZONE_COMPONENT), vt != NULL) {
196 icalcomponent_remove_component(cal, vt);
197 icalcomponent_free(vt);
200 /* lprintf(9, "ical_dezonify() completed\n"); */
204 #endif /* WEBCIT_WITH_CALENDAR_SERVICE */