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