/src/icu/source/i18n/unicode/reldatefmt.h
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | // © 2016 and later: Unicode, Inc. and others.  | 
2  |  | // License & terms of use: http://www.unicode.org/copyright.html  | 
3  |  | /*  | 
4  |  | *****************************************************************************  | 
5  |  | * Copyright (C) 2014-2016, International Business Machines Corporation and  | 
6  |  | * others.  | 
7  |  | * All Rights Reserved.  | 
8  |  | *****************************************************************************  | 
9  |  | *  | 
10  |  | * File RELDATEFMT.H  | 
11  |  | *****************************************************************************  | 
12  |  | */  | 
13  |  |  | 
14  |  | #ifndef __RELDATEFMT_H  | 
15  |  | #define __RELDATEFMT_H  | 
16  |  |  | 
17  |  | #include "unicode/utypes.h"  | 
18  |  |  | 
19  |  | #if U_SHOW_CPLUSPLUS_API  | 
20  |  |  | 
21  |  | #include "unicode/uobject.h"  | 
22  |  | #include "unicode/udisplaycontext.h"  | 
23  |  | #include "unicode/ureldatefmt.h"  | 
24  |  | #include "unicode/locid.h"  | 
25  |  | #include "unicode/formattedvalue.h"  | 
26  |  |  | 
27  |  | /**  | 
28  |  |  * \file  | 
29  |  |  * \brief C++ API: Formats relative dates such as "1 day ago" or "tomorrow"  | 
30  |  |  */  | 
31  |  |  | 
32  |  | #if !UCONFIG_NO_FORMATTING  | 
33  |  |  | 
34  |  | /**  | 
35  |  |  * Represents the unit for formatting a relative date. e.g "in 5 days"  | 
36  |  |  * or "in 3 months"  | 
37  |  |  * @stable ICU 53  | 
38  |  |  */  | 
39  |  | typedef enum UDateRelativeUnit { | 
40  |  |  | 
41  |  |     /**  | 
42  |  |      * Seconds  | 
43  |  |      * @stable ICU 53  | 
44  |  |      */  | 
45  |  |     UDAT_RELATIVE_SECONDS,  | 
46  |  |  | 
47  |  |     /**  | 
48  |  |      * Minutes  | 
49  |  |      * @stable ICU 53  | 
50  |  |      */  | 
51  |  |     UDAT_RELATIVE_MINUTES,  | 
52  |  |  | 
53  |  |     /**  | 
54  |  |      * Hours  | 
55  |  |      * @stable ICU 53  | 
56  |  |      */  | 
57  |  |     UDAT_RELATIVE_HOURS,  | 
58  |  |  | 
59  |  |     /**  | 
60  |  |      * Days  | 
61  |  |      * @stable ICU 53  | 
62  |  |      */  | 
63  |  |     UDAT_RELATIVE_DAYS,  | 
64  |  |  | 
65  |  |     /**  | 
66  |  |      * Weeks  | 
67  |  |      * @stable ICU 53  | 
68  |  |      */  | 
69  |  |     UDAT_RELATIVE_WEEKS,  | 
70  |  |  | 
71  |  |     /**  | 
72  |  |      * Months  | 
73  |  |      * @stable ICU 53  | 
74  |  |      */  | 
75  |  |     UDAT_RELATIVE_MONTHS,  | 
76  |  |  | 
77  |  |     /**  | 
78  |  |      * Years  | 
79  |  |      * @stable ICU 53  | 
80  |  |      */  | 
81  |  |     UDAT_RELATIVE_YEARS,  | 
82  |  |  | 
83  |  | #ifndef U_HIDE_DEPRECATED_API  | 
84  |  |     /**  | 
85  |  |      * One more than the highest normal UDateRelativeUnit value.  | 
86  |  |      * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.  | 
87  |  |      */  | 
88  |  |     UDAT_RELATIVE_UNIT_COUNT  | 
89  |  | #endif  // U_HIDE_DEPRECATED_API  | 
90  |  | } UDateRelativeUnit;  | 
91  |  |  | 
92  |  | /**  | 
93  |  |  * Represents an absolute unit.  | 
94  |  |  * @stable ICU 53  | 
95  |  |  */  | 
96  |  | typedef enum UDateAbsoluteUnit { | 
97  |  |  | 
98  |  |     // Days of week have to remain together and in order from Sunday to  | 
99  |  |     // Saturday.  | 
100  |  |     /**  | 
101  |  |      * Sunday  | 
102  |  |      * @stable ICU 53  | 
103  |  |      */  | 
104  |  |     UDAT_ABSOLUTE_SUNDAY,  | 
105  |  |  | 
106  |  |     /**  | 
107  |  |      * Monday  | 
108  |  |      * @stable ICU 53  | 
109  |  |      */  | 
110  |  |     UDAT_ABSOLUTE_MONDAY,  | 
111  |  |  | 
112  |  |     /**  | 
113  |  |      * Tuesday  | 
114  |  |      * @stable ICU 53  | 
115  |  |      */  | 
116  |  |     UDAT_ABSOLUTE_TUESDAY,  | 
117  |  |  | 
118  |  |     /**  | 
119  |  |      * Wednesday  | 
120  |  |      * @stable ICU 53  | 
121  |  |      */  | 
122  |  |     UDAT_ABSOLUTE_WEDNESDAY,  | 
123  |  |  | 
124  |  |     /**  | 
125  |  |      * Thursday  | 
126  |  |      * @stable ICU 53  | 
127  |  |      */  | 
128  |  |     UDAT_ABSOLUTE_THURSDAY,  | 
129  |  |  | 
130  |  |     /**  | 
131  |  |      * Friday  | 
132  |  |      * @stable ICU 53  | 
133  |  |      */  | 
134  |  |     UDAT_ABSOLUTE_FRIDAY,  | 
135  |  |  | 
136  |  |     /**  | 
137  |  |      * Saturday  | 
138  |  |      * @stable ICU 53  | 
139  |  |      */  | 
140  |  |     UDAT_ABSOLUTE_SATURDAY,  | 
141  |  |  | 
142  |  |     /**  | 
143  |  |      * Day  | 
144  |  |      * @stable ICU 53  | 
145  |  |      */  | 
146  |  |     UDAT_ABSOLUTE_DAY,  | 
147  |  |  | 
148  |  |     /**  | 
149  |  |      * Week  | 
150  |  |      * @stable ICU 53  | 
151  |  |      */  | 
152  |  |     UDAT_ABSOLUTE_WEEK,  | 
153  |  |  | 
154  |  |     /**  | 
155  |  |      * Month  | 
156  |  |      * @stable ICU 53  | 
157  |  |      */  | 
158  |  |     UDAT_ABSOLUTE_MONTH,  | 
159  |  |  | 
160  |  |     /**  | 
161  |  |      * Year  | 
162  |  |      * @stable ICU 53  | 
163  |  |      */  | 
164  |  |     UDAT_ABSOLUTE_YEAR,  | 
165  |  |  | 
166  |  |     /**  | 
167  |  |      * Now  | 
168  |  |      * @stable ICU 53  | 
169  |  |      */  | 
170  |  |     UDAT_ABSOLUTE_NOW,  | 
171  |  |  | 
172  |  |     /**  | 
173  |  |      * Quarter  | 
174  |  |      * @stable ICU 63  | 
175  |  |      */  | 
176  |  |     UDAT_ABSOLUTE_QUARTER,  | 
177  |  |  | 
178  |  |     /**  | 
179  |  |      * Hour  | 
180  |  |      * @stable ICU 65  | 
181  |  |      */  | 
182  |  |     UDAT_ABSOLUTE_HOUR,  | 
183  |  |  | 
184  |  |     /**  | 
185  |  |      * Minute  | 
186  |  |      * @stable ICU 65  | 
187  |  |      */  | 
188  |  |     UDAT_ABSOLUTE_MINUTE,  | 
189  |  |  | 
190  |  | #ifndef U_HIDE_DEPRECATED_API  | 
191  |  |     /**  | 
192  |  |      * One more than the highest normal UDateAbsoluteUnit value.  | 
193  |  |      * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.  | 
194  |  |      */  | 
195  |  |     UDAT_ABSOLUTE_UNIT_COUNT = UDAT_ABSOLUTE_NOW + 4  | 
196  |  | #endif  // U_HIDE_DEPRECATED_API  | 
197  |  | } UDateAbsoluteUnit;  | 
198  |  |  | 
199  |  | /**  | 
200  |  |  * Represents a direction for an absolute unit e.g "Next Tuesday"  | 
201  |  |  * or "Last Tuesday"  | 
202  |  |  * @stable ICU 53  | 
203  |  |  */  | 
204  |  | typedef enum UDateDirection { | 
205  |  |  | 
206  |  |     /**  | 
207  |  |      * Two before. Not fully supported in every locale.  | 
208  |  |      * @stable ICU 53  | 
209  |  |      */  | 
210  |  |     UDAT_DIRECTION_LAST_2,  | 
211  |  |  | 
212  |  |     /**  | 
213  |  |      * Last  | 
214  |  |      * @stable ICU 53  | 
215  |  |      */  | 
216  |  |     UDAT_DIRECTION_LAST,  | 
217  |  |  | 
218  |  |     /**  | 
219  |  |      * This  | 
220  |  |      * @stable ICU 53  | 
221  |  |      */  | 
222  |  |     UDAT_DIRECTION_THIS,  | 
223  |  |  | 
224  |  |     /**  | 
225  |  |      * Next  | 
226  |  |      * @stable ICU 53  | 
227  |  |      */  | 
228  |  |     UDAT_DIRECTION_NEXT,  | 
229  |  |  | 
230  |  |     /**  | 
231  |  |      * Two after. Not fully supported in every locale.  | 
232  |  |      * @stable ICU 53  | 
233  |  |      */  | 
234  |  |     UDAT_DIRECTION_NEXT_2,  | 
235  |  |  | 
236  |  |     /**  | 
237  |  |      * Plain, which means the absence of a qualifier.  | 
238  |  |      * @stable ICU 53  | 
239  |  |      */  | 
240  |  |     UDAT_DIRECTION_PLAIN,  | 
241  |  |  | 
242  |  | #ifndef U_HIDE_DEPRECATED_API  | 
243  |  |     /**  | 
244  |  |      * One more than the highest normal UDateDirection value.  | 
245  |  |      * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.  | 
246  |  |      */  | 
247  |  |     UDAT_DIRECTION_COUNT  | 
248  |  | #endif  // U_HIDE_DEPRECATED_API  | 
249  |  | } UDateDirection;  | 
250  |  |  | 
251  |  | #if !UCONFIG_NO_BREAK_ITERATION  | 
252  |  |  | 
253  |  | U_NAMESPACE_BEGIN  | 
254  |  |  | 
255  |  | class BreakIterator;  | 
256  |  | class RelativeDateTimeCacheData;  | 
257  |  | class SharedNumberFormat;  | 
258  |  | class SharedPluralRules;  | 
259  |  | class SharedBreakIterator;  | 
260  |  | class NumberFormat;  | 
261  |  | class UnicodeString;  | 
262  |  | class FormattedRelativeDateTime;  | 
263  |  | class FormattedRelativeDateTimeData;  | 
264  |  |  | 
265  |  | /**  | 
266  |  |  * An immutable class containing the result of a relative datetime formatting operation.  | 
267  |  |  *  | 
268  |  |  * Instances of this class are immutable and thread-safe.  | 
269  |  |  *  | 
270  |  |  * Not intended for public subclassing.  | 
271  |  |  *  | 
272  |  |  * @stable ICU 64  | 
273  |  |  */  | 
274  |  | class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue { | 
275  |  |   public:  | 
276  |  |     /**  | 
277  |  |      * Default constructor; makes an empty FormattedRelativeDateTime.  | 
278  |  |      * @stable ICU 64  | 
279  |  |      */  | 
280  | 0  |     FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {} | 
281  |  |  | 
282  |  |     /**  | 
283  |  |      * Move constructor: Leaves the source FormattedRelativeDateTime in an undefined state.  | 
284  |  |      * @stable ICU 64  | 
285  |  |      */  | 
286  |  |     FormattedRelativeDateTime(FormattedRelativeDateTime&& src) U_NOEXCEPT;  | 
287  |  |  | 
288  |  |     /**  | 
289  |  |      * Destruct an instance of FormattedRelativeDateTime.  | 
290  |  |      * @stable ICU 64  | 
291  |  |      */  | 
292  |  |     virtual ~FormattedRelativeDateTime() U_OVERRIDE;  | 
293  |  |  | 
294  |  |     /** Copying not supported; use move constructor instead. */  | 
295  |  |     FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete;  | 
296  |  |  | 
297  |  |     /** Copying not supported; use move assignment instead. */  | 
298  |  |     FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;  | 
299  |  |  | 
300  |  |     /**  | 
301  |  |      * Move assignment: Leaves the source FormattedRelativeDateTime in an undefined state.  | 
302  |  |      * @stable ICU 64  | 
303  |  |      */  | 
304  |  |     FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) U_NOEXCEPT;  | 
305  |  |  | 
306  |  |     /** @copydoc FormattedValue::toString() */  | 
307  |  |     UnicodeString toString(UErrorCode& status) const U_OVERRIDE;  | 
308  |  |  | 
309  |  |     /** @copydoc FormattedValue::toTempString() */  | 
310  |  |     UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;  | 
311  |  |  | 
312  |  |     /** @copydoc FormattedValue::appendTo() */  | 
313  |  |     Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;  | 
314  |  |  | 
315  |  |     /** @copydoc FormattedValue::nextPosition() */  | 
316  |  |     UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;  | 
317  |  |  | 
318  |  |   private:  | 
319  |  |     FormattedRelativeDateTimeData *fData;  | 
320  |  |     UErrorCode fErrorCode;  | 
321  |  |     explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)  | 
322  | 0  |         : fData(results), fErrorCode(U_ZERO_ERROR) {} | 
323  |  |     explicit FormattedRelativeDateTime(UErrorCode errorCode)  | 
324  | 0  |         : fData(nullptr), fErrorCode(errorCode) {} | 
325  |  |     friend class RelativeDateTimeFormatter;  | 
326  |  | };  | 
327  |  |  | 
328  |  | /**  | 
329  |  |  * Formats simple relative dates. There are two types of relative dates that  | 
330  |  |  * it handles:  | 
331  |  |  * <ul>  | 
332  |  |  *   <li>relative dates with a quantity e.g "in 5 days"</li>  | 
333  |  |  *   <li>relative dates without a quantity e.g "next Tuesday"</li>  | 
334  |  |  * </ul>  | 
335  |  |  * <p>  | 
336  |  |  * This API is very basic and is intended to be a building block for more  | 
337  |  |  * fancy APIs. The caller tells it exactly what to display in a locale  | 
338  |  |  * independent way. While this class automatically provides the correct plural  | 
339  |  |  * forms, the grammatical form is otherwise as neutral as possible. It is the  | 
340  |  |  * caller's responsibility to handle cut-off logic such as deciding between  | 
341  |  |  * displaying "in 7 days" or "in 1 week." This API supports relative dates  | 
342  |  |  * involving one single unit. This API does not support relative dates  | 
343  |  |  * involving compound units,  | 
344  |  |  * e.g "in 5 days and 4 hours" nor does it support parsing.  | 
345  |  |  * <p>  | 
346  |  |  * This class is mostly thread safe and immutable with the following caveats:  | 
347  |  |  * 1. The assignment operator violates Immutability. It must not be used  | 
348  |  |  *    concurrently with other operations.  | 
349  |  |  * 2. Caller must not hold onto adopted pointers.  | 
350  |  |  * <p>  | 
351  |  |  * This class is not intended for public subclassing.  | 
352  |  |  * <p>  | 
353  |  |  * Here are some examples of use:  | 
354  |  |  * <blockquote>  | 
355  |  |  * <pre>  | 
356  |  |  * UErrorCode status = U_ZERO_ERROR;  | 
357  |  |  * UnicodeString appendTo;  | 
358  |  |  * RelativeDateTimeFormatter fmt(status);  | 
359  |  |  * // Appends "in 1 day"  | 
360  |  |  * fmt.format(  | 
361  |  |  *     1, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);  | 
362  |  |  * // Appends "in 3 days"  | 
363  |  |  * fmt.format(  | 
364  |  |  *     3, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);  | 
365  |  |  * // Appends "3.2 years ago"  | 
366  |  |  * fmt.format(  | 
367  |  |  *     3.2, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, appendTo, status);  | 
368  |  |  * // Appends "last Sunday"  | 
369  |  |  * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, appendTo, status);  | 
370  |  |  * // Appends "this Sunday"  | 
371  |  |  * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, appendTo, status);  | 
372  |  |  * // Appends "next Sunday"  | 
373  |  |  * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, appendTo, status);  | 
374  |  |  * // Appends "Sunday"  | 
375  |  |  * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, appendTo, status);  | 
376  |  |  *  | 
377  |  |  * // Appends "yesterday"  | 
378  |  |  * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, appendTo, status);  | 
379  |  |  * // Appends "today"  | 
380  |  |  * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, appendTo, status);  | 
381  |  |  * // Appends "tomorrow"  | 
382  |  |  * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, appendTo, status);  | 
383  |  |  * // Appends "now"  | 
384  |  |  * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, appendTo, status);  | 
385  |  |  *  | 
386  |  |  * </pre>  | 
387  |  |  * </blockquote>  | 
388  |  |  * <p>  | 
389  |  |  * In the future, we may add more forms, such as abbreviated/short forms  | 
390  |  |  * (3 secs ago), and relative day periods ("yesterday afternoon"), etc. | 
391  |  |  *  | 
392  |  |  * The RelativeDateTimeFormatter class is not intended for public subclassing.  | 
393  |  |  *  | 
394  |  |  * @stable ICU 53  | 
395  |  |  */  | 
396  |  | class U_I18N_API RelativeDateTimeFormatter : public UObject { | 
397  |  | public:  | 
398  |  |  | 
399  |  |     /**  | 
400  |  |      * Create RelativeDateTimeFormatter with default locale.  | 
401  |  |      * @stable ICU 53  | 
402  |  |      */  | 
403  |  |     RelativeDateTimeFormatter(UErrorCode& status);  | 
404  |  |  | 
405  |  |     /**  | 
406  |  |      * Create RelativeDateTimeFormatter with given locale.  | 
407  |  |      * @stable ICU 53  | 
408  |  |      */  | 
409  |  |     RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);  | 
410  |  |  | 
411  |  |     /**  | 
412  |  |      * Create RelativeDateTimeFormatter with given locale and NumberFormat.  | 
413  |  |      *  | 
414  |  |      * @param locale the locale  | 
415  |  |      * @param nfToAdopt Constructed object takes ownership of this pointer.  | 
416  |  |      *   It is an error for caller to delete this pointer or change its  | 
417  |  |      *   contents after calling this constructor.  | 
418  |  |      * @param status Any error is returned here.  | 
419  |  |      * @stable ICU 53  | 
420  |  |      */  | 
421  |  |     RelativeDateTimeFormatter(  | 
422  |  |         const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);  | 
423  |  |  | 
424  |  |     /**  | 
425  |  |      * Create RelativeDateTimeFormatter with given locale, NumberFormat,  | 
426  |  |      * and capitalization context.  | 
427  |  |      *  | 
428  |  |      * @param locale the locale  | 
429  |  |      * @param nfToAdopt Constructed object takes ownership of this pointer.  | 
430  |  |      *   It is an error for caller to delete this pointer or change its  | 
431  |  |      *   contents after calling this constructor. Caller may pass NULL for  | 
432  |  |      *   this argument if they want default number format behavior.  | 
433  |  |      * @param style the format style. The UDAT_RELATIVE bit field has no effect.  | 
434  |  |      * @param capitalizationContext A value from UDisplayContext that pertains to  | 
435  |  |      * capitalization.  | 
436  |  |      * @param status Any error is returned here.  | 
437  |  |      * @stable ICU 54  | 
438  |  |      */  | 
439  |  |     RelativeDateTimeFormatter(  | 
440  |  |             const Locale& locale,  | 
441  |  |             NumberFormat *nfToAdopt,  | 
442  |  |             UDateRelativeDateTimeFormatterStyle style,  | 
443  |  |             UDisplayContext capitalizationContext,  | 
444  |  |             UErrorCode& status);  | 
445  |  |  | 
446  |  |     /**  | 
447  |  |      * Copy constructor.  | 
448  |  |      * @stable ICU 53  | 
449  |  |      */  | 
450  |  |     RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);  | 
451  |  |  | 
452  |  |     /**  | 
453  |  |      * Assignment operator.  | 
454  |  |      * @stable ICU 53  | 
455  |  |      */  | 
456  |  |     RelativeDateTimeFormatter& operator=(  | 
457  |  |             const RelativeDateTimeFormatter& other);  | 
458  |  |  | 
459  |  |     /**  | 
460  |  |      * Destructor.  | 
461  |  |      * @stable ICU 53  | 
462  |  |      */  | 
463  |  |     virtual ~RelativeDateTimeFormatter();  | 
464  |  |  | 
465  |  |     /**  | 
466  |  |      * Formats a relative date with a quantity such as "in 5 days" or  | 
467  |  |      * "3 months ago"  | 
468  |  |      *  | 
469  |  |      * This method returns a String. To get more information about the  | 
470  |  |      * formatting result, use formatToValue().  | 
471  |  |      *  | 
472  |  |      * @param quantity The numerical amount e.g 5. This value is formatted  | 
473  |  |      * according to this object's NumberFormat object.  | 
474  |  |      * @param direction NEXT means a future relative date; LAST means a past  | 
475  |  |      * relative date. If direction is anything else, this method sets  | 
476  |  |      * status to U_ILLEGAL_ARGUMENT_ERROR.  | 
477  |  |      * @param unit the unit e.g day? month? year?  | 
478  |  |      * @param appendTo The string to which the formatted result will be  | 
479  |  |      *  appended  | 
480  |  |      * @param status ICU error code returned here.  | 
481  |  |      * @return appendTo  | 
482  |  |      * @stable ICU 53  | 
483  |  |      */  | 
484  |  |     UnicodeString& format(  | 
485  |  |             double quantity,  | 
486  |  |             UDateDirection direction,  | 
487  |  |             UDateRelativeUnit unit,  | 
488  |  |             UnicodeString& appendTo,  | 
489  |  |             UErrorCode& status) const;  | 
490  |  |  | 
491  |  |     /**  | 
492  |  |      * Formats a relative date with a quantity such as "in 5 days" or  | 
493  |  |      * "3 months ago"  | 
494  |  |      *  | 
495  |  |      * This method returns a FormattedRelativeDateTime, which exposes more  | 
496  |  |      * information than the String returned by format().  | 
497  |  |      *  | 
498  |  |      * @param quantity The numerical amount e.g 5. This value is formatted  | 
499  |  |      * according to this object's NumberFormat object.  | 
500  |  |      * @param direction NEXT means a future relative date; LAST means a past  | 
501  |  |      * relative date. If direction is anything else, this method sets  | 
502  |  |      * status to U_ILLEGAL_ARGUMENT_ERROR.  | 
503  |  |      * @param unit the unit e.g day? month? year?  | 
504  |  |      * @param status ICU error code returned here.  | 
505  |  |      * @return The formatted relative datetime  | 
506  |  |      * @stable ICU 64  | 
507  |  |      */  | 
508  |  |     FormattedRelativeDateTime formatToValue(  | 
509  |  |             double quantity,  | 
510  |  |             UDateDirection direction,  | 
511  |  |             UDateRelativeUnit unit,  | 
512  |  |             UErrorCode& status) const;  | 
513  |  |  | 
514  |  |     /**  | 
515  |  |      * Formats a relative date without a quantity.  | 
516  |  |      *  | 
517  |  |      * This method returns a String. To get more information about the  | 
518  |  |      * formatting result, use formatToValue().  | 
519  |  |      *  | 
520  |  |      * @param direction NEXT, LAST, THIS, etc.  | 
521  |  |      * @param unit e.g SATURDAY, DAY, MONTH  | 
522  |  |      * @param appendTo The string to which the formatted result will be  | 
523  |  |      *  appended. If the value of direction is documented as not being fully  | 
524  |  |      *  supported in all locales then this method leaves appendTo unchanged if  | 
525  |  |      *  no format string is available.  | 
526  |  |      * @param status ICU error code returned here.  | 
527  |  |      * @return appendTo  | 
528  |  |      * @stable ICU 53  | 
529  |  |      */  | 
530  |  |     UnicodeString& format(  | 
531  |  |             UDateDirection direction,  | 
532  |  |             UDateAbsoluteUnit unit,  | 
533  |  |             UnicodeString& appendTo,  | 
534  |  |             UErrorCode& status) const;  | 
535  |  |  | 
536  |  |     /**  | 
537  |  |      * Formats a relative date without a quantity.  | 
538  |  |      *  | 
539  |  |      * This method returns a FormattedRelativeDateTime, which exposes more  | 
540  |  |      * information than the String returned by format().  | 
541  |  |      *  | 
542  |  |      * If the string is not available in the requested locale, the return  | 
543  |  |      * value will be empty (calling toString will give an empty string).  | 
544  |  |      *  | 
545  |  |      * @param direction NEXT, LAST, THIS, etc.  | 
546  |  |      * @param unit e.g SATURDAY, DAY, MONTH  | 
547  |  |      * @param status ICU error code returned here.  | 
548  |  |      * @return The formatted relative datetime  | 
549  |  |      * @stable ICU 64  | 
550  |  |      */  | 
551  |  |     FormattedRelativeDateTime formatToValue(  | 
552  |  |             UDateDirection direction,  | 
553  |  |             UDateAbsoluteUnit unit,  | 
554  |  |             UErrorCode& status) const;  | 
555  |  |  | 
556  |  |     /**  | 
557  |  |      * Format a combination of URelativeDateTimeUnit and numeric offset  | 
558  |  |      * using a numeric style, e.g. "1 week ago", "in 1 week",  | 
559  |  |      * "5 weeks ago", "in 5 weeks".  | 
560  |  |      *  | 
561  |  |      * This method returns a String. To get more information about the  | 
562  |  |      * formatting result, use formatNumericToValue().  | 
563  |  |      *   | 
564  |  |      * @param offset    The signed offset for the specified unit. This  | 
565  |  |      *                  will be formatted according to this object's  | 
566  |  |      *                  NumberFormat object.  | 
567  |  |      * @param unit      The unit to use when formatting the relative  | 
568  |  |      *                  date, e.g. UDAT_REL_UNIT_WEEK,  | 
569  |  |      *                  UDAT_REL_UNIT_FRIDAY.  | 
570  |  |      * @param appendTo  The string to which the formatted result will be  | 
571  |  |      *                  appended.  | 
572  |  |      * @param status    ICU error code returned here.  | 
573  |  |      * @return          appendTo  | 
574  |  |      * @stable ICU 57  | 
575  |  |      */  | 
576  |  |     UnicodeString& formatNumeric(  | 
577  |  |             double offset,  | 
578  |  |             URelativeDateTimeUnit unit,  | 
579  |  |             UnicodeString& appendTo,  | 
580  |  |             UErrorCode& status) const;  | 
581  |  |  | 
582  |  |     /**  | 
583  |  |      * Format a combination of URelativeDateTimeUnit and numeric offset  | 
584  |  |      * using a numeric style, e.g. "1 week ago", "in 1 week",  | 
585  |  |      * "5 weeks ago", "in 5 weeks".  | 
586  |  |      *  | 
587  |  |      * This method returns a FormattedRelativeDateTime, which exposes more  | 
588  |  |      * information than the String returned by formatNumeric().  | 
589  |  |      *   | 
590  |  |      * @param offset    The signed offset for the specified unit. This  | 
591  |  |      *                  will be formatted according to this object's  | 
592  |  |      *                  NumberFormat object.  | 
593  |  |      * @param unit      The unit to use when formatting the relative  | 
594  |  |      *                  date, e.g. UDAT_REL_UNIT_WEEK,  | 
595  |  |      *                  UDAT_REL_UNIT_FRIDAY.  | 
596  |  |      * @param status    ICU error code returned here.  | 
597  |  |      * @return          The formatted relative datetime  | 
598  |  |      * @stable ICU 64  | 
599  |  |      */  | 
600  |  |     FormattedRelativeDateTime formatNumericToValue(  | 
601  |  |             double offset,  | 
602  |  |             URelativeDateTimeUnit unit,  | 
603  |  |             UErrorCode& status) const;  | 
604  |  |  | 
605  |  |     /**  | 
606  |  |      * Format a combination of URelativeDateTimeUnit and numeric offset  | 
607  |  |      * using a text style if possible, e.g. "last week", "this week",  | 
608  |  |      * "next week", "yesterday", "tomorrow". Falls back to numeric  | 
609  |  |      * style if no appropriate text term is available for the specified  | 
610  |  |      * offset in the object's locale.  | 
611  |  |      *  | 
612  |  |      * This method returns a String. To get more information about the  | 
613  |  |      * formatting result, use formatToValue().  | 
614  |  |      *  | 
615  |  |      * @param offset    The signed offset for the specified unit.  | 
616  |  |      * @param unit      The unit to use when formatting the relative  | 
617  |  |      *                  date, e.g. UDAT_REL_UNIT_WEEK,  | 
618  |  |      *                  UDAT_REL_UNIT_FRIDAY.  | 
619  |  |      * @param appendTo  The string to which the formatted result will be  | 
620  |  |      *                  appended.  | 
621  |  |      * @param status    ICU error code returned here.  | 
622  |  |      * @return          appendTo  | 
623  |  |      * @stable ICU 57  | 
624  |  |      */  | 
625  |  |     UnicodeString& format(  | 
626  |  |             double offset,  | 
627  |  |             URelativeDateTimeUnit unit,  | 
628  |  |             UnicodeString& appendTo,  | 
629  |  |             UErrorCode& status) const;  | 
630  |  |  | 
631  |  |     /**  | 
632  |  |      * Format a combination of URelativeDateTimeUnit and numeric offset  | 
633  |  |      * using a text style if possible, e.g. "last week", "this week",  | 
634  |  |      * "next week", "yesterday", "tomorrow". Falls back to numeric  | 
635  |  |      * style if no appropriate text term is available for the specified  | 
636  |  |      * offset in the object's locale.  | 
637  |  |      *  | 
638  |  |      * This method returns a FormattedRelativeDateTime, which exposes more  | 
639  |  |      * information than the String returned by format().  | 
640  |  |      *  | 
641  |  |      * @param offset    The signed offset for the specified unit.  | 
642  |  |      * @param unit      The unit to use when formatting the relative  | 
643  |  |      *                  date, e.g. UDAT_REL_UNIT_WEEK,  | 
644  |  |      *                  UDAT_REL_UNIT_FRIDAY.  | 
645  |  |      * @param status    ICU error code returned here.  | 
646  |  |      * @return          The formatted relative datetime  | 
647  |  |      * @stable ICU 64  | 
648  |  |      */  | 
649  |  |     FormattedRelativeDateTime formatToValue(  | 
650  |  |             double offset,  | 
651  |  |             URelativeDateTimeUnit unit,  | 
652  |  |             UErrorCode& status) const;  | 
653  |  |  | 
654  |  |     /**  | 
655  |  |      * Combines a relative date string and a time string in this object's  | 
656  |  |      * locale. This is done with the same date-time separator used for the  | 
657  |  |      * default calendar in this locale.  | 
658  |  |      *  | 
659  |  |      * @param relativeDateString the relative date, e.g 'yesterday'  | 
660  |  |      * @param timeString the time e.g '3:45'  | 
661  |  |      * @param appendTo concatenated date and time appended here  | 
662  |  |      * @param status ICU error code returned here.  | 
663  |  |      * @return appendTo  | 
664  |  |      * @stable ICU 53  | 
665  |  |      */  | 
666  |  |     UnicodeString& combineDateAndTime(  | 
667  |  |             const UnicodeString& relativeDateString,  | 
668  |  |             const UnicodeString& timeString,  | 
669  |  |             UnicodeString& appendTo,  | 
670  |  |             UErrorCode& status) const;  | 
671  |  |  | 
672  |  |     /**  | 
673  |  |      * Returns the NumberFormat this object is using.  | 
674  |  |      *  | 
675  |  |      * @stable ICU 53  | 
676  |  |      */  | 
677  |  |     const NumberFormat& getNumberFormat() const;  | 
678  |  |  | 
679  |  |     /**  | 
680  |  |      * Returns the capitalization context.  | 
681  |  |      *  | 
682  |  |      * @stable ICU 54  | 
683  |  |      */  | 
684  |  |     UDisplayContext getCapitalizationContext() const;  | 
685  |  |  | 
686  |  |     /**  | 
687  |  |      * Returns the format style.  | 
688  |  |      *  | 
689  |  |      * @stable ICU 54  | 
690  |  |      */  | 
691  |  |     UDateRelativeDateTimeFormatterStyle getFormatStyle() const;  | 
692  |  |  | 
693  |  | private:  | 
694  |  |     const RelativeDateTimeCacheData* fCache;  | 
695  |  |     const SharedNumberFormat *fNumberFormat;  | 
696  |  |     const SharedPluralRules *fPluralRules;  | 
697  |  |     UDateRelativeDateTimeFormatterStyle fStyle;  | 
698  |  |     UDisplayContext fContext;  | 
699  |  |     const SharedBreakIterator *fOptBreakIterator;  | 
700  |  |     Locale fLocale;  | 
701  |  |     void init(  | 
702  |  |             NumberFormat *nfToAdopt,  | 
703  |  |             BreakIterator *brkIter,  | 
704  |  |             UErrorCode &status);  | 
705  |  |     UnicodeString& adjustForContext(UnicodeString &) const;  | 
706  |  |     UBool checkNoAdjustForContext(UErrorCode& status) const;  | 
707  |  |  | 
708  |  |     template<typename F, typename... Args>  | 
709  |  |     UnicodeString& doFormat(  | 
710  |  |             F callback,  | 
711  |  |             UnicodeString& appendTo,  | 
712  |  |             UErrorCode& status,  | 
713  |  |             Args... args) const;  | 
714  |  |  | 
715  |  |     template<typename F, typename... Args>  | 
716  |  |     FormattedRelativeDateTime doFormatToValue(  | 
717  |  |             F callback,  | 
718  |  |             UErrorCode& status,  | 
719  |  |             Args... args) const;  | 
720  |  |  | 
721  |  |     void formatImpl(  | 
722  |  |             double quantity,  | 
723  |  |             UDateDirection direction,  | 
724  |  |             UDateRelativeUnit unit,  | 
725  |  |             FormattedRelativeDateTimeData& output,  | 
726  |  |             UErrorCode& status) const;  | 
727  |  |     void formatAbsoluteImpl(  | 
728  |  |             UDateDirection direction,  | 
729  |  |             UDateAbsoluteUnit unit,  | 
730  |  |             FormattedRelativeDateTimeData& output,  | 
731  |  |             UErrorCode& status) const;  | 
732  |  |     void formatNumericImpl(  | 
733  |  |             double offset,  | 
734  |  |             URelativeDateTimeUnit unit,  | 
735  |  |             FormattedRelativeDateTimeData& output,  | 
736  |  |             UErrorCode& status) const;  | 
737  |  |     void formatRelativeImpl(  | 
738  |  |             double offset,  | 
739  |  |             URelativeDateTimeUnit unit,  | 
740  |  |             FormattedRelativeDateTimeData& output,  | 
741  |  |             UErrorCode& status) const;  | 
742  |  | };  | 
743  |  |  | 
744  |  | U_NAMESPACE_END  | 
745  |  |  | 
746  |  | #endif /* !UCONFIG_NO_BREAK_ITERATION */  | 
747  |  | #endif /* !UCONFIG_NO_FORMATTING */  | 
748  |  |  | 
749  |  | #endif /* U_SHOW_CPLUSPLUS_API */  | 
750  |  |  | 
751  |  | #endif /* __RELDATEFMT_H */  |