Example #1
0
/** 
 * call-seq:
 *     calendar.time_zone(locale = nil) 
 *
 * Returns the TimeZone name used in this UCalendar. Name is returned in requested locale or default, if not set.
 */
VALUE icu4r_cal_get_tz (int argc, VALUE * argv, VALUE obj)
{
	UErrorCode  status = U_ZERO_ERROR;
	UChar * buf  = NULL;
	long capa = 0;
	char *locale = NULL;
	VALUE loc;
	if( rb_scan_args(argc, argv, "01", &loc) == 1){
		Check_Type(loc, T_STRING);
		locale = RSTRING_PTR(loc);
	}
	
	capa = ucal_getTimeZoneDisplayName(UCALENDAR(obj), UCAL_STANDARD, locale, buf, capa, &status);
	if( U_BUFFER_OVERFLOW_ERROR == status) {
		buf = ALLOC_N(UChar, capa+1);
		status = U_ZERO_ERROR;
		capa = ucal_getTimeZoneDisplayName(UCALENDAR(obj), UCAL_STANDARD, locale, buf, capa, &status);
		return icu_ustr_new_set(buf, capa, capa+1);
	}
	ICU_RAISE(status);
	return Qnil;

}
// Qt wrapper around ucal_getTimeZoneDisplayName()
static QString ucalTimeZoneDisplayName(UCalendar *ucal, QTimeZone::TimeType timeType,
                                       QTimeZone::NameType nameType,
                                       const QString &localeCode)
{
    int32_t size = 50;
    QString result(size, Qt::Uninitialized);
    UErrorCode status = U_ZERO_ERROR;

    // size = ucal_getTimeZoneDisplayName(cal, type, locale, result, resultLength, status)
    size = ucal_getTimeZoneDisplayName(ucal,
                                       ucalDisplayNameType(timeType, nameType),
                                       localeCode.toUtf8(),
                                       reinterpret_cast<UChar *>(result.data()),
                                       size,
                                       &status);

    // If overflow, then resize and retry
    if (status == U_BUFFER_OVERFLOW_ERROR) {
        result.resize(size);
        status = U_ZERO_ERROR;
        size = ucal_getTimeZoneDisplayName(ucal,
                                           ucalDisplayNameType(timeType, nameType),
                                           localeCode.toUtf8(),
                                           reinterpret_cast<UChar *>(result.data()),
                                           size,
                                           &status);
    }

    // If successful on first or second go, resize and return
    if (U_SUCCESS(status)) {
        result.resize(size);
        return result;
    }

    return QString();
}