2 *******************************************************************************
3 * Copyright (C) 2007-2009, 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"
24 // forward declarations
27 // internal structure used for caching strings
28 struct URelativeString
;
31 * This class is normally accessed using the kRelative or k...Relative values of EStyle as parameters to DateFormat::createDateInstance.
34 * DateFormat *fullrelative = DateFormat::createDateInstance(DateFormat::kFullRelative, loc);
39 class RelativeDateFormat
: public DateFormat
{
41 RelativeDateFormat( UDateFormatStyle timeStyle
, UDateFormatStyle dateStyle
, const Locale
& locale
, UErrorCode
& status
);
48 RelativeDateFormat(const RelativeDateFormat
&);
51 * Assignment operator.
54 RelativeDateFormat
& operator=(const RelativeDateFormat
&);
60 virtual ~RelativeDateFormat();
63 * Clone this Format object polymorphically. The caller owns the result and
64 * should delete it when done.
65 * @return A copy of the object.
68 virtual Format
* clone(void) const;
71 * Return true if the given Format objects are semantically equal. Objects
72 * of different subclasses are considered unequal.
73 * @param other the object to be compared with.
74 * @return true if the given Format objects are semantically equal.
77 virtual UBool
operator==(const Format
& other
) const;
80 * Format a date or time, which is the standard millis since 24:00 GMT, Jan
81 * 1, 1970. Overrides DateFormat pure virtual method.
83 * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
84 * 1996.07.10 AD at 15:08:56 PDT
86 * @param cal Calendar set to the date and time to be formatted
87 * into a date/time string.
88 * @param appendTo Output parameter to receive result.
89 * Result is appended to existing contents.
90 * @param pos The formatting position. On input: an alignment field,
91 * if desired. On output: the offsets of the alignment field.
92 * @return Reference to 'appendTo' parameter.
95 virtual UnicodeString
& format( Calendar
& cal
,
96 UnicodeString
& appendTo
,
97 FieldPosition
& pos
) const;
100 * Format an object to produce a string. This method handles Formattable
101 * objects with a UDate type. If a the Formattable object type is not a Date,
102 * then it returns a failing UErrorCode.
104 * @param obj The object to format. Must be a Date.
105 * @param appendTo Output parameter to receive result.
106 * Result is appended to existing contents.
107 * @param pos On input: an alignment field, if desired.
108 * On output: the offsets of the alignment field.
109 * @param status Output param filled with success/failure status.
110 * @return Reference to 'appendTo' parameter.
113 virtual UnicodeString
& format(const Formattable
& obj
,
114 UnicodeString
& appendTo
,
116 UErrorCode
& status
) const;
120 * Parse a date/time string beginning at the given parse position. For
121 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
122 * that is equivalent to Date(837039928046).
124 * By default, parsing is lenient: If the input is not in the form used by
125 * this object's format method but can still be parsed as a date, then the
126 * parse succeeds. Clients may insist on strict adherence to the format by
127 * calling setLenient(false).
129 * @param text The date/time string to be parsed
130 * @param cal a Calendar set to the date and time to be formatted
131 * into a date/time string.
132 * @param pos On input, the position at which to start parsing; on
133 * output, the position at which parsing terminated, or the
134 * start position if the parse failed.
135 * @return A valid UDate if the input could be parsed.
138 virtual void parse( const UnicodeString
& text
,
140 ParsePosition
& pos
) const;
143 * Parse a date/time string starting at the given parse position. For
144 * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
145 * that is equivalent to Date(837039928046).
147 * By default, parsing is lenient: If the input is not in the form used by
148 * this object's format method but can still be parsed as a date, then the
149 * parse succeeds. Clients may insist on strict adherence to the format by
150 * calling setLenient(false).
152 * @see DateFormat::setLenient(boolean)
154 * @param text The date/time string to be parsed
155 * @param pos On input, the position at which to start parsing; on
156 * output, the position at which parsing terminated, or the
157 * start position if the parse failed.
158 * @return A valid UDate if the input could be parsed.
161 UDate
parse( const UnicodeString
& text
,
162 ParsePosition
& pos
) const;
166 * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT"
167 * will be parsed into a UDate that is equivalent to Date(837039928046).
168 * Parsing begins at the beginning of the string and proceeds as far as
169 * possible. Assuming no parse errors were encountered, this function
170 * doesn't return any information about how much of the string was consumed
171 * by the parsing. If you need that information, use the version of
172 * parse() that takes a ParsePosition.
174 * @param text The date/time string to be parsed
175 * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
176 * an error value if there was a parse error.
177 * @return A valid UDate if the input could be parsed.
180 virtual UDate
parse( const UnicodeString
& text
,
181 UErrorCode
& status
) const;
184 * Return a single pattern string generated by combining the patterns for the
185 * date and time formatters associated with this object.
186 * @param result Output param to receive the pattern.
187 * @return A reference to 'result'.
188 * @internal ICU 4.2 technology preview
190 virtual UnicodeString
& toPattern(UnicodeString
& result
, UErrorCode
& status
) const;
193 * Get the date pattern for the the date formatter associated with this object.
194 * @param result Output param to receive the date pattern.
195 * @return A reference to 'result'.
196 * @internal ICU 4.2 technology preview
198 virtual UnicodeString
& toPatternDate(UnicodeString
& result
, UErrorCode
& status
) const;
201 * Get the time pattern for the the time formatter associated with this object.
202 * @param result Output param to receive the time pattern.
203 * @return A reference to 'result'.
204 * @internal ICU 4.2 technology preview
206 virtual UnicodeString
& toPatternTime(UnicodeString
& result
, UErrorCode
& status
) const;
209 * Apply the given unlocalized date & time pattern strings to this relative date format.
210 * (i.e., after this call, this formatter will format dates and times according to
213 * @param datePattern The date pattern to be applied.
214 * @param timePattern The time pattern to be applied.
215 * @internal ICU 4.2 technology preview
217 virtual void applyPatterns(const UnicodeString
& datePattern
, const UnicodeString
& timePattern
, UErrorCode
&status
);
221 DateFormat
*fDateFormat
; // the held date format
222 DateFormat
*fTimeFormat
; // the held time format
223 MessageFormat
*fCombinedFormat
; // the {0} {1} format.
225 UDateFormatStyle fDateStyle
;
226 UDateFormatStyle fTimeStyle
;
229 int32_t fDayMin
; // day id of lowest #
230 int32_t fDayMax
; // day id of highest #
231 int32_t fDatesLen
; // Length of array
232 URelativeString
*fDates
; // array of strings
236 * Get the string at a specific offset.
237 * @param day day offset ( -1, 0, 1, etc.. )
238 * @param len on output, length of string.
239 * @return the string, or NULL if none at that location.
241 const UChar
*getStringForDay(int32_t day
, int32_t &len
, UErrorCode
&status
) const;
244 * Load the Date string array
246 void loadDates(UErrorCode
&status
);
249 * @return the number of days in "until-now"
251 static int32_t dayDifference(Calendar
&until
, UErrorCode
&status
);
254 * initializes fCalendar from parameters. Returns fCalendar as a convenience.
255 * @param adoptZone Zone to be adopted, or NULL for TimeZone::createDefault().
256 * @param locale Locale of the calendar
257 * @param status Error code
258 * @return the newly constructed fCalendar
261 Calendar
* initializeCalendar(TimeZone
* adoptZone
, const Locale
& locale
, UErrorCode
& status
);
265 * Return the class ID for this class. This is useful only for comparing to
266 * a return value from getDynamicClassID(). For example:
268 * . Base* polymorphic_pointer = createPolymorphicObject();
269 * . if (polymorphic_pointer->getDynamicClassID() ==
270 * . erived::getStaticClassID()) ...
272 * @return The class ID for all objects of this class.
275 U_I18N_API
static UClassID U_EXPORT2
getStaticClassID(void);
278 * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
279 * method is to implement a simple version of RTTI, since not all C++
280 * compilers support genuine RTTI. Polymorphic operator==() and clone()
281 * methods call this method.
283 * @return The class ID for this object. All objects of a
284 * given class have the same class ID. Objects of
285 * other classes have different class IDs.
288 virtual UClassID
getDynamicClassID(void) const;
294 #endif /* #if !UCONFIG_NO_FORMATTING */