2 *****************************************************************************
3 * Copyright (C) 2014-2015, International Business Machines Corporation and
6 *****************************************************************************
9 *****************************************************************************
12 #ifndef __RELDATEFMT_H
13 #define __RELDATEFMT_H
15 #include "unicode/utypes.h"
16 #include "unicode/uobject.h"
17 #include "unicode/udisplaycontext.h"
18 #include "unicode/locid.h"
22 * \brief C++ API: Formats relative dates such as "1 day ago" or "tomorrow"
25 #if !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION
27 #ifndef U_HIDE_DRAFT_API
30 * The formatting style
33 typedef enum UDateRelativeDateTimeFormatterStyle
{
36 * Everything spelled out.
42 * Abbreviations used when possible.
48 * Use the shortest possible form.
54 * The number of styles.
58 } UDateRelativeDateTimeFormatterStyle
;
60 #endif /* U_HIDE_DRAFT_API */
63 * Represents the unit for formatting a relative date. e.g "in 5 days"
67 typedef enum UDateRelativeUnit
{
73 UDAT_RELATIVE_SECONDS
,
79 UDAT_RELATIVE_MINUTES
,
103 UDAT_RELATIVE_MONTHS
,
112 * Count of items in this enum.
115 UDAT_RELATIVE_UNIT_COUNT
119 * Represents an absolute unit.
122 typedef enum UDateAbsoluteUnit
{
124 // Days of week have to remain together and in order from Sunday to
130 UDAT_ABSOLUTE_SUNDAY
,
136 UDAT_ABSOLUTE_MONDAY
,
142 UDAT_ABSOLUTE_TUESDAY
,
148 UDAT_ABSOLUTE_WEDNESDAY
,
154 UDAT_ABSOLUTE_THURSDAY
,
160 UDAT_ABSOLUTE_FRIDAY
,
166 UDAT_ABSOLUTE_SATURDAY
,
199 * Count of items in this enum.
202 UDAT_ABSOLUTE_UNIT_COUNT
206 * Represents a direction for an absolute unit e.g "Next Tuesday"
210 typedef enum UDateDirection
{
213 * Two before. Not fully supported in every locale.
216 UDAT_DIRECTION_LAST_2
,
237 * Two after. Not fully supported in every locale.
240 UDAT_DIRECTION_NEXT_2
,
243 * Plain, which means the absence of a qualifier.
246 UDAT_DIRECTION_PLAIN
,
249 * Count of items in this enum.
258 class RelativeDateTimeCacheData
;
259 class SharedNumberFormat
;
260 class SharedPluralRules
;
261 class SharedBreakIterator
;
266 * Formats simple relative dates. There are two types of relative dates that
269 * <li>relative dates with a quantity e.g "in 5 days"</li>
270 * <li>relative dates without a quantity e.g "next Tuesday"</li>
273 * This API is very basic and is intended to be a building block for more
274 * fancy APIs. The caller tells it exactly what to display in a locale
275 * independent way. While this class automatically provides the correct plural
276 * forms, the grammatical form is otherwise as neutral as possible. It is the
277 * caller's responsibility to handle cut-off logic such as deciding between
278 * displaying "in 7 days" or "in 1 week." This API supports relative dates
279 * involving one single unit. This API does not support relative dates
280 * involving compound units,
281 * e.g "in 5 days and 4 hours" nor does it support parsing.
283 * This class is mostly thread safe and immutable with the following caveats:
284 * 1. The assignment operator violates Immutability. It must not be used
285 * concurrently with other operations.
286 * 2. Caller must not hold onto adopted pointers.
288 * This class is not intended for public subclassing.
290 * Here are some examples of use:
293 * UErrorCode status = U_ZERO_ERROR;
294 * UnicodeString appendTo;
295 * RelativeDateTimeFormatter fmt(status);
296 * // Appends "in 1 day"
298 * 1, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);
299 * // Appends "in 3 days"
301 * 3, UDAT_DIRECTION_NEXT, UDAT_RELATIVE_DAYS, appendTo, status);
302 * // Appends "3.2 years ago"
304 * 3.2, UDAT_DIRECTION_LAST, UDAT_RELATIVE_YEARS, appendTo, status);
305 * // Appends "last Sunday"
306 * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
307 * // Appends "this Sunday"
308 * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
309 * // Appends "next Sunday"
310 * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
311 * // Appends "Sunday"
312 * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_SUNDAY, appendTo, status);
314 * // Appends "yesterday"
315 * fmt.format(UDAT_DIRECTION_LAST, UDAT_ABSOLUTE_DAY, appendTo, status);
317 * fmt.format(UDAT_DIRECTION_THIS, UDAT_ABSOLUTE_DAY, appendTo, status);
318 * // Appends "tomorrow"
319 * fmt.format(UDAT_DIRECTION_NEXT, UDAT_ABSOLUTE_DAY, appendTo, status);
321 * fmt.format(UDAT_DIRECTION_PLAIN, UDAT_ABSOLUTE_NOW, appendTo, status);
326 * In the future, we may add more forms, such as abbreviated/short forms
327 * (3 secs ago), and relative day periods ("yesterday afternoon"), etc.
329 * The RelativeDateTimeFormatter class is not intended for public subclassing.
333 class U_I18N_API RelativeDateTimeFormatter
: public UObject
{
337 * Create RelativeDateTimeFormatter with default locale.
340 RelativeDateTimeFormatter(UErrorCode
& status
);
343 * Create RelativeDateTimeFormatter with given locale.
346 RelativeDateTimeFormatter(const Locale
& locale
, UErrorCode
& status
);
349 * Create RelativeDateTimeFormatter with given locale and NumberFormat.
351 * @param locale the locale
352 * @param nfToAdopt Constructed object takes ownership of this pointer.
353 * It is an error for caller to delete this pointer or change its
354 * contents after calling this constructor.
355 * @status Any error is returned here.
358 RelativeDateTimeFormatter(
359 const Locale
& locale
, NumberFormat
*nfToAdopt
, UErrorCode
& status
);
361 #ifndef U_HIDE_DRAFT_API
363 * Create RelativeDateTimeFormatter with given locale, NumberFormat,
364 * and capitalization context.
366 * @param locale the locale
367 * @param nfToAdopt Constructed object takes ownership of this pointer.
368 * It is an error for caller to delete this pointer or change its
369 * contents after calling this constructor. Caller may pass NULL for
370 * this argument if they want default number format behavior.
371 * @param style the format style. The UDAT_RELATIVE bit field has no effect.
372 * @param capitalizationContext A value from UDisplayContext that pertains to
374 * @status Any error is returned here.
377 RelativeDateTimeFormatter(
378 const Locale
& locale
,
379 NumberFormat
*nfToAdopt
,
380 UDateRelativeDateTimeFormatterStyle style
,
381 UDisplayContext capitalizationContext
,
383 #endif /* U_HIDE_DRAFT_API */
389 RelativeDateTimeFormatter(const RelativeDateTimeFormatter
& other
);
392 * Assignment operator.
395 RelativeDateTimeFormatter
& operator=(
396 const RelativeDateTimeFormatter
& other
);
402 virtual ~RelativeDateTimeFormatter();
405 * Formats a relative date with a quantity such as "in 5 days" or
407 * @param quantity The numerical amount e.g 5. This value is formatted
408 * according to this object's NumberFormat object.
409 * @param direction NEXT means a future relative date; LAST means a past
410 * relative date. If direction is anything else, this method sets
411 * status to U_ILLEGAL_ARGUMENT_ERROR.
412 * @param unit the unit e.g day? month? year?
413 * @param appendTo The string to which the formatted result will be
415 * @param status ICU error code returned here.
419 UnicodeString
& format(
421 UDateDirection direction
,
422 UDateRelativeUnit unit
,
423 UnicodeString
& appendTo
,
424 UErrorCode
& status
) const;
427 * Formats a relative date without a quantity.
428 * @param direction NEXT, LAST, THIS, etc.
429 * @param unit e.g SATURDAY, DAY, MONTH
430 * @param appendTo The string to which the formatted result will be
431 * appended. If the value of direction is documented as not being fully
432 * supported in all locales then this method leaves appendTo unchanged if
433 * no format string is available.
434 * @param status ICU error code returned here.
438 UnicodeString
& format(
439 UDateDirection direction
,
440 UDateAbsoluteUnit unit
,
441 UnicodeString
& appendTo
,
442 UErrorCode
& status
) const;
445 * Combines a relative date string and a time string in this object's
446 * locale. This is done with the same date-time separator used for the
447 * default calendar in this locale.
449 * @param relativeDateString the relative date, e.g 'yesterday'
450 * @param timeString the time e.g '3:45'
451 * @param appendTo concatenated date and time appended here
452 * @param status ICU error code returned here.
456 UnicodeString
& combineDateAndTime(
457 const UnicodeString
& relativeDateString
,
458 const UnicodeString
& timeString
,
459 UnicodeString
& appendTo
,
460 UErrorCode
& status
) const;
463 * Returns the NumberFormat this object is using.
467 const NumberFormat
& getNumberFormat() const;
469 #ifndef U_HIDE_DRAFT_API
471 * Returns the capitalization context.
475 UDisplayContext
getCapitalizationContext() const;
478 * Returns the format style.
482 UDateRelativeDateTimeFormatterStyle
getFormatStyle() const;
483 #endif /* U_HIDE_DRAFT_API */
486 const RelativeDateTimeCacheData
* fCache
;
487 const SharedNumberFormat
*fNumberFormat
;
488 const SharedPluralRules
*fPluralRules
;
489 UDateRelativeDateTimeFormatterStyle fStyle
;
490 UDisplayContext fContext
;
491 const SharedBreakIterator
*fOptBreakIterator
;
494 NumberFormat
*nfToAdopt
,
495 BreakIterator
*brkIter
,
497 void adjustForContext(UnicodeString
&) const;
502 #endif /* !UCONFIG_NO_FORMATTING && !UCONFIG_NO_BREAK_ITERATION*/