Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* |
michael@0 | 2 | ******************************************************************************* |
michael@0 | 3 | * Copyright (C) 2007-2012, International Business Machines Corporation and * |
michael@0 | 4 | * others. All Rights Reserved. * |
michael@0 | 5 | ******************************************************************************* |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #ifndef RELDTFMT_H |
michael@0 | 9 | #define RELDTFMT_H |
michael@0 | 10 | |
michael@0 | 11 | #include "unicode/utypes.h" |
michael@0 | 12 | |
michael@0 | 13 | /** |
michael@0 | 14 | * \file |
michael@0 | 15 | * \brief C++ API: Format and parse relative dates and times. |
michael@0 | 16 | */ |
michael@0 | 17 | |
michael@0 | 18 | #if !UCONFIG_NO_FORMATTING |
michael@0 | 19 | |
michael@0 | 20 | #include "unicode/datefmt.h" |
michael@0 | 21 | #include "unicode/smpdtfmt.h" |
michael@0 | 22 | |
michael@0 | 23 | U_NAMESPACE_BEGIN |
michael@0 | 24 | |
michael@0 | 25 | // forward declarations |
michael@0 | 26 | class DateFormatSymbols; |
michael@0 | 27 | class MessageFormat; |
michael@0 | 28 | |
michael@0 | 29 | // internal structure used for caching strings |
michael@0 | 30 | struct URelativeString; |
michael@0 | 31 | |
michael@0 | 32 | /** |
michael@0 | 33 | * This class is normally accessed using the kRelative or k...Relative values of EStyle as |
michael@0 | 34 | * parameters to DateFormat::createDateInstance. |
michael@0 | 35 | * |
michael@0 | 36 | * Example: |
michael@0 | 37 | * DateFormat *fullrelative = DateFormat::createDateInstance(DateFormat::kFullRelative, loc); |
michael@0 | 38 | * |
michael@0 | 39 | * @internal ICU 3.8 |
michael@0 | 40 | */ |
michael@0 | 41 | |
michael@0 | 42 | class RelativeDateFormat : public DateFormat { |
michael@0 | 43 | public: |
michael@0 | 44 | RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle, const Locale& locale, UErrorCode& status); |
michael@0 | 45 | |
michael@0 | 46 | // overrides |
michael@0 | 47 | /** |
michael@0 | 48 | * Copy constructor. |
michael@0 | 49 | * @internal ICU 3.8 |
michael@0 | 50 | */ |
michael@0 | 51 | RelativeDateFormat(const RelativeDateFormat&); |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Assignment operator. |
michael@0 | 55 | * @internal ICU 3.8 |
michael@0 | 56 | */ |
michael@0 | 57 | RelativeDateFormat& operator=(const RelativeDateFormat&); |
michael@0 | 58 | |
michael@0 | 59 | /** |
michael@0 | 60 | * Destructor. |
michael@0 | 61 | * @internal ICU 3.8 |
michael@0 | 62 | */ |
michael@0 | 63 | virtual ~RelativeDateFormat(); |
michael@0 | 64 | |
michael@0 | 65 | /** |
michael@0 | 66 | * Clone this Format object polymorphically. The caller owns the result and |
michael@0 | 67 | * should delete it when done. |
michael@0 | 68 | * @return A copy of the object. |
michael@0 | 69 | * @internal ICU 3.8 |
michael@0 | 70 | */ |
michael@0 | 71 | virtual Format* clone(void) const; |
michael@0 | 72 | |
michael@0 | 73 | /** |
michael@0 | 74 | * Return true if the given Format objects are semantically equal. Objects |
michael@0 | 75 | * of different subclasses are considered unequal. |
michael@0 | 76 | * @param other the object to be compared with. |
michael@0 | 77 | * @return true if the given Format objects are semantically equal. |
michael@0 | 78 | * @internal ICU 3.8 |
michael@0 | 79 | */ |
michael@0 | 80 | virtual UBool operator==(const Format& other) const; |
michael@0 | 81 | |
michael@0 | 82 | |
michael@0 | 83 | using DateFormat::format; |
michael@0 | 84 | |
michael@0 | 85 | /** |
michael@0 | 86 | * Format a date or time, which is the standard millis since 24:00 GMT, Jan |
michael@0 | 87 | * 1, 1970. Overrides DateFormat pure virtual method. |
michael@0 | 88 | * <P> |
michael@0 | 89 | * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->> |
michael@0 | 90 | * 1996.07.10 AD at 15:08:56 PDT |
michael@0 | 91 | * |
michael@0 | 92 | * @param cal Calendar set to the date and time to be formatted |
michael@0 | 93 | * into a date/time string. |
michael@0 | 94 | * @param appendTo Output parameter to receive result. |
michael@0 | 95 | * Result is appended to existing contents. |
michael@0 | 96 | * @param pos The formatting position. On input: an alignment field, |
michael@0 | 97 | * if desired. On output: the offsets of the alignment field. |
michael@0 | 98 | * @return Reference to 'appendTo' parameter. |
michael@0 | 99 | * @internal ICU 3.8 |
michael@0 | 100 | */ |
michael@0 | 101 | virtual UnicodeString& format( Calendar& cal, |
michael@0 | 102 | UnicodeString& appendTo, |
michael@0 | 103 | FieldPosition& pos) const; |
michael@0 | 104 | |
michael@0 | 105 | /** |
michael@0 | 106 | * Format an object to produce a string. This method handles Formattable |
michael@0 | 107 | * objects with a UDate type. If a the Formattable object type is not a Date, |
michael@0 | 108 | * then it returns a failing UErrorCode. |
michael@0 | 109 | * |
michael@0 | 110 | * @param obj The object to format. Must be a Date. |
michael@0 | 111 | * @param appendTo Output parameter to receive result. |
michael@0 | 112 | * Result is appended to existing contents. |
michael@0 | 113 | * @param pos On input: an alignment field, if desired. |
michael@0 | 114 | * On output: the offsets of the alignment field. |
michael@0 | 115 | * @param status Output param filled with success/failure status. |
michael@0 | 116 | * @return Reference to 'appendTo' parameter. |
michael@0 | 117 | * @internal ICU 3.8 |
michael@0 | 118 | */ |
michael@0 | 119 | virtual UnicodeString& format(const Formattable& obj, |
michael@0 | 120 | UnicodeString& appendTo, |
michael@0 | 121 | FieldPosition& pos, |
michael@0 | 122 | UErrorCode& status) const; |
michael@0 | 123 | |
michael@0 | 124 | |
michael@0 | 125 | /** |
michael@0 | 126 | * Parse a date/time string beginning at the given parse position. For |
michael@0 | 127 | * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date |
michael@0 | 128 | * that is equivalent to Date(837039928046). |
michael@0 | 129 | * <P> |
michael@0 | 130 | * By default, parsing is lenient: If the input is not in the form used by |
michael@0 | 131 | * this object's format method but can still be parsed as a date, then the |
michael@0 | 132 | * parse succeeds. Clients may insist on strict adherence to the format by |
michael@0 | 133 | * calling setLenient(false). |
michael@0 | 134 | * |
michael@0 | 135 | * @param text The date/time string to be parsed |
michael@0 | 136 | * @param cal a Calendar set to the date and time to be formatted |
michael@0 | 137 | * into a date/time string. |
michael@0 | 138 | * @param pos On input, the position at which to start parsing; on |
michael@0 | 139 | * output, the position at which parsing terminated, or the |
michael@0 | 140 | * start position if the parse failed. |
michael@0 | 141 | * @return A valid UDate if the input could be parsed. |
michael@0 | 142 | * @internal ICU 3.8 |
michael@0 | 143 | */ |
michael@0 | 144 | virtual void parse( const UnicodeString& text, |
michael@0 | 145 | Calendar& cal, |
michael@0 | 146 | ParsePosition& pos) const; |
michael@0 | 147 | |
michael@0 | 148 | /** |
michael@0 | 149 | * Parse a date/time string starting at the given parse position. For |
michael@0 | 150 | * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date |
michael@0 | 151 | * that is equivalent to Date(837039928046). |
michael@0 | 152 | * <P> |
michael@0 | 153 | * By default, parsing is lenient: If the input is not in the form used by |
michael@0 | 154 | * this object's format method but can still be parsed as a date, then the |
michael@0 | 155 | * parse succeeds. Clients may insist on strict adherence to the format by |
michael@0 | 156 | * calling setLenient(false). |
michael@0 | 157 | * |
michael@0 | 158 | * @see DateFormat::setLenient(boolean) |
michael@0 | 159 | * |
michael@0 | 160 | * @param text The date/time string to be parsed |
michael@0 | 161 | * @param pos On input, the position at which to start parsing; on |
michael@0 | 162 | * output, the position at which parsing terminated, or the |
michael@0 | 163 | * start position if the parse failed. |
michael@0 | 164 | * @return A valid UDate if the input could be parsed. |
michael@0 | 165 | * @internal ICU 3.8 |
michael@0 | 166 | */ |
michael@0 | 167 | UDate parse( const UnicodeString& text, |
michael@0 | 168 | ParsePosition& pos) const; |
michael@0 | 169 | |
michael@0 | 170 | |
michael@0 | 171 | /** |
michael@0 | 172 | * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT" |
michael@0 | 173 | * will be parsed into a UDate that is equivalent to Date(837039928046). |
michael@0 | 174 | * Parsing begins at the beginning of the string and proceeds as far as |
michael@0 | 175 | * possible. Assuming no parse errors were encountered, this function |
michael@0 | 176 | * doesn't return any information about how much of the string was consumed |
michael@0 | 177 | * by the parsing. If you need that information, use the version of |
michael@0 | 178 | * parse() that takes a ParsePosition. |
michael@0 | 179 | * |
michael@0 | 180 | * @param text The date/time string to be parsed |
michael@0 | 181 | * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with |
michael@0 | 182 | * an error value if there was a parse error. |
michael@0 | 183 | * @return A valid UDate if the input could be parsed. |
michael@0 | 184 | * @internal ICU 3.8 |
michael@0 | 185 | */ |
michael@0 | 186 | virtual UDate parse( const UnicodeString& text, |
michael@0 | 187 | UErrorCode& status) const; |
michael@0 | 188 | |
michael@0 | 189 | /** |
michael@0 | 190 | * Return a single pattern string generated by combining the patterns for the |
michael@0 | 191 | * date and time formatters associated with this object. |
michael@0 | 192 | * @param result Output param to receive the pattern. |
michael@0 | 193 | * @return A reference to 'result'. |
michael@0 | 194 | * @internal ICU 4.2 technology preview |
michael@0 | 195 | */ |
michael@0 | 196 | virtual UnicodeString& toPattern(UnicodeString& result, UErrorCode& status) const; |
michael@0 | 197 | |
michael@0 | 198 | /** |
michael@0 | 199 | * Get the date pattern for the the date formatter associated with this object. |
michael@0 | 200 | * @param result Output param to receive the date pattern. |
michael@0 | 201 | * @return A reference to 'result'. |
michael@0 | 202 | * @internal ICU 4.2 technology preview |
michael@0 | 203 | */ |
michael@0 | 204 | virtual UnicodeString& toPatternDate(UnicodeString& result, UErrorCode& status) const; |
michael@0 | 205 | |
michael@0 | 206 | /** |
michael@0 | 207 | * Get the time pattern for the the time formatter associated with this object. |
michael@0 | 208 | * @param result Output param to receive the time pattern. |
michael@0 | 209 | * @return A reference to 'result'. |
michael@0 | 210 | * @internal ICU 4.2 technology preview |
michael@0 | 211 | */ |
michael@0 | 212 | virtual UnicodeString& toPatternTime(UnicodeString& result, UErrorCode& status) const; |
michael@0 | 213 | |
michael@0 | 214 | /** |
michael@0 | 215 | * Apply the given unlocalized date & time pattern strings to this relative date format. |
michael@0 | 216 | * (i.e., after this call, this formatter will format dates and times according to |
michael@0 | 217 | * the new patterns) |
michael@0 | 218 | * |
michael@0 | 219 | * @param datePattern The date pattern to be applied. |
michael@0 | 220 | * @param timePattern The time pattern to be applied. |
michael@0 | 221 | * @internal ICU 4.2 technology preview |
michael@0 | 222 | */ |
michael@0 | 223 | virtual void applyPatterns(const UnicodeString& datePattern, const UnicodeString& timePattern, UErrorCode &status); |
michael@0 | 224 | |
michael@0 | 225 | /** |
michael@0 | 226 | * Gets the date/time formatting symbols (this is an object carrying |
michael@0 | 227 | * the various strings and other symbols used in formatting: e.g., month |
michael@0 | 228 | * names and abbreviations, time zone names, AM/PM strings, etc.) |
michael@0 | 229 | * @return a copy of the date-time formatting data associated |
michael@0 | 230 | * with this date-time formatter. |
michael@0 | 231 | * @internal ICU 4.8 |
michael@0 | 232 | */ |
michael@0 | 233 | virtual const DateFormatSymbols* getDateFormatSymbols(void) const; |
michael@0 | 234 | |
michael@0 | 235 | |
michael@0 | 236 | private: |
michael@0 | 237 | SimpleDateFormat *fDateTimeFormatter; |
michael@0 | 238 | UnicodeString fDatePattern; |
michael@0 | 239 | UnicodeString fTimePattern; |
michael@0 | 240 | MessageFormat *fCombinedFormat; // the {0} {1} format. |
michael@0 | 241 | |
michael@0 | 242 | UDateFormatStyle fDateStyle; |
michael@0 | 243 | Locale fLocale; |
michael@0 | 244 | |
michael@0 | 245 | int32_t fDayMin; // day id of lowest # |
michael@0 | 246 | int32_t fDayMax; // day id of highest # |
michael@0 | 247 | int32_t fDatesLen; // Length of array |
michael@0 | 248 | URelativeString *fDates; // array of strings |
michael@0 | 249 | |
michael@0 | 250 | |
michael@0 | 251 | /** |
michael@0 | 252 | * Get the string at a specific offset. |
michael@0 | 253 | * @param day day offset ( -1, 0, 1, etc.. ) |
michael@0 | 254 | * @param len on output, length of string. |
michael@0 | 255 | * @return the string, or NULL if none at that location. |
michael@0 | 256 | */ |
michael@0 | 257 | const UChar *getStringForDay(int32_t day, int32_t &len, UErrorCode &status) const; |
michael@0 | 258 | |
michael@0 | 259 | /** |
michael@0 | 260 | * Load the Date string array |
michael@0 | 261 | */ |
michael@0 | 262 | void loadDates(UErrorCode &status); |
michael@0 | 263 | |
michael@0 | 264 | /** |
michael@0 | 265 | * @return the number of days in "until-now" |
michael@0 | 266 | */ |
michael@0 | 267 | static int32_t dayDifference(Calendar &until, UErrorCode &status); |
michael@0 | 268 | |
michael@0 | 269 | /** |
michael@0 | 270 | * initializes fCalendar from parameters. Returns fCalendar as a convenience. |
michael@0 | 271 | * @param adoptZone Zone to be adopted, or NULL for TimeZone::createDefault(). |
michael@0 | 272 | * @param locale Locale of the calendar |
michael@0 | 273 | * @param status Error code |
michael@0 | 274 | * @return the newly constructed fCalendar |
michael@0 | 275 | * @internal ICU 3.8 |
michael@0 | 276 | */ |
michael@0 | 277 | Calendar* initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status); |
michael@0 | 278 | |
michael@0 | 279 | public: |
michael@0 | 280 | /** |
michael@0 | 281 | * Return the class ID for this class. This is useful only for comparing to |
michael@0 | 282 | * a return value from getDynamicClassID(). For example: |
michael@0 | 283 | * <pre> |
michael@0 | 284 | * . Base* polymorphic_pointer = createPolymorphicObject(); |
michael@0 | 285 | * . if (polymorphic_pointer->getDynamicClassID() == |
michael@0 | 286 | * . erived::getStaticClassID()) ... |
michael@0 | 287 | * </pre> |
michael@0 | 288 | * @return The class ID for all objects of this class. |
michael@0 | 289 | * @internal ICU 3.8 |
michael@0 | 290 | */ |
michael@0 | 291 | U_I18N_API static UClassID U_EXPORT2 getStaticClassID(void); |
michael@0 | 292 | |
michael@0 | 293 | /** |
michael@0 | 294 | * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This |
michael@0 | 295 | * method is to implement a simple version of RTTI, since not all C++ |
michael@0 | 296 | * compilers support genuine RTTI. Polymorphic operator==() and clone() |
michael@0 | 297 | * methods call this method. |
michael@0 | 298 | * |
michael@0 | 299 | * @return The class ID for this object. All objects of a |
michael@0 | 300 | * given class have the same class ID. Objects of |
michael@0 | 301 | * other classes have different class IDs. |
michael@0 | 302 | * @internal ICU 3.8 |
michael@0 | 303 | */ |
michael@0 | 304 | virtual UClassID getDynamicClassID(void) const; |
michael@0 | 305 | }; |
michael@0 | 306 | |
michael@0 | 307 | |
michael@0 | 308 | U_NAMESPACE_END |
michael@0 | 309 | |
michael@0 | 310 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
michael@0 | 311 | |
michael@0 | 312 | #endif // RELDTFMT_H |
michael@0 | 313 | //eof |