- }
-
- // Linear search the relative strings
- for(int n=0;n<fDatesLen;n++) {
- if(fDates[n].string != NULL &&
- (0==text.compare(pos.getIndex(),
- fDates[n].len,
- fDates[n].string))) {
- UErrorCode status = U_ZERO_ERROR;
-
- // Set the calendar to now+offset
- cal.setTime(Calendar::getNow(),status);
- cal.add(UCAL_DATE,fDates[n].offset, status);
-
- if(U_FAILURE(status)) {
- // failure in setting calendar fields
- pos.setErrorIndex(pos.getIndex()+fDates[n].len);
- } else {
- pos.setIndex(pos.getIndex()+fDates[n].len);
+ if (!matchedRelative) {
+ // just parse as normal date
+ fDateTimeFormatter->applyPattern(fDatePattern);
+ fDateTimeFormatter->parse(text,cal,pos);
+ }
+ } else {
+ // Here we replace any relativeDayString in text with the equivalent date
+ // formatted per fDatePattern, then parse text normally using the combined pattern.
+ UnicodeString modifiedText(text);
+ FieldPosition fPos;
+ int32_t dateStart = 0, origDateLen = 0, modDateLen = 0;
+ UErrorCode status = U_ZERO_ERROR;
+ for (int n=0; n < fDatesLen; n++) {
+ int32_t relativeStringOffset;
+ if (fDates[n].string != NULL &&
+ (relativeStringOffset = modifiedText.indexOf(fDates[n].string, fDates[n].len, startIndex)) >= startIndex) {
+ // it matched, replace the relative date with a real one for parsing
+ UnicodeString dateString;
+ Calendar * tempCal = cal.clone();
+
+ // Set the calendar to now+offset
+ tempCal->setTime(Calendar::getNow(),status);
+ tempCal->add(UCAL_DATE,fDates[n].offset, status);
+ if(U_FAILURE(status)) {
+ pos.setErrorIndex(startIndex);
+ delete tempCal;
+ return;
+ }
+
+ fDateTimeFormatter->applyPattern(fDatePattern);
+ fDateTimeFormatter->format(*tempCal, dateString, fPos);
+ dateStart = relativeStringOffset;
+ origDateLen = fDates[n].len;
+ modDateLen = dateString.length();
+ modifiedText.replace(dateStart, origDateLen, dateString);
+ delete tempCal;
+ break;