/**
 * e_cal_match_tzid:
 * matches a TZID against the system timezone definitions
 * and returns the matching TZID, or NULL if none found
 */
const char *e_cal_match_tzid(const char *tzid)
{
    const char *location;
    const char *systzid;
    size_t len = strlen(tzid);
    ssize_t eostr;

    /*
     * Try without any trailing spaces/digits: they might have been added
     * by e_cal_check_timezones() in order to distinguish between
     * different incompatible definitions. At that time mapping
     * to system time zones must have failed, but perhaps now
     * we have better code and it succeeds...
     */
    eostr = len - 1;
    while (eostr >= 0 &&
           isdigit(tzid[eostr])) {
        eostr--;
    }
    while (eostr >= 0 &&
           isspace(tzid[eostr])) {
        eostr--;
    }
    if (eostr + 1 < len) {
        char *strippedtzid = g_strndup(tzid, eostr + 1);
        if (strippedtzid) {
            systzid = e_cal_match_tzid(strippedtzid);
            g_free(strippedtzid);
            if (systzid) {
                return systzid;
            }
        }
    }

    /*
     * old-style Evolution: /softwarestudio.org/Olson_20011030_5/America/Denver
     *
     * jump from one slash to the next and check whether the remainder
     * is a known location; start with the whole string (just in case)
     */
    for (location = tzid;
         location && location[0];
         location = strchr(location + 1, '/')) {
        systzid = e_cal_match_location(location[0] == '/' ?
                                       location + 1 :
                                       location);
        if (systzid) {
            return systzid;
        }
    }

    /* TODO: lookup table for Exchange TZIDs */

    return NULL;
}
/**
 * e_cal_match_tzid:
 * @tzid: a timezone ID
 *
 * Matches @tzid against the system timezone definitions
 * and returns the matching TZID, or %NULL if none found
 *
 * Since: 2.24
 */
const gchar *
e_cal_match_tzid (const gchar *tzid)
{
	const gchar *location;
	const gchar *systzid = NULL;
	gsize len = strlen (tzid);
	gssize eostr;

	/*
	 * Try without any trailing spaces/digits: they might have been added
	 * by e_cal_check_timezones() in order to distinguish between
	 * different incompatible definitions. At that time mapping
	 * to system time zones must have failed, but perhaps now
	 * we have better code and it succeeds...
	 */
	eostr = len - 1;
	while (eostr >= 0 && isdigit (tzid[eostr])) {
		eostr--;
	}
	while (eostr >= 0 && isspace (tzid[eostr])) {
		eostr--;
	}
	if (eostr + 1 < len) {
		gchar *strippedtzid = g_strndup (tzid, eostr + 1);
		if (strippedtzid) {
			systzid = e_cal_match_tzid (strippedtzid);
			g_free (strippedtzid);
			if (systzid) {
				goto done;
			}
		}
	}

	/*
	 * old-style Evolution: /softwarestudio.org/Olson_20011030_5/America/Denver
	 *
	 * jump from one slash to the next and check whether the remainder
	 * is a known location; start with the whole string (just in case)
	 */
	for (location = tzid;
		 location && location[0];
		 location = strchr (location + 1, '/')) {
		systzid = e_cal_match_location (
			location[0] == '/' ?
			location + 1 : location);
		if (systzid) {
			goto done;
		}
	}

	/* TODO: lookup table for Exchange TZIDs */

 done:
	if (systzid && !strcmp (systzid, "UTC")) {
		/*
		 * UTC is special: it doesn't have a real VTIMEZONE in
		 * EDS. Matching some pseudo VTTIMEZONE with UTC in the TZID
		 * to our internal UTC "timezone" breaks
		 * e_cal_check_timezones() (it patches the event to use
		 * TZID=UTC, which cannot be exported correctly later on) and
		 * e_cal_get_timezone() (triggers an assert).
		 *
		 * So better avoid matching against it...
		 */
		return NULL;
	} else {
		return systzid;
	}
}