michael@0: /* michael@0: ******************************************************************************* michael@0: * Copyright (C) 2007-2012, International Business Machines Corporation and * michael@0: * others. All Rights Reserved. * michael@0: ******************************************************************************* michael@0: */ michael@0: michael@0: #ifndef RELDTFMT_H michael@0: #define RELDTFMT_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: /** michael@0: * \file michael@0: * \brief C++ API: Format and parse relative dates and times. michael@0: */ michael@0: michael@0: #if !UCONFIG_NO_FORMATTING michael@0: michael@0: #include "unicode/datefmt.h" michael@0: #include "unicode/smpdtfmt.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: // forward declarations michael@0: class DateFormatSymbols; michael@0: class MessageFormat; michael@0: michael@0: // internal structure used for caching strings michael@0: struct URelativeString; michael@0: michael@0: /** michael@0: * This class is normally accessed using the kRelative or k...Relative values of EStyle as michael@0: * parameters to DateFormat::createDateInstance. michael@0: * michael@0: * Example: michael@0: * DateFormat *fullrelative = DateFormat::createDateInstance(DateFormat::kFullRelative, loc); michael@0: * michael@0: * @internal ICU 3.8 michael@0: */ michael@0: michael@0: class RelativeDateFormat : public DateFormat { michael@0: public: michael@0: RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle, const Locale& locale, UErrorCode& status); michael@0: michael@0: // overrides michael@0: /** michael@0: * Copy constructor. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: RelativeDateFormat(const RelativeDateFormat&); michael@0: michael@0: /** michael@0: * Assignment operator. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: RelativeDateFormat& operator=(const RelativeDateFormat&); michael@0: michael@0: /** michael@0: * Destructor. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: virtual ~RelativeDateFormat(); michael@0: michael@0: /** michael@0: * Clone this Format object polymorphically. The caller owns the result and michael@0: * should delete it when done. michael@0: * @return A copy of the object. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: virtual Format* clone(void) const; michael@0: michael@0: /** michael@0: * Return true if the given Format objects are semantically equal. Objects michael@0: * of different subclasses are considered unequal. michael@0: * @param other the object to be compared with. michael@0: * @return true if the given Format objects are semantically equal. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: virtual UBool operator==(const Format& other) const; michael@0: michael@0: michael@0: using DateFormat::format; michael@0: michael@0: /** michael@0: * Format a date or time, which is the standard millis since 24:00 GMT, Jan michael@0: * 1, 1970. Overrides DateFormat pure virtual method. michael@0: *
michael@0: * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->> michael@0: * 1996.07.10 AD at 15:08:56 PDT michael@0: * michael@0: * @param cal Calendar set to the date and time to be formatted michael@0: * into a date/time string. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param pos The formatting position. On input: an alignment field, michael@0: * if desired. On output: the offsets of the alignment field. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: virtual UnicodeString& format( Calendar& cal, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& pos) const; michael@0: michael@0: /** michael@0: * Format an object to produce a string. This method handles Formattable michael@0: * objects with a UDate type. If a the Formattable object type is not a Date, michael@0: * then it returns a failing UErrorCode. michael@0: * michael@0: * @param obj The object to format. Must be a Date. michael@0: * @param appendTo Output parameter to receive result. michael@0: * Result is appended to existing contents. michael@0: * @param pos On input: an alignment field, if desired. michael@0: * On output: the offsets of the alignment field. michael@0: * @param status Output param filled with success/failure status. michael@0: * @return Reference to 'appendTo' parameter. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: virtual UnicodeString& format(const Formattable& obj, michael@0: UnicodeString& appendTo, michael@0: FieldPosition& pos, michael@0: UErrorCode& status) const; michael@0: michael@0: michael@0: /** michael@0: * Parse a date/time string beginning at the given parse position. For michael@0: * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date michael@0: * that is equivalent to Date(837039928046). michael@0: *
michael@0: * By default, parsing is lenient: If the input is not in the form used by michael@0: * this object's format method but can still be parsed as a date, then the michael@0: * parse succeeds. Clients may insist on strict adherence to the format by michael@0: * calling setLenient(false). michael@0: * michael@0: * @param text The date/time string to be parsed michael@0: * @param cal a Calendar set to the date and time to be formatted michael@0: * into a date/time string. michael@0: * @param pos On input, the position at which to start parsing; on michael@0: * output, the position at which parsing terminated, or the michael@0: * start position if the parse failed. michael@0: * @return A valid UDate if the input could be parsed. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: virtual void parse( const UnicodeString& text, michael@0: Calendar& cal, michael@0: ParsePosition& pos) const; michael@0: michael@0: /** michael@0: * Parse a date/time string starting at the given parse position. For michael@0: * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date michael@0: * that is equivalent to Date(837039928046). michael@0: *
michael@0: * By default, parsing is lenient: If the input is not in the form used by michael@0: * this object's format method but can still be parsed as a date, then the michael@0: * parse succeeds. Clients may insist on strict adherence to the format by michael@0: * calling setLenient(false). michael@0: * michael@0: * @see DateFormat::setLenient(boolean) michael@0: * michael@0: * @param text The date/time string to be parsed michael@0: * @param pos On input, the position at which to start parsing; on michael@0: * output, the position at which parsing terminated, or the michael@0: * start position if the parse failed. michael@0: * @return A valid UDate if the input could be parsed. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: UDate parse( const UnicodeString& text, michael@0: ParsePosition& pos) const; michael@0: michael@0: michael@0: /** michael@0: * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT" michael@0: * will be parsed into a UDate that is equivalent to Date(837039928046). michael@0: * Parsing begins at the beginning of the string and proceeds as far as michael@0: * possible. Assuming no parse errors were encountered, this function michael@0: * doesn't return any information about how much of the string was consumed michael@0: * by the parsing. If you need that information, use the version of michael@0: * parse() that takes a ParsePosition. michael@0: * michael@0: * @param text The date/time string to be parsed michael@0: * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with michael@0: * an error value if there was a parse error. michael@0: * @return A valid UDate if the input could be parsed. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: virtual UDate parse( const UnicodeString& text, michael@0: UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Return a single pattern string generated by combining the patterns for the michael@0: * date and time formatters associated with this object. michael@0: * @param result Output param to receive the pattern. michael@0: * @return A reference to 'result'. michael@0: * @internal ICU 4.2 technology preview michael@0: */ michael@0: virtual UnicodeString& toPattern(UnicodeString& result, UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Get the date pattern for the the date formatter associated with this object. michael@0: * @param result Output param to receive the date pattern. michael@0: * @return A reference to 'result'. michael@0: * @internal ICU 4.2 technology preview michael@0: */ michael@0: virtual UnicodeString& toPatternDate(UnicodeString& result, UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Get the time pattern for the the time formatter associated with this object. michael@0: * @param result Output param to receive the time pattern. michael@0: * @return A reference to 'result'. michael@0: * @internal ICU 4.2 technology preview michael@0: */ michael@0: virtual UnicodeString& toPatternTime(UnicodeString& result, UErrorCode& status) const; michael@0: michael@0: /** michael@0: * Apply the given unlocalized date & time pattern strings to this relative date format. michael@0: * (i.e., after this call, this formatter will format dates and times according to michael@0: * the new patterns) michael@0: * michael@0: * @param datePattern The date pattern to be applied. michael@0: * @param timePattern The time pattern to be applied. michael@0: * @internal ICU 4.2 technology preview michael@0: */ michael@0: virtual void applyPatterns(const UnicodeString& datePattern, const UnicodeString& timePattern, UErrorCode &status); michael@0: michael@0: /** michael@0: * Gets the date/time formatting symbols (this is an object carrying michael@0: * the various strings and other symbols used in formatting: e.g., month michael@0: * names and abbreviations, time zone names, AM/PM strings, etc.) michael@0: * @return a copy of the date-time formatting data associated michael@0: * with this date-time formatter. michael@0: * @internal ICU 4.8 michael@0: */ michael@0: virtual const DateFormatSymbols* getDateFormatSymbols(void) const; michael@0: michael@0: michael@0: private: michael@0: SimpleDateFormat *fDateTimeFormatter; michael@0: UnicodeString fDatePattern; michael@0: UnicodeString fTimePattern; michael@0: MessageFormat *fCombinedFormat; // the {0} {1} format. michael@0: michael@0: UDateFormatStyle fDateStyle; michael@0: Locale fLocale; michael@0: michael@0: int32_t fDayMin; // day id of lowest # michael@0: int32_t fDayMax; // day id of highest # michael@0: int32_t fDatesLen; // Length of array michael@0: URelativeString *fDates; // array of strings michael@0: michael@0: michael@0: /** michael@0: * Get the string at a specific offset. michael@0: * @param day day offset ( -1, 0, 1, etc.. ) michael@0: * @param len on output, length of string. michael@0: * @return the string, or NULL if none at that location. michael@0: */ michael@0: const UChar *getStringForDay(int32_t day, int32_t &len, UErrorCode &status) const; michael@0: michael@0: /** michael@0: * Load the Date string array michael@0: */ michael@0: void loadDates(UErrorCode &status); michael@0: michael@0: /** michael@0: * @return the number of days in "until-now" michael@0: */ michael@0: static int32_t dayDifference(Calendar &until, UErrorCode &status); michael@0: michael@0: /** michael@0: * initializes fCalendar from parameters. Returns fCalendar as a convenience. michael@0: * @param adoptZone Zone to be adopted, or NULL for TimeZone::createDefault(). michael@0: * @param locale Locale of the calendar michael@0: * @param status Error code michael@0: * @return the newly constructed fCalendar michael@0: * @internal ICU 3.8 michael@0: */ michael@0: Calendar* initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status); michael@0: michael@0: public: michael@0: /** michael@0: * Return the class ID for this class. This is useful only for comparing to michael@0: * a return value from getDynamicClassID(). For example: michael@0: *
michael@0: * . Base* polymorphic_pointer = createPolymorphicObject(); michael@0: * . if (polymorphic_pointer->getDynamicClassID() == michael@0: * . erived::getStaticClassID()) ... michael@0: *michael@0: * @return The class ID for all objects of this class. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: U_I18N_API static UClassID U_EXPORT2 getStaticClassID(void); michael@0: michael@0: /** michael@0: * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This michael@0: * method is to implement a simple version of RTTI, since not all C++ michael@0: * compilers support genuine RTTI. Polymorphic operator==() and clone() michael@0: * methods call this method. michael@0: * michael@0: * @return The class ID for this object. All objects of a michael@0: * given class have the same class ID. Objects of michael@0: * other classes have different class IDs. michael@0: * @internal ICU 3.8 michael@0: */ michael@0: virtual UClassID getDynamicClassID(void) const; michael@0: }; michael@0: michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #endif /* #if !UCONFIG_NO_FORMATTING */ michael@0: michael@0: #endif // RELDTFMT_H michael@0: //eof