1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/i18n/reldtfmt.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,313 @@ 1.4 +/* 1.5 +******************************************************************************* 1.6 +* Copyright (C) 2007-2012, International Business Machines Corporation and * 1.7 +* others. All Rights Reserved. * 1.8 +******************************************************************************* 1.9 +*/ 1.10 + 1.11 +#ifndef RELDTFMT_H 1.12 +#define RELDTFMT_H 1.13 + 1.14 +#include "unicode/utypes.h" 1.15 + 1.16 +/** 1.17 + * \file 1.18 + * \brief C++ API: Format and parse relative dates and times. 1.19 + */ 1.20 + 1.21 +#if !UCONFIG_NO_FORMATTING 1.22 + 1.23 +#include "unicode/datefmt.h" 1.24 +#include "unicode/smpdtfmt.h" 1.25 + 1.26 +U_NAMESPACE_BEGIN 1.27 + 1.28 +// forward declarations 1.29 +class DateFormatSymbols; 1.30 +class MessageFormat; 1.31 + 1.32 +// internal structure used for caching strings 1.33 +struct URelativeString; 1.34 + 1.35 +/** 1.36 + * This class is normally accessed using the kRelative or k...Relative values of EStyle as 1.37 + * parameters to DateFormat::createDateInstance. 1.38 + * 1.39 + * Example: 1.40 + * DateFormat *fullrelative = DateFormat::createDateInstance(DateFormat::kFullRelative, loc); 1.41 + * 1.42 + * @internal ICU 3.8 1.43 + */ 1.44 + 1.45 +class RelativeDateFormat : public DateFormat { 1.46 +public: 1.47 + RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle, const Locale& locale, UErrorCode& status); 1.48 + 1.49 + // overrides 1.50 + /** 1.51 + * Copy constructor. 1.52 + * @internal ICU 3.8 1.53 + */ 1.54 + RelativeDateFormat(const RelativeDateFormat&); 1.55 + 1.56 + /** 1.57 + * Assignment operator. 1.58 + * @internal ICU 3.8 1.59 + */ 1.60 + RelativeDateFormat& operator=(const RelativeDateFormat&); 1.61 + 1.62 + /** 1.63 + * Destructor. 1.64 + * @internal ICU 3.8 1.65 + */ 1.66 + virtual ~RelativeDateFormat(); 1.67 + 1.68 + /** 1.69 + * Clone this Format object polymorphically. The caller owns the result and 1.70 + * should delete it when done. 1.71 + * @return A copy of the object. 1.72 + * @internal ICU 3.8 1.73 + */ 1.74 + virtual Format* clone(void) const; 1.75 + 1.76 + /** 1.77 + * Return true if the given Format objects are semantically equal. Objects 1.78 + * of different subclasses are considered unequal. 1.79 + * @param other the object to be compared with. 1.80 + * @return true if the given Format objects are semantically equal. 1.81 + * @internal ICU 3.8 1.82 + */ 1.83 + virtual UBool operator==(const Format& other) const; 1.84 + 1.85 + 1.86 + using DateFormat::format; 1.87 + 1.88 + /** 1.89 + * Format a date or time, which is the standard millis since 24:00 GMT, Jan 1.90 + * 1, 1970. Overrides DateFormat pure virtual method. 1.91 + * <P> 1.92 + * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->> 1.93 + * 1996.07.10 AD at 15:08:56 PDT 1.94 + * 1.95 + * @param cal Calendar set to the date and time to be formatted 1.96 + * into a date/time string. 1.97 + * @param appendTo Output parameter to receive result. 1.98 + * Result is appended to existing contents. 1.99 + * @param pos The formatting position. On input: an alignment field, 1.100 + * if desired. On output: the offsets of the alignment field. 1.101 + * @return Reference to 'appendTo' parameter. 1.102 + * @internal ICU 3.8 1.103 + */ 1.104 + virtual UnicodeString& format( Calendar& cal, 1.105 + UnicodeString& appendTo, 1.106 + FieldPosition& pos) const; 1.107 + 1.108 + /** 1.109 + * Format an object to produce a string. This method handles Formattable 1.110 + * objects with a UDate type. If a the Formattable object type is not a Date, 1.111 + * then it returns a failing UErrorCode. 1.112 + * 1.113 + * @param obj The object to format. Must be a Date. 1.114 + * @param appendTo Output parameter to receive result. 1.115 + * Result is appended to existing contents. 1.116 + * @param pos On input: an alignment field, if desired. 1.117 + * On output: the offsets of the alignment field. 1.118 + * @param status Output param filled with success/failure status. 1.119 + * @return Reference to 'appendTo' parameter. 1.120 + * @internal ICU 3.8 1.121 + */ 1.122 + virtual UnicodeString& format(const Formattable& obj, 1.123 + UnicodeString& appendTo, 1.124 + FieldPosition& pos, 1.125 + UErrorCode& status) const; 1.126 + 1.127 + 1.128 + /** 1.129 + * Parse a date/time string beginning at the given parse position. For 1.130 + * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date 1.131 + * that is equivalent to Date(837039928046). 1.132 + * <P> 1.133 + * By default, parsing is lenient: If the input is not in the form used by 1.134 + * this object's format method but can still be parsed as a date, then the 1.135 + * parse succeeds. Clients may insist on strict adherence to the format by 1.136 + * calling setLenient(false). 1.137 + * 1.138 + * @param text The date/time string to be parsed 1.139 + * @param cal a Calendar set to the date and time to be formatted 1.140 + * into a date/time string. 1.141 + * @param pos On input, the position at which to start parsing; on 1.142 + * output, the position at which parsing terminated, or the 1.143 + * start position if the parse failed. 1.144 + * @return A valid UDate if the input could be parsed. 1.145 + * @internal ICU 3.8 1.146 + */ 1.147 + virtual void parse( const UnicodeString& text, 1.148 + Calendar& cal, 1.149 + ParsePosition& pos) const; 1.150 + 1.151 + /** 1.152 + * Parse a date/time string starting at the given parse position. For 1.153 + * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date 1.154 + * that is equivalent to Date(837039928046). 1.155 + * <P> 1.156 + * By default, parsing is lenient: If the input is not in the form used by 1.157 + * this object's format method but can still be parsed as a date, then the 1.158 + * parse succeeds. Clients may insist on strict adherence to the format by 1.159 + * calling setLenient(false). 1.160 + * 1.161 + * @see DateFormat::setLenient(boolean) 1.162 + * 1.163 + * @param text The date/time string to be parsed 1.164 + * @param pos On input, the position at which to start parsing; on 1.165 + * output, the position at which parsing terminated, or the 1.166 + * start position if the parse failed. 1.167 + * @return A valid UDate if the input could be parsed. 1.168 + * @internal ICU 3.8 1.169 + */ 1.170 + UDate parse( const UnicodeString& text, 1.171 + ParsePosition& pos) const; 1.172 + 1.173 + 1.174 + /** 1.175 + * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT" 1.176 + * will be parsed into a UDate that is equivalent to Date(837039928046). 1.177 + * Parsing begins at the beginning of the string and proceeds as far as 1.178 + * possible. Assuming no parse errors were encountered, this function 1.179 + * doesn't return any information about how much of the string was consumed 1.180 + * by the parsing. If you need that information, use the version of 1.181 + * parse() that takes a ParsePosition. 1.182 + * 1.183 + * @param text The date/time string to be parsed 1.184 + * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with 1.185 + * an error value if there was a parse error. 1.186 + * @return A valid UDate if the input could be parsed. 1.187 + * @internal ICU 3.8 1.188 + */ 1.189 + virtual UDate parse( const UnicodeString& text, 1.190 + UErrorCode& status) const; 1.191 + 1.192 + /** 1.193 + * Return a single pattern string generated by combining the patterns for the 1.194 + * date and time formatters associated with this object. 1.195 + * @param result Output param to receive the pattern. 1.196 + * @return A reference to 'result'. 1.197 + * @internal ICU 4.2 technology preview 1.198 + */ 1.199 + virtual UnicodeString& toPattern(UnicodeString& result, UErrorCode& status) const; 1.200 + 1.201 + /** 1.202 + * Get the date pattern for the the date formatter associated with this object. 1.203 + * @param result Output param to receive the date pattern. 1.204 + * @return A reference to 'result'. 1.205 + * @internal ICU 4.2 technology preview 1.206 + */ 1.207 + virtual UnicodeString& toPatternDate(UnicodeString& result, UErrorCode& status) const; 1.208 + 1.209 + /** 1.210 + * Get the time pattern for the the time formatter associated with this object. 1.211 + * @param result Output param to receive the time pattern. 1.212 + * @return A reference to 'result'. 1.213 + * @internal ICU 4.2 technology preview 1.214 + */ 1.215 + virtual UnicodeString& toPatternTime(UnicodeString& result, UErrorCode& status) const; 1.216 + 1.217 + /** 1.218 + * Apply the given unlocalized date & time pattern strings to this relative date format. 1.219 + * (i.e., after this call, this formatter will format dates and times according to 1.220 + * the new patterns) 1.221 + * 1.222 + * @param datePattern The date pattern to be applied. 1.223 + * @param timePattern The time pattern to be applied. 1.224 + * @internal ICU 4.2 technology preview 1.225 + */ 1.226 + virtual void applyPatterns(const UnicodeString& datePattern, const UnicodeString& timePattern, UErrorCode &status); 1.227 + 1.228 + /** 1.229 + * Gets the date/time formatting symbols (this is an object carrying 1.230 + * the various strings and other symbols used in formatting: e.g., month 1.231 + * names and abbreviations, time zone names, AM/PM strings, etc.) 1.232 + * @return a copy of the date-time formatting data associated 1.233 + * with this date-time formatter. 1.234 + * @internal ICU 4.8 1.235 + */ 1.236 + virtual const DateFormatSymbols* getDateFormatSymbols(void) const; 1.237 + 1.238 + 1.239 +private: 1.240 + SimpleDateFormat *fDateTimeFormatter; 1.241 + UnicodeString fDatePattern; 1.242 + UnicodeString fTimePattern; 1.243 + MessageFormat *fCombinedFormat; // the {0} {1} format. 1.244 + 1.245 + UDateFormatStyle fDateStyle; 1.246 + Locale fLocale; 1.247 + 1.248 + int32_t fDayMin; // day id of lowest # 1.249 + int32_t fDayMax; // day id of highest # 1.250 + int32_t fDatesLen; // Length of array 1.251 + URelativeString *fDates; // array of strings 1.252 + 1.253 + 1.254 + /** 1.255 + * Get the string at a specific offset. 1.256 + * @param day day offset ( -1, 0, 1, etc.. ) 1.257 + * @param len on output, length of string. 1.258 + * @return the string, or NULL if none at that location. 1.259 + */ 1.260 + const UChar *getStringForDay(int32_t day, int32_t &len, UErrorCode &status) const; 1.261 + 1.262 + /** 1.263 + * Load the Date string array 1.264 + */ 1.265 + void loadDates(UErrorCode &status); 1.266 + 1.267 + /** 1.268 + * @return the number of days in "until-now" 1.269 + */ 1.270 + static int32_t dayDifference(Calendar &until, UErrorCode &status); 1.271 + 1.272 + /** 1.273 + * initializes fCalendar from parameters. Returns fCalendar as a convenience. 1.274 + * @param adoptZone Zone to be adopted, or NULL for TimeZone::createDefault(). 1.275 + * @param locale Locale of the calendar 1.276 + * @param status Error code 1.277 + * @return the newly constructed fCalendar 1.278 + * @internal ICU 3.8 1.279 + */ 1.280 + Calendar* initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status); 1.281 + 1.282 +public: 1.283 + /** 1.284 + * Return the class ID for this class. This is useful only for comparing to 1.285 + * a return value from getDynamicClassID(). For example: 1.286 + * <pre> 1.287 + * . Base* polymorphic_pointer = createPolymorphicObject(); 1.288 + * . if (polymorphic_pointer->getDynamicClassID() == 1.289 + * . erived::getStaticClassID()) ... 1.290 + * </pre> 1.291 + * @return The class ID for all objects of this class. 1.292 + * @internal ICU 3.8 1.293 + */ 1.294 + U_I18N_API static UClassID U_EXPORT2 getStaticClassID(void); 1.295 + 1.296 + /** 1.297 + * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This 1.298 + * method is to implement a simple version of RTTI, since not all C++ 1.299 + * compilers support genuine RTTI. Polymorphic operator==() and clone() 1.300 + * methods call this method. 1.301 + * 1.302 + * @return The class ID for this object. All objects of a 1.303 + * given class have the same class ID. Objects of 1.304 + * other classes have different class IDs. 1.305 + * @internal ICU 3.8 1.306 + */ 1.307 + virtual UClassID getDynamicClassID(void) const; 1.308 +}; 1.309 + 1.310 + 1.311 +U_NAMESPACE_END 1.312 + 1.313 +#endif /* #if !UCONFIG_NO_FORMATTING */ 1.314 + 1.315 +#endif // RELDTFMT_H 1.316 +//eof