diff --git a/icu4c/source/i18n/datefmt.cpp b/icu4c/source/i18n/datefmt.cpp index c5c0e671747..0e383ffb24b 100644 --- a/icu4c/source/i18n/datefmt.cpp +++ b/icu4c/source/i18n/datefmt.cpp @@ -289,7 +289,7 @@ DateFormat::create(EStyle timeStyle, EStyle dateStyle, const Locale& locale) #endif // is it relative? - if(/*((timeStyle!=UDAT_NONE)&&(timeStyle & UDAT_RELATIVE)) || */((dateStyle!=(UDateFormatStyle)UDAT_NONE)&&((dateStyle-kDateOffset) & UDAT_RELATIVE))) { + if(/*((timeStyle!=UDAT_NONE)&&(timeStyle & UDAT_RELATIVE)) || */((dateStyle!=kNone)&&((dateStyle-kDateOffset) & UDAT_RELATIVE))) { RelativeDateFormat *r = new RelativeDateFormat((UDateFormatStyle)timeStyle, (UDateFormatStyle)(dateStyle-kDateOffset), locale, status); if(U_SUCCESS(status)) return r; delete r; diff --git a/icu4c/source/i18n/reldtfmt.cpp b/icu4c/source/i18n/reldtfmt.cpp index 6542e2d53e6..e0158898189 100644 --- a/icu4c/source/i18n/reldtfmt.cpp +++ b/icu4c/source/i18n/reldtfmt.cpp @@ -36,36 +36,38 @@ struct URelativeString { UOBJECT_DEFINE_RTTI_IMPLEMENTATION(RelativeDateFormat) RelativeDateFormat::RelativeDateFormat(const RelativeDateFormat& other) : -DateFormat(other),fCalData(NULL), fStrings(NULL), dates(NULL), datesLen(0), fCombinedFormat(NULL) { - - dateStyle = other.dateStyle; - timeStyle = other.timeStyle; +DateFormat(other), fDateFormat(NULL), fTimeFormat(NULL), fCombinedFormat(NULL), +fDateStyle(other.fDateStyle), fTimeStyle(other.fTimeStyle), fLocale(other.fLocale), +fCalData(NULL), fStrings(NULL), fDatesLen(0), fDates(NULL) +{ if(other.fDateFormat != NULL) { fDateFormat = (DateFormat*)other.fDateFormat->clone(); } else { fDateFormat = NULL; } - +/* if(other.fTimeFormat != NULL) { fTimeFormat = (DateFormat*)other.fTimeFormat->clone(); } else { fTimeFormat = NULL; } +*/ } RelativeDateFormat::RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle, const Locale& locale, UErrorCode& status) - : dateStyle(dateStyle), timeStyle(timeStyle), fDateFormat(NULL), fCombinedFormat(NULL), fTimeFormat(NULL), - locale(locale),fCalData(NULL), fStrings(NULL), dates(NULL), datesLen(0) { + : DateFormat(), fDateFormat(NULL), fTimeFormat(NULL), fCombinedFormat(NULL), +fDateStyle(dateStyle), fTimeStyle(timeStyle), fCalData(NULL), fStrings(NULL), fDatesLen(0), fDates(NULL) + { if(U_FAILURE(status) ) { return; } - if(dateStyle != UDAT_NONE) { - EStyle newStyle = (EStyle)(dateStyle & ~UDAT_RELATIVE); + if(fDateStyle != UDAT_NONE) { + EStyle newStyle = (EStyle)(fDateStyle & ~UDAT_RELATIVE); // Create a DateFormat in the non-relative style requested. fDateFormat = createDateInstance(newStyle, locale); } - if(timeStyle != UDAT_NONE) { + if(fTimeStyle != UDAT_NONE) { // don't support time style, for now status = U_ILLEGAL_ARGUMENT_ERROR; return; @@ -79,7 +81,7 @@ RelativeDateFormat::~RelativeDateFormat() { delete fDateFormat; delete fTimeFormat; delete fCombinedFormat; - uprv_free(dates); + uprv_free(fDates); // do NOT: delete fStrings - as they are loaded from mapped memory, all owned by fCalData. delete fCalData; } @@ -94,9 +96,9 @@ UBool RelativeDateFormat::operator==(const Format& other) const { if(DateFormat::operator==(other)) { // DateFormat::operator== guarantees following cast is safe RelativeDateFormat* that = (RelativeDateFormat*)&other; - return (dateStyle==that->dateStyle && - timeStyle==that->timeStyle && - locale==that->locale); + return (fDateStyle==that->fDateStyle && + fTimeStyle==that->fTimeStyle && + fLocale==that->fLocale); } return FALSE; } @@ -127,6 +129,22 @@ UnicodeString& RelativeDateFormat::format( Calendar& cal, } } + + +UnicodeString& +RelativeDateFormat::format(const Formattable& obj, + UnicodeString& appendTo, + FieldPosition& pos, + UErrorCode& status) const +{ + // this is just here to get around the hiding problem + // (the previous format() override would hide the version of + // format() on DateFormat that this function correspond to, so we + // have to redefine it here) + return DateFormat::format(obj, appendTo, pos, status); +} + + void RelativeDateFormat::parse( const UnicodeString& text, Calendar& cal, ParsePosition& pos) const { @@ -143,22 +161,22 @@ void RelativeDateFormat::parse( const UnicodeString& text, } // Linear search the relative strings - for(int n=0;nfCalData = new CalendarData(locale, "gregorian", status); + ((RelativeDateFormat*)this)->fCalData = new CalendarData(fLocale, "gregorian", status); } if(fStrings == NULL) { @@ -191,7 +226,7 @@ const UChar *RelativeDateFormat::getStringForDay(int32_t day, int32_t &len, UErr return NULL; } - if(dates == NULL) { + if(fDates == NULL) { loadDates(status); if(U_FAILURE(status)) { return NULL; @@ -199,20 +234,20 @@ const UChar *RelativeDateFormat::getStringForDay(int32_t day, int32_t &len, UErr } // no strings. - if(datesLen == 0) { + if(fDatesLen == 0) { return NULL; } // Is it outside the resource bundle's range? - if(day < dayMin || day > dayMax) { + if(day < fDayMin || day > fDayMax) { return NULL; // don't have it. } // Linear search the held strings - for(int n=0;ndayMin=-1; - nonConstThis->dayMax=1; + nonConstThis->fDayMin=-1; + nonConstThis->fDayMax=1; if(U_FAILURE(status)) { - nonConstThis->datesLen=0; + nonConstThis->fDatesLen=0; return; } - nonConstThis->datesLen = ures_getSize(strings); - nonConstThis->dates = (URelativeString*) uprv_malloc(sizeof(dates[0])*datesLen); + nonConstThis->fDatesLen = ures_getSize(strings); + nonConstThis->fDates = (URelativeString*) uprv_malloc(sizeof(fDates[0])*fDatesLen); // Load in each item into the array... int n = 0; @@ -259,22 +294,22 @@ void RelativeDateFormat::loadDates(UErrorCode &status) const { int32_t offset = atoi(key); // set min/max - if(offset < dayMin) { - nonConstThis->dayMin = offset; + if(offset < fDayMin) { + nonConstThis->fDayMin = offset; } - if(offset > dayMax) { - nonConstThis->dayMax = offset; + if(offset > fDayMax) { + nonConstThis->fDayMax = offset; } // copy the string pointer - nonConstThis->dates[n].offset = offset; - nonConstThis->dates[n].string = aString; - nonConstThis->dates[n].len = aLen; + nonConstThis->fDates[n].offset = offset; + nonConstThis->fDates[n].string = aString; + nonConstThis->fDates[n].len = aLen; n++; } - // the dates[] array could be sorted here, for direct access. + // the fDates[] array could be sorted here, for direct access. } diff --git a/icu4c/source/i18n/reldtfmt.h b/icu4c/source/i18n/reldtfmt.h index de7b991b279..baf1b269749 100644 --- a/icu4c/source/i18n/reldtfmt.h +++ b/icu4c/source/i18n/reldtfmt.h @@ -97,6 +97,25 @@ public: UnicodeString& appendTo, FieldPosition& pos) const; + /** + * Format an object to produce a string. This method handles Formattable + * objects with a UDate type. If a the Formattable object type is not a Date, + * then it returns a failing UErrorCode. + * + * @param obj The object to format. Must be a Date. + * @param appendTo Output parameter to receive result. + * Result is appended to existing contents. + * @param pos On input: an alignment field, if desired. + * 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 + */ + virtual UnicodeString& format(const Formattable& obj, + UnicodeString& appendTo, + FieldPosition& pos, + UErrorCode& status) const; + /** * Parse a date/time string beginning at the given parse position. For @@ -119,23 +138,67 @@ public: */ virtual void parse( const UnicodeString& text, Calendar& cal, - ParsePosition& pos) const; + ParsePosition& pos) const; + + /** + * Parse a date/time string starting at the given parse position. For + * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date + * that is equivalent to Date(837039928046). + *

+ * By default, parsing is lenient: If the input is not in the form used by + * this object's format method but can still be parsed as a date, then the + * parse succeeds. Clients may insist on strict adherence to the format by + * calling setLenient(false). + * + * @see DateFormat::setLenient(boolean) + * + * @param text The date/time string to be parsed + * @param pos On input, the position at which to start parsing; on + * 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 + */ + UDate parse( const UnicodeString& text, + ParsePosition& pos) const; + + + /** + * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT" + * will be parsed into a UDate that is equivalent to Date(837039928046). + * Parsing begins at the beginning of the string and proceeds as far as + * possible. Assuming no parse errors were encountered, this function + * doesn't return any information about how much of the string was consumed + * by the parsing. If you need that information, use the version of + * parse() that takes a ParsePosition. + * + * @param text The date/time string to be parsed + * @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 + */ + virtual UDate parse( const UnicodeString& text, + UErrorCode& status) const; + + + private: DateFormat *fDateFormat; // the held date format DateFormat *fTimeFormat; // the held time format MessageFormat *fCombinedFormat; // the {0} {1} format. - UDateFormatStyle dateStyle; - UDateFormatStyle timeStyle; - Locale locale; + UDateFormatStyle fDateStyle; + UDateFormatStyle fTimeStyle; + Locale fLocale; CalendarData *fCalData; // holds a reference to the resource data UResourceBundle *fStrings; // the array of strings - int32_t dayMin; // day id of lowest # - int32_t dayMax; // day id of highest # - int32_t datesLen; // Length of array - URelativeString *dates; // array of strings + int32_t fDayMin; // day id of lowest # + int32_t fDayMax; // day id of highest # + int32_t fDatesLen; // Length of array + URelativeString *fDates; // array of strings /**