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