ICU-8237 Add RelativeDateFormat::getDateFormatSymbols, use in udat_getSymbols/countSymbols

X-SVN-Rev: 29964
This commit is contained in:
Peter Edberg 2011-05-02 23:14:32 +00:00
parent 8f19d6e019
commit 2fdfe20655
4 changed files with 81 additions and 53 deletions
icu4c/source

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2007-2010, International Business Machines Corporation and
* Copyright (C) 2007-2011, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*/
@ -339,6 +339,16 @@ RelativeDateFormat::applyPatterns(const UnicodeString& datePattern, const Unicod
}
}
const DateFormatSymbols*
RelativeDateFormat::getDateFormatSymbols() const
{
SimpleDateFormat* sdtfmt = NULL;
if (fDateFormat && (sdtfmt = dynamic_cast<SimpleDateFormat*>(fDateFormat)) != NULL) {
return sdtfmt->getDateFormatSymbols();
}
return NULL;
}
void RelativeDateFormat::loadDates(UErrorCode &status) {
CalendarData calData(fLocale, "gregorian", status);
@ -353,25 +363,25 @@ void RelativeDateFormat::loadDates(UErrorCode &status) {
if (patternsSize >= (DateFormat::kDateTimeOffset + DateFormat::kShort + 1)) {
// Get proper date time format
switch (fDateStyle) {
case kFullRelative:
case kFull:
glueIndex = kDateTimeOffset + kFull;
break;
case kLongRelative:
case kLong:
glueIndex = kDateTimeOffset + kLong;
break;
case kMediumRelative:
case kMedium:
glueIndex = kDateTimeOffset + kMedium;
break;
case kShortRelative:
case kShort:
glueIndex = kDateTimeOffset + kShort;
break;
default:
break;
}
case kFullRelative:
case kFull:
glueIndex = kDateTimeOffset + kFull;
break;
case kLongRelative:
case kLong:
glueIndex = kDateTimeOffset + kLong;
break;
case kMediumRelative:
case kMedium:
glueIndex = kDateTimeOffset + kMedium;
break;
case kShortRelative:
case kShort:
glueIndex = kDateTimeOffset + kShort;
break;
default:
break;
}
}
const UChar *resStr = ures_getStringByIndex(dateTimePatterns, glueIndex, &resStrLen, &tempStatus);

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 2007-2010, International Business Machines Corporation and *
* Copyright (C) 2007-2011, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*/
@ -22,6 +22,7 @@
U_NAMESPACE_BEGIN
// forward declarations
class DateFormatSymbols;
class MessageFormat;
// internal structure used for caching strings
@ -34,7 +35,7 @@ struct URelativeString;
* Example:
* DateFormat *fullrelative = DateFormat::createDateInstance(DateFormat::kFullRelative, loc);
*
* @draft ICU 3.8
* @internal ICU 3.8
*/
class RelativeDateFormat : public DateFormat {
@ -44,19 +45,19 @@ public:
// overrides
/**
* Copy constructor.
* @draft ICU 3.8
* @internal ICU 3.8
*/
RelativeDateFormat(const RelativeDateFormat&);
/**
* Assignment operator.
* @draft ICU 3.8
* @internal ICU 3.8
*/
RelativeDateFormat& operator=(const RelativeDateFormat&);
/**
* Destructor.
* @draft ICU 3.8
* @internal ICU 3.8
*/
virtual ~RelativeDateFormat();
@ -64,7 +65,7 @@ public:
* Clone this Format object polymorphically. The caller owns the result and
* should delete it when done.
* @return A copy of the object.
* @draft ICU 3.8
* @internal ICU 3.8
*/
virtual Format* clone(void) const;
@ -73,7 +74,7 @@ public:
* of different subclasses are considered unequal.
* @param other the object to be compared with.
* @return true if the given Format objects are semantically equal.
* @draft ICU 3.8
* @internal ICU 3.8
*/
virtual UBool operator==(const Format& other) const;
@ -94,7 +95,7 @@ public:
* @param pos The formatting position. On input: an alignment field,
* if desired. On output: the offsets of the alignment field.
* @return Reference to 'appendTo' parameter.
* @draft ICU 3.8
* @internal ICU 3.8
*/
virtual UnicodeString& format( Calendar& cal,
UnicodeString& appendTo,
@ -112,7 +113,7 @@ public:
* On output: the offsets of the alignment field.
* @param status Output param filled with success/failure status.
* @return Reference to 'appendTo' parameter.
* @draft ICU 3.8
* @internal ICU 3.8
*/
virtual UnicodeString& format(const Formattable& obj,
UnicodeString& appendTo,
@ -137,7 +138,7 @@ public:
* output, the position at which parsing terminated, or the
* start position if the parse failed.
* @return A valid UDate if the input could be parsed.
* @draft ICU 3.8
* @internal ICU 3.8
*/
virtual void parse( const UnicodeString& text,
Calendar& cal,
@ -160,7 +161,7 @@ public:
* output, the position at which parsing terminated, or the
* start position if the parse failed.
* @return A valid UDate if the input could be parsed.
* @draft ICU 3.8
* @internal ICU 3.8
*/
UDate parse( const UnicodeString& text,
ParsePosition& pos) const;
@ -179,7 +180,7 @@ public:
* @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
* an error value if there was a parse error.
* @return A valid UDate if the input could be parsed.
* @draft ICU 3.8
* @internal ICU 3.8
*/
virtual UDate parse( const UnicodeString& text,
UErrorCode& status) const;
@ -220,6 +221,16 @@ public:
*/
virtual void applyPatterns(const UnicodeString& datePattern, const UnicodeString& timePattern, UErrorCode &status);
/**
* Gets the date/time formatting symbols (this is an object carrying
* the various strings and other symbols used in formatting: e.g., month
* names and abbreviations, time zone names, AM/PM strings, etc.)
* @return a copy of the date-time formatting data associated
* with this date-time formatter.
* @internal ICU 4.8
*/
virtual const DateFormatSymbols* getDateFormatSymbols(void) const;
private:
DateFormat *fDateFormat; // the held date format
@ -260,7 +271,7 @@ private:
* @param locale Locale of the calendar
* @param status Error code
* @return the newly constructed fCalendar
* @draft ICU 3.8
* @internal ICU 3.8
*/
Calendar* initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
@ -274,7 +285,7 @@ public:
* . erived::getStaticClassID()) ...
* </pre>
* @return The class ID for all objects of this class.
* @draft ICU 3.8
* @internal ICU 3.8
*/
U_I18N_API static UClassID U_EXPORT2 getStaticClassID(void);
@ -287,7 +298,7 @@ public:
* @return The class ID for this object. All objects of a
* given class have the same class ID. Objects of
* other classes have different class IDs.
* @draft ICU 3.8
* @internal ICU 3.8
*/
virtual UClassID getDynamicClassID(void) const;
};

View file

@ -1,6 +1,6 @@
/*
*******************************************************************************
* Copyright (C) 1996-2010, International Business Machines
* Copyright (C) 1996-2011, International Business Machines
* Corporation and others. All Rights Reserved.
*******************************************************************************
*/
@ -385,11 +385,16 @@ udat_getSymbols(const UDateFormat *fmt,
int32_t resultLength,
UErrorCode *status)
{
verifyIsSimpleDateFormat(fmt, status);
if(U_FAILURE(*status)) return -1;
const DateFormatSymbols *syms =
((SimpleDateFormat*)fmt)->getDateFormatSymbols();
const DateFormatSymbols *syms;
const SimpleDateFormat* sdtfmt;
const RelativeDateFormat* rdtfmt;
if ((sdtfmt = dynamic_cast<const SimpleDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))) != NULL) {
syms = sdtfmt->getDateFormatSymbols();
} else if ((rdtfmt = dynamic_cast<const RelativeDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))) != NULL) {
syms = rdtfmt->getDateFormatSymbols();
} else {
return -1;
}
int32_t count;
const UnicodeString *res = NULL;
@ -495,15 +500,16 @@ U_CAPI int32_t U_EXPORT2
udat_countSymbols( const UDateFormat *fmt,
UDateFormatSymbolType type)
{
UErrorCode status = U_ZERO_ERROR;
verifyIsSimpleDateFormat(fmt, &status);
if(U_FAILURE(status)) {
const DateFormatSymbols *syms;
const SimpleDateFormat* sdtfmt;
const RelativeDateFormat* rdtfmt;
if ((sdtfmt = dynamic_cast<const SimpleDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))) != NULL) {
syms = sdtfmt->getDateFormatSymbols();
} else if ((rdtfmt = dynamic_cast<const RelativeDateFormat*>(reinterpret_cast<const DateFormat*>(fmt))) != NULL) {
syms = rdtfmt->getDateFormatSymbols();
} else {
return 0;
}
const DateFormatSymbols *syms =
((SimpleDateFormat*)fmt)->getDateFormatSymbols();
int32_t count = 0;
switch(type) {

View file

@ -1,6 +1,6 @@
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2010, International Business Machines Corporation and
* Copyright (c) 1997-2011, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/********************************************************************************
@ -1191,14 +1191,15 @@ static void TestRelativeCrash(void) {
}
}
{
UChar erabuf[32];
UErrorCode subStatus = U_ZERO_ERROR;
what = "udat_getSymbols";
log_verbose("Trying %s on a relative date..\n", what);
udat_getSymbols(icudf, UDAT_ERAS,0,NULL,0, &subStatus); /* bogus values */
if(subStatus == expectStatus) {
log_verbose("Success: did not crash on %s, but got %s.\n", what, u_errorName(subStatus));
udat_getSymbols(icudf, UDAT_ERAS,0,erabuf,sizeof(erabuf)/sizeof(erabuf[0]), &subStatus);
if(subStatus == U_ZERO_ERROR) {
log_verbose("Success: %s returned %s.\n", what, u_errorName(subStatus));
} else {
log_err("FAIL: didn't crash on %s, but got success %s instead of %s. \n", what, u_errorName(subStatus), u_errorName(expectStatus));
log_err("FAIL: didn't crash on %s, but got %s instead of U_ZERO_ERROR.\n", what, u_errorName(subStatus));
}
}
{