Esempio n. 1
0
status_t
BFormattingConventions::GetTimeFormat(BTimeFormatStyle style,
                                      BString& outFormat) const
{
    if (style < 0 || style >= B_TIME_FORMAT_STYLE_COUNT)
        return B_BAD_VALUE;

    outFormat = fExplicitTimeFormats[style].Length()
                ? fExplicitTimeFormats[style]
                : fCachedTimeFormats[style];

    if (outFormat.Length() > 0)
        return B_OK;

    ObjectDeleter<DateFormat> timeFormatter(
        DateFormat::createTimeInstance((DateFormat::EStyle)style, *fICULocale));
    if (timeFormatter.Get() == NULL)
        return B_NO_MEMORY;

    SimpleDateFormat* timeFormatterImpl
        = static_cast<SimpleDateFormat*>(timeFormatter.Get());

    UnicodeString icuString;
    timeFormatterImpl->toPattern(icuString);
    BStringByteSink stringConverter(&outFormat);
    icuString.toUTF8(stringConverter);

    int8 use24HourClock = fExplicitUse24HourClock != CLOCK_HOURS_UNSET
                          ?  fExplicitUse24HourClock : fCachedUse24HourClock;
    if (use24HourClock != CLOCK_HOURS_UNSET) {
        // adjust to 12/24-hour clock as requested
        bool localeUses24HourClock = !FormatUsesAmPm(outFormat);
        if (localeUses24HourClock) {
            if (use24HourClock == CLOCK_HOURS_12)
                CoerceFormatTo12HourClock(outFormat);
        } else {
            if (use24HourClock == CLOCK_HOURS_24)
                CoerceFormatTo24HourClock(outFormat);
        }
    }

    // use abbreviated timezone in short timezone format
    CoerceFormatToAbbreviatedTimezone(outFormat);

    fCachedTimeFormats[style] = outFormat;

    return B_OK;
}
Esempio n. 2
0
status_t
BFormattingConventions::GetDateTimeFormat(BDateFormatStyle dateStyle,
	BTimeFormatStyle timeStyle, BString& outFormat) const
{
	if (dateStyle < 0 || dateStyle >= B_DATE_FORMAT_STYLE_COUNT)
		return B_BAD_VALUE;

	if (timeStyle < 0 || timeStyle >= B_TIME_FORMAT_STYLE_COUNT)
		return B_BAD_VALUE;

	outFormat = fExplicitDateTimeFormats[dateStyle][timeStyle].Length()
		? fExplicitDateTimeFormats[dateStyle][timeStyle]
		: fCachedDateTimeFormats[dateStyle][timeStyle];

	if (outFormat.Length() > 0)
		return B_OK;

	ObjectDeleter<DateFormat> dateFormatter(
		DateFormat::createDateTimeInstance((DateFormat::EStyle)dateStyle,
			(DateFormat::EStyle)timeStyle, *fICULocale));
	if (dateFormatter.Get() == NULL)
		return B_NO_MEMORY;

	SimpleDateFormat* dateFormatterImpl
		= static_cast<SimpleDateFormat*>(dateFormatter.Get());

	UnicodeString icuString;
	dateFormatterImpl->toPattern(icuString);
	BStringByteSink stringConverter(&outFormat);
	icuString.toUTF8(stringConverter);

	CoerceFormatForClock(outFormat);

	if (dateStyle != B_FULL_DATE_FORMAT) {
		// use abbreviated timezone in short timezone format
		CoerceFormatToAbbreviatedTimezone(outFormat);
	}

	fCachedDateTimeFormats[dateStyle][timeStyle] = outFormat;

	return B_OK;
}