2 *******************************************************************************
3 * Copyright (C) 2007-2012, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
11 #include "unicode/utypes.h"
15 * \brief C++ API: Format and parse relative dates and times.
18 #if !UCONFIG_NO_FORMATTING
20 #include "unicode/datefmt.h"
21 #include "unicode/smpdtfmt.h"
25 // forward declarations
26 class DateFormatSymbols
;
29 // internal structure used for caching strings
30 struct URelativeString
;
33 * This class is normally accessed using the kRelative or k...Relative values of EStyle as
34 * parameters to DateFormat::createDateInstance.
37 * DateFormat *fullrelative = DateFormat::createDateInstance(DateFormat::kFullRelative, loc);
42 class RelativeDateFormat
: public DateFormat
{
44 RelativeDateFormat( UDateFormatStyle timeStyle
, UDateFormatStyle dateStyle
, const Locale
& locale
, UErrorCode
& status
);
51 RelativeDateFormat(const RelativeDateFormat
&);
54 * Assignment operator.
57 RelativeDateFormat
& operator=(const RelativeDateFormat
&);
63 virtual ~RelativeDateFormat();
66 * Clone this Format object polymorphically. The caller owns the result and
67 * should delete it when done.
68 * @return A copy of the object.
71 virtual Format
* clone(void) const;
74 * Return true if the given Format objects are semantically equal. Objects
75 * of different subclasses are considered unequal.
76 * @param other the object to be compared with.
77 * @return true if the given Format objects are semantically equal.
80 virtual UBool
operator==(const Format
& other
) const;
83 using DateFormat::format
;
86 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
87 * 1, 1970. Overrides DateFormat pure virtual method.
89 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
90 * 1996.07.10 AD at 15:08:56 PDT
92 * @param cal Calendar set to the date and time to be formatted
93 * into a date/time string.
94 * @param appendTo Output parameter to receive result.
95 * Result is appended to existing contents.
96 * @param pos The formatting position. On input: an alignment field,
97 * if desired. On output: the offsets of the alignment field.
98 * @return Reference to 'appendTo' parameter.
101 virtual UnicodeString
& format( Calendar
& cal
,
102 UnicodeString
& appendTo
,
103 FieldPosition
& pos
) const;
106 * Format an object to produce a string. This method handles Formattable
107 * objects with a UDate type. If a the Formattable object type is not a Date,
108 * then it returns a failing UErrorCode.
110 * @param obj The object to format. Must be a Date.
111 * @param appendTo Output parameter to receive result.
112 * Result is appended to existing contents.
113 * @param pos On input: an alignment field, if desired.
114 * On output: the offsets of the alignment field.
115 * @param status Output param filled with success/failure status.
116 * @return Reference to 'appendTo' parameter.
119 virtual UnicodeString
& format(const Formattable
& obj
,
120 UnicodeString
& appendTo
,
122 UErrorCode
& status
) const;
126 * Parse a date/time string beginning at the given parse position. For
127 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
128 * that is equivalent to Date(837039928046).
130 * By default, parsing is lenient: If the input is not in the form used by
131 * this object's format method but can still be parsed as a date, then the
132 * parse succeeds. Clients may insist on strict adherence to the format by
133 * calling setLenient(false).
135 * @param text The date/time string to be parsed
136 * @param cal a Calendar set to the date and time to be formatted
137 * into a date/time string.
138 * @param pos On input, the position at which to start parsing; on
139 * output, the position at which parsing terminated, or the
140 * start position if the parse failed.
141 * @return A valid UDate if the input could be parsed.
144 virtual void parse( const UnicodeString
& text
,
146 ParsePosition
& pos
) const;
149 * Parse a date/time string starting at the given parse position. For
150 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
151 * that is equivalent to Date(837039928046).
153 * By default, parsing is lenient: If the input is not in the form used by
154 * this object's format method but can still be parsed as a date, then the
155 * parse succeeds. Clients may insist on strict adherence to the format by
156 * calling setLenient(false).
158 * @see DateFormat::setLenient(boolean)
160 * @param text The date/time string to be parsed
161 * @param pos On input, the position at which to start parsing; on
162 * output, the position at which parsing terminated, or the
163 * start position if the parse failed.
164 * @return A valid UDate if the input could be parsed.
167 UDate
parse( const UnicodeString
& text
,
168 ParsePosition
& pos
) const;
172 * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT"
173 * will be parsed into a UDate that is equivalent to Date(837039928046).
174 * Parsing begins at the beginning of the string and proceeds as far as
175 * possible. Assuming no parse errors were encountered, this function
176 * doesn't return any information about how much of the string was consumed
177 * by the parsing. If you need that information, use the version of
178 * parse() that takes a ParsePosition.
180 * @param text The date/time string to be parsed
181 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
182 * an error value if there was a parse error.
183 * @return A valid UDate if the input could be parsed.
186 virtual UDate
parse( const UnicodeString
& text
,
187 UErrorCode
& status
) const;
190 * Return a single pattern string generated by combining the patterns for the
191 * date and time formatters associated with this object.
192 * @param result Output param to receive the pattern.
193 * @return A reference to 'result'.
194 * @internal ICU 4.2 technology preview
196 virtual UnicodeString
& toPattern(UnicodeString
& result
, UErrorCode
& status
) const;
199 * Get the date pattern for the the date formatter associated with this object.
200 * @param result Output param to receive the date pattern.
201 * @return A reference to 'result'.
202 * @internal ICU 4.2 technology preview
204 virtual UnicodeString
& toPatternDate(UnicodeString
& result
, UErrorCode
& status
) const;
207 * Get the time pattern for the the time formatter associated with this object.
208 * @param result Output param to receive the time pattern.
209 * @return A reference to 'result'.
210 * @internal ICU 4.2 technology preview
212 virtual UnicodeString
& toPatternTime(UnicodeString
& result
, UErrorCode
& status
) const;
215 * Apply the given unlocalized date & time pattern strings to this relative date format.
216 * (i.e., after this call, this formatter will format dates and times according to
219 * @param datePattern The date pattern to be applied.
220 * @param timePattern The time pattern to be applied.
221 * @internal ICU 4.2 technology preview
223 virtual void applyPatterns(const UnicodeString
& datePattern
, const UnicodeString
& timePattern
, UErrorCode
&status
);
226 * Gets the date/time formatting symbols (this is an object carrying
227 * the various strings and other symbols used in formatting: e.g., month
228 * names and abbreviations, time zone names, AM/PM strings, etc.)
229 * @return a copy of the date-time formatting data associated
230 * with this date-time formatter.
233 virtual const DateFormatSymbols
* getDateFormatSymbols(void) const;
237 SimpleDateFormat
*fDateTimeFormatter
;
238 UnicodeString fDatePattern
;
239 UnicodeString fTimePattern
;
240 MessageFormat
*fCombinedFormat
; // the {0} {1} format.
242 UDateFormatStyle fDateStyle
;
245 int32_t fDayMin
; // day id of lowest #
246 int32_t fDayMax
; // day id of highest #
247 int32_t fDatesLen
; // Length of array
248 URelativeString
*fDates
; // array of strings
252 * Get the string at a specific offset.
253 * @param day day offset ( -1, 0, 1, etc.. )
254 * @param len on output, length of string.
255 * @return the string, or NULL if none at that location.
257 const UChar
*getStringForDay(int32_t day
, int32_t &len
, UErrorCode
&status
) const;
260 * Load the Date string array
262 void loadDates(UErrorCode
&status
);
265 * @return the number of days in "until-now"
267 static int32_t dayDifference(Calendar
&until
, UErrorCode
&status
);
270 * initializes fCalendar from parameters. Returns fCalendar as a convenience.
271 * @param adoptZone Zone to be adopted, or NULL for TimeZone::createDefault().
272 * @param locale Locale of the calendar
273 * @param status Error code
274 * @return the newly constructed fCalendar
277 Calendar
* initializeCalendar(TimeZone
* adoptZone
, const Locale
& locale
, UErrorCode
& status
);
281 * Return the class ID for this class. This is useful only for comparing to
282 * a return value from getDynamicClassID(). For example:
284 * . Base* polymorphic_pointer = createPolymorphicObject();
285 * . if (polymorphic_pointer->getDynamicClassID() ==
286 * . erived::getStaticClassID()) ...
288 * @return The class ID for all objects of this class.
291 U_I18N_API
static UClassID U_EXPORT2
getStaticClassID(void);
294 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
295 * method is to implement a simple version of RTTI, since not all C++
296 * compilers support genuine RTTI. Polymorphic operator==() and clone()
297 * methods call this method.
299 * @return The class ID for this object. All objects of a
300 * given class have the same class ID. Objects of
301 * other classes have different class IDs.
304 virtual UClassID
getDynamicClassID(void) const;
310 #endif /* #if !UCONFIG_NO_FORMATTING */