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