From 2fdfe2065514bd9d0238edc9ab283ccf1be90516 Mon Sep 17 00:00:00 2001 From: Peter Edberg Date: Mon, 2 May 2011 23:14:32 +0000 Subject: [PATCH] ICU-8237 Add RelativeDateFormat::getDateFormatSymbols, use in udat_getSymbols/countSymbols X-SVN-Rev: 29964 --- icu4c/source/i18n/reldtfmt.cpp | 50 +++++++++++++++++----------- icu4c/source/i18n/reldtfmt.h | 41 ++++++++++++++--------- icu4c/source/i18n/udat.cpp | 32 ++++++++++-------- icu4c/source/test/cintltst/cdattst.c | 11 +++--- 4 files changed, 81 insertions(+), 53 deletions(-) diff --git a/icu4c/source/i18n/reldtfmt.cpp b/icu4c/source/i18n/reldtfmt.cpp index c135c1d8782..065b0a1e612 100644 --- a/icu4c/source/i18n/reldtfmt.cpp +++ b/icu4c/source/i18n/reldtfmt.cpp @@ -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(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); diff --git a/icu4c/source/i18n/reldtfmt.h b/icu4c/source/i18n/reldtfmt.h index 692f9a95570..d7eed6e4582 100644 --- a/icu4c/source/i18n/reldtfmt.h +++ b/icu4c/source/i18n/reldtfmt.h @@ -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()) ... * * @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; }; diff --git a/icu4c/source/i18n/udat.cpp b/icu4c/source/i18n/udat.cpp index 758a6a7e113..4456a923bed 100644 --- a/icu4c/source/i18n/udat.cpp +++ b/icu4c/source/i18n/udat.cpp @@ -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(reinterpret_cast(fmt))) != NULL) { + syms = sdtfmt->getDateFormatSymbols(); + } else if ((rdtfmt = dynamic_cast(reinterpret_cast(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(reinterpret_cast(fmt))) != NULL) { + syms = sdtfmt->getDateFormatSymbols(); + } else if ((rdtfmt = dynamic_cast(reinterpret_cast(fmt))) != NULL) { + syms = rdtfmt->getDateFormatSymbols(); + } else { return 0; } - - const DateFormatSymbols *syms = - ((SimpleDateFormat*)fmt)->getDateFormatSymbols(); int32_t count = 0; switch(type) { diff --git a/icu4c/source/test/cintltst/cdattst.c b/icu4c/source/test/cintltst/cdattst.c index e06bd904909..36c8427a60d 100644 --- a/icu4c/source/test/cintltst/cdattst.c +++ b/icu4c/source/test/cintltst/cdattst.c @@ -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)); } } {