diff --git a/icu4c/source/i18n/udat.cpp b/icu4c/source/i18n/udat.cpp index 2a3456d1028..d1e519d8820 100644 --- a/icu4c/source/i18n/udat.cpp +++ b/icu4c/source/i18n/udat.cpp @@ -356,6 +356,13 @@ udat_setNumberFormat(UDateFormat* fmt, ((DateFormat*)fmt)->setNumberFormat(*((NumberFormat*)numberFormatToSet)); } +U_DRAFT void U_EXPORT2 +udat_adoptNumberFormat( UDateFormat* fmt, + UNumberFormat* numberFormatToAdopt) +{ + ((DateFormat*)fmt)->adoptNumberFormat((NumberFormat*)numberFormatToAdopt); +} + U_CAPI const char* U_EXPORT2 udat_getAvailable(int32_t index) { diff --git a/icu4c/source/i18n/unicode/udat.h b/icu4c/source/i18n/unicode/udat.h index d6ea01b172a..bf4aba10eec 100644 --- a/icu4c/source/i18n/unicode/udat.h +++ b/icu4c/source/i18n/unicode/udat.h @@ -1088,6 +1088,7 @@ udat_getNumberFormat(const UDateFormat* fmt); * Set the UNumberFormat associated with an UDateFormat. * A UDateFormat uses a UNumberFormat to format numbers within a date, * for example the day number. +* Note: udat_setNumberFormat will clone the UNumberFormat* * @param fmt The formatter to set. * @param numberFormatToSet A pointer to the UNumberFormat to be used by fmt to format numbers. * @see udat_getNumberFormat @@ -1097,6 +1098,19 @@ U_STABLE void U_EXPORT2 udat_setNumberFormat( UDateFormat* fmt, const UNumberFormat* numberFormatToSet); +/** +* Adopt the UNumberFormat associated with an UDateFormat. +* A UDateFormat uses a UNumberFormat to format numbers within a date, +* for example the day number. +* @param fmt The formatter to set. +* @param numberFormatToAdopt A pointer to the UNumberFormat to be used by fmt to format numbers. +* @see udat_getNumberFormat +* @draft ICU 54 +*/ +U_DRAFT void U_EXPORT2 +udat_adoptNumberFormat( UDateFormat* fmt, + UNumberFormat* numberFormatToAdopt); + /** * Get a locale for which date/time formatting patterns are available. * A UDateFormat in a locale returned by this function will perform the correct diff --git a/icu4c/source/test/cintltst/cdattst.c b/icu4c/source/test/cintltst/cdattst.c index dd62b9a43d8..37e38804fdb 100644 --- a/icu4c/source/test/cintltst/cdattst.c +++ b/icu4c/source/test/cintltst/cdattst.c @@ -66,6 +66,7 @@ static void TestDateFormat() UChar* result = NULL; const UCalendar *cal; const UNumberFormat *numformat1, *numformat2; + UNumberFormat *adoptNF; UChar temp[50]; int32_t numlocales; UDate d1; @@ -338,6 +339,16 @@ static void TestDateFormat() log_err("FAIL: error in setNumberFormat or getNumberFormat()\n"); else log_verbose("PASS:setNumberFormat and getNumberFormat succesful\n"); + + /*Test getNumberFormat() and adoptNumberFormat() */ + log_verbose("\nTesting the get and adopt NumberFormat properties of date format\n"); + adoptNF= unum_open(UNUM_DEFAULT, NULL, 0, NULL, NULL, &status); + udat_adoptNumberFormat(def1, adoptNF); + numformat2=udat_getNumberFormat(def1); + if(u_strcmp(myNumformat(adoptNF, num), myNumformat(numformat2, num)) !=0) + log_err("FAIL: error in adoptNumberFormat or getNumberFormat()\n"); + else + log_verbose("PASS:adoptNumberFormat and getNumberFormat succesful\n"); /*try setting the number format to another format */ numformat1=udat_getNumberFormat(def);