diff --git a/icu4c/source/i18n/dtitvfmt.cpp b/icu4c/source/i18n/dtitvfmt.cpp index d308c0454d0..a0bbc15df8f 100644 --- a/icu4c/source/i18n/dtitvfmt.cpp +++ b/icu4c/source/i18n/dtitvfmt.cpp @@ -459,10 +459,10 @@ DateIntervalFormat::adoptTimeZone(TimeZone* zone) // work clones of that calendar (and should not also be given ownership of the // adopted TimeZone). if (fFromCalendar) { - fFromCalendar->setTimeZone(*zone); + fFromCalendar->setTimeZone(*zone); } if (fToCalendar) { - fToCalendar->setTimeZone(*zone); + fToCalendar->setTimeZone(*zone); } } @@ -475,10 +475,10 @@ DateIntervalFormat::setTimeZone(const TimeZone& zone) // The fDateFormat has the master calendar for the DateIntervalFormat; // fFromCalendar and fToCalendar are internal work clones of that calendar. if (fFromCalendar) { - fFromCalendar->setTimeZone(zone); + fFromCalendar->setTimeZone(zone); } if (fToCalendar) { - fToCalendar->setTimeZone(zone); + fToCalendar->setTimeZone(zone); } } @@ -1386,25 +1386,21 @@ DateIntervalFormat::fallbackFormat(Calendar& fromCalendar, UnicodeString fallbackRange; MessageFormat::format(fallbackPattern, fmtArray, 2, fallbackRange, status); + if ( U_SUCCESS(status) && formatDatePlusTimeRange ) { + // fallbackRange has just the time range, need to format the date part and combine that + fDateFormat->applyPattern(*fDatePattern); + UnicodeString* datePortion = new UnicodeString(); + otherPos.setBeginIndex(0); + otherPos.setEndIndex(0); + fDateFormat->format(fromCalendar, *datePortion, otherPos); + adjustPosition(*fDateTimeFormat, fallbackRange, pos, *datePortion, otherPos, pos); + fmtArray[0].setString(fallbackRange); // {0} is time range + fmtArray[1].adoptString(datePortion); // {1} is single date portion + fallbackRange.remove(); + MessageFormat::format(*fDateTimeFormat, fmtArray, 2, fallbackRange, status); + } if ( U_SUCCESS(status) ) { - if (!formatDatePlusTimeRange) { - appendTo.append(fallbackRange); - } else { - // fallbackRange has just the time range, need to format the date part and combine that - fDateFormat->applyPattern(*fDatePattern); - UnicodeString* datePortion = new UnicodeString(); - otherPos.setBeginIndex(0); - otherPos.setEndIndex(0); - fDateFormat->format(fromCalendar, *datePortion, otherPos); - adjustPosition(*fDateTimeFormat, fallbackRange, pos, *datePortion, otherPos, pos); - fmtArray[0].setString(fallbackRange); // {0} is time range - fmtArray[1].adoptString(datePortion); // {1} is single date portion - fallbackRange.remove(); - MessageFormat::format(*fDateTimeFormat, fmtArray, 2, fallbackRange, status); - if ( U_SUCCESS(status) ) { - appendTo.append(fallbackRange); - } - } + appendTo.append(fallbackRange); } if (formatDatePlusTimeRange) { // restore full pattern diff --git a/icu4c/source/i18n/unicode/dtitvfmt.h b/icu4c/source/i18n/unicode/dtitvfmt.h index b91493780d5..4f6fdd0874e 100644 --- a/icu4c/source/i18n/unicode/dtitvfmt.h +++ b/icu4c/source/i18n/unicode/dtitvfmt.h @@ -389,6 +389,9 @@ public: * Result is appended to existing contents. * @param fieldPosition On input: an alignment field, if desired. * On output: the offsets of the alignment field. + * There may be multiple instances of a given field type + * in an interval format; in this case the fieldPosition + * offsets refer to the first instance. * @param status Output param filled with success/failure status. * @return Reference to 'appendTo' parameter. * @stable ICU 4.0 @@ -408,6 +411,9 @@ public: * Result is appended to existing contents. * @param fieldPosition On input: an alignment field, if desired. * On output: the offsets of the alignment field. + * There may be multiple instances of a given field type + * in an interval format; in this case the fieldPosition + * offsets refer to the first instance. * @param status Output param filled with success/failure status. * @return Reference to 'appendTo' parameter. * @stable ICU 4.0 @@ -432,6 +438,9 @@ public: * Result is appended to existing contents. * @param fieldPosition On input: an alignment field, if desired. * On output: the offsets of the alignment field. + * There may be multiple instances of a given field type + * in an interval format; in this case the fieldPosition + * offsets refer to the first instance. * @param status Output param filled with success/failure status. * Caller needs to make sure it is SUCCESS * at the function entrance @@ -667,12 +676,20 @@ private: */ /** - * @param combiningPattern xxx - * @param pat0 xxx - * @param pos0 xxx - * @param pat1 xxx - * @param pos1 xxx - * @param posResult xxx + * Provide an updated FieldPosition posResult based on two formats, + * the FieldPosition values for each of them, and the pattern used + * to combine them. The idea is for posResult to indicate the first + * instance (if any) of the specified field in the combined result, + * with correct offsets. + * + * @param combiningPattern Pattern used to combine pat0 and pat1 + * @param pat0 Formatted date/time value to replace {0} + * @param pos0 FieldPosition within pat0 + * @param pat1 Formatted date/time value to replace {1} + * @param pos1 FieldPosition within pat1 + * @param posResult FieldPosition to be set to the correct + * position of the first field instance when + * pat0 and pat1 are combined using combiningPattern */ static void adjustPosition(UnicodeString& combiningPattern, // has {0} and {1} in it @@ -681,7 +698,6 @@ private: FieldPosition& posResult); - /** * Format 2 Calendars using fall-back interval pattern * diff --git a/icu4c/source/i18n/unicode/udateintervalformat.h b/icu4c/source/i18n/unicode/udateintervalformat.h index 5ee92e629af..0b8cb8c9120 100644 --- a/icu4c/source/i18n/unicode/udateintervalformat.h +++ b/icu4c/source/i18n/unicode/udateintervalformat.h @@ -1,6 +1,6 @@ /* ***************************************************************************************** -* Copyright (C) 2010-2012, International Business Machines +* Copyright (C) 2010-2012,2015 International Business Machines * Corporation and others. All Rights Reserved. ***************************************************************************************** */ @@ -160,6 +160,9 @@ U_NAMESPACE_END * the beginning and ending indices of field number position->field, * if such a field exists. This parameter may be NULL, in which case * no field position data is returned. + * There may be multiple instances of a given field type in an + * interval format; in this case the position indices refer to the + * first instance. * @param status * A pointer to a UErrorCode to receive any errors. * @return