/src/icu/icu4c/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 | | U_NAMESPACE_BEGIN |
252 | | |
253 | | class BreakIterator; |
254 | | class RelativeDateTimeCacheData; |
255 | | class SharedNumberFormat; |
256 | | class SharedPluralRules; |
257 | | class SharedBreakIterator; |
258 | | class NumberFormat; |
259 | | class UnicodeString; |
260 | | class FormattedRelativeDateTime; |
261 | | class FormattedRelativeDateTimeData; |
262 | | |
263 | | /** |
264 | | * An immutable class containing the result of a relative datetime formatting operation. |
265 | | * |
266 | | * Instances of this class are immutable and thread-safe. |
267 | | * |
268 | | * Not intended for public subclassing. |
269 | | * |
270 | | * @stable ICU 64 |
271 | | */ |
272 | | class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue { |
273 | | public: |
274 | | /** |
275 | | * Default constructor; makes an empty FormattedRelativeDateTime. |
276 | | * @stable ICU 64 |
277 | | */ |
278 | 0 | FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {} |
279 | | |
280 | | /** |
281 | | * Move constructor: Leaves the source FormattedRelativeDateTime in an undefined state. |
282 | | * @stable ICU 64 |
283 | | */ |
284 | | FormattedRelativeDateTime(FormattedRelativeDateTime&& src) noexcept; |
285 | | |
286 | | /** |
287 | | * Destruct an instance of FormattedRelativeDateTime. |
288 | | * @stable ICU 64 |
289 | | */ |
290 | | virtual ~FormattedRelativeDateTime() override; |
291 | | |
292 | | /** Copying not supported; use move constructor instead. */ |
293 | | FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete; |
294 | | |
295 | | /** Copying not supported; use move assignment instead. */ |
296 | | FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete; |
297 | | |
298 | | /** |
299 | | * Move assignment: Leaves the source FormattedRelativeDateTime in an undefined state. |
300 | | * @stable ICU 64 |
301 | | */ |
302 | | FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) noexcept; |
303 | | |
304 | | /** @copydoc FormattedValue::toString() */ |
305 | | UnicodeString toString(UErrorCode& status) const override; |
306 | | |
307 | | /** @copydoc FormattedValue::toTempString() */ |
308 | | UnicodeString toTempString(UErrorCode& status) const override; |
309 | | |
310 | | /** @copydoc FormattedValue::appendTo() */ |
311 | | Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override; |
312 | | |
313 | | /** @copydoc FormattedValue::nextPosition() */ |
314 | | UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override; |
315 | | |
316 | | private: |
317 | | FormattedRelativeDateTimeData *fData; |
318 | | UErrorCode fErrorCode; |
319 | | explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results) |
320 | 5.86M | : fData(results), fErrorCode(U_ZERO_ERROR) {} |
321 | | explicit FormattedRelativeDateTime(UErrorCode errorCode) |
322 | 2.62M | : fData(nullptr), fErrorCode(errorCode) {} |
323 | | friend class RelativeDateTimeFormatter; |
324 | | }; |
325 | | |
326 | | /** |
327 | | * Formats simple relative dates. There are two types of relative dates that |
328 | | * it handles: |
329 | | * <ul> |
330 | | * <li>relative dates with a quantity e.g "in 5 days"</li> |
331 | | * <li>relative dates without a quantity e.g "next Tuesday"</li> |
332 | | * </ul> |
333 | | * <p> |
334 | | * This API is very basic and is intended to be a building block for more |
335 | | * fancy APIs. The caller tells it exactly what to display in a locale |
336 | | * independent way. While this class automatically provides the correct plural |
337 | | * forms, the grammatical form is otherwise as neutral as possible. It is the |
338 | | * caller's responsibility to handle cut-off logic such as deciding between |
339 | | * displaying "in 7 days" or "in 1 week." This API supports relative dates |
340 | | * involving one single unit. This API does not support relative dates |
341 | | * involving compound units, |
342 | | * e.g "in 5 days and 4 hours" nor does it support parsing. |
343 | | * <p> |
344 | | * This class is mostly thread safe and immutable with the following caveats: |
345 | | * 1. The assignment operator violates Immutability. It must not be used |
346 | | * concurrently with other operations. |
347 | | * 2. Caller must not hold onto adopted pointers. |
348 | | * <p> |
349 | | * This class is not intended for public subclassing. |
350 | | * <p> |
351 | | * Here are some examples of use: |
352 | | * <blockquote> |
353 | | * <pre> |
354 | | * UErrorCode status = U_ZERO_ERROR; |
355 | | * UnicodeString appendTo; |
356 | | * RelativeDateTimeFormatter fmt(status); |
357 | | * // Appends "in 1 day" |
358 | | * fmt.format( |
359 | | * 1, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status); |
360 | | * // Appends "in 3 days" |
361 | | * fmt.format( |
362 | | * 3, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status); |
363 | | * // Appends "3.2 years ago" |
364 | | * fmt.format( |
365 | | * 3.2, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, appendTo, status); |
366 | | * // Appends "last Sunday" |
367 | | * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, appendTo, status); |
368 | | * // Appends "this Sunday" |
369 | | * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, appendTo, status); |
370 | | * // Appends "next Sunday" |
371 | | * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, appendTo, status); |
372 | | * // Appends "Sunday" |
373 | | * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, appendTo, status); |
374 | | * |
375 | | * // Appends "yesterday" |
376 | | * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, appendTo, status); |
377 | | * // Appends "today" |
378 | | * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, appendTo, status); |
379 | | * // Appends "tomorrow" |
380 | | * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, appendTo, status); |
381 | | * // Appends "now" |
382 | | * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, appendTo, status); |
383 | | * |
384 | | * </pre> |
385 | | * </blockquote> |
386 | | * <p> |
387 | | * The UDateRelativeDateTimeFormatterStyle parameter allows selection of |
388 | | * different length styles: LONG ("3 seconds ago"), SHORT ("3 sec. ago"), |
389 | | * NARROW ("3s ago"). In the future, we may add more forms, such as |
390 | | * 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 nullptr 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 | | #if !UCONFIG_NO_BREAK_ITERATION |
700 | | const SharedBreakIterator *fOptBreakIterator; |
701 | | #else |
702 | | std::nullptr_t fOptBreakIterator = nullptr; |
703 | | #endif // !UCONFIG_NO_BREAK_ITERATION |
704 | | Locale fLocale; |
705 | | void init( |
706 | | NumberFormat *nfToAdopt, |
707 | | #if !UCONFIG_NO_BREAK_ITERATION |
708 | | BreakIterator *brkIter, |
709 | | #else |
710 | | std::nullptr_t, |
711 | | #endif // !UCONFIG_NO_BREAK_ITERATION |
712 | | UErrorCode &status); |
713 | | UnicodeString& adjustForContext(UnicodeString &) const; |
714 | | UBool checkNoAdjustForContext(UErrorCode& status) const; |
715 | | |
716 | | template<typename F, typename... Args> |
717 | | UnicodeString& doFormat( |
718 | | F callback, |
719 | | UnicodeString& appendTo, |
720 | | UErrorCode& status, |
721 | | Args... args) const; |
722 | | |
723 | | template<typename F, typename... Args> |
724 | | FormattedRelativeDateTime doFormatToValue( |
725 | | F callback, |
726 | | UErrorCode& status, |
727 | | Args... args) const; |
728 | | |
729 | | void formatImpl( |
730 | | double quantity, |
731 | | UDateDirection direction, |
732 | | UDateRelativeUnit unit, |
733 | | FormattedRelativeDateTimeData& output, |
734 | | UErrorCode& status) const; |
735 | | void formatAbsoluteImpl( |
736 | | UDateDirection direction, |
737 | | UDateAbsoluteUnit unit, |
738 | | FormattedRelativeDateTimeData& output, |
739 | | UErrorCode& status) const; |
740 | | void formatNumericImpl( |
741 | | double offset, |
742 | | URelativeDateTimeUnit unit, |
743 | | FormattedRelativeDateTimeData& output, |
744 | | UErrorCode& status) const; |
745 | | void formatRelativeImpl( |
746 | | double offset, |
747 | | URelativeDateTimeUnit unit, |
748 | | FormattedRelativeDateTimeData& output, |
749 | | UErrorCode& status) const; |
750 | | }; |
751 | | |
752 | | U_NAMESPACE_END |
753 | | |
754 | | #endif /* !UCONFIG_NO_FORMATTING */ |
755 | | |
756 | | #endif /* U_SHOW_CPLUSPLUS_API */ |
757 | | |
758 | | #endif /* __RELDATEFMT_H */ |