ICU 64.2  64.2
reldatefmt.h
Go to the documentation of this file.
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 #include "unicode/uobject.h"
20 #include "unicode/ureldatefmt.h"
21 #include "unicode/locid.h"
22 #include "unicode/formattedvalue.h"
23 
29 #if !UCONFIG_NO_FORMATTING
30 
36 typedef enum UDateRelativeUnit {
37 
43 
49 
55 
61 
67 
73 
79 
80 #ifndef U_HIDE_DEPRECATED_API
81 
86 #endif // U_HIDE_DEPRECATED_API
88 
93 typedef enum UDateAbsoluteUnit {
94 
95  // Days of week have to remain together and in order from Sunday to
96  // Saturday.
102 
108 
114 
120 
126 
132 
138 
144 
150 
156 
162 
168 
169 #ifndef U_HIDE_DRAFT_API
170 
175 #endif // U_HIDE_DRAFT_API
176 
177 #ifndef U_HIDE_DEPRECATED_API
178 
183 #endif // U_HIDE_DEPRECATED_API
185 
191 typedef enum UDateDirection {
192 
198 
204 
210 
216 
222 
228 
229 #ifndef U_HIDE_DEPRECATED_API
230 
235 #endif // U_HIDE_DEPRECATED_API
237 
238 #if !UCONFIG_NO_BREAK_ITERATION
239 
241 
242 class BreakIterator;
243 class RelativeDateTimeCacheData;
244 class SharedNumberFormat;
245 class SharedPluralRules;
246 class SharedBreakIterator;
247 class NumberFormat;
248 class UnicodeString;
249 class FormattedRelativeDateTimeData;
250 
251 #ifndef U_HIDE_DRAFT_API
252 
262  public:
267  FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
268 
274 
280 
283 
285  FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
286 
292 
295 
298 
300  Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
301 
304 
305  private:
306  FormattedRelativeDateTimeData *fData;
307  UErrorCode fErrorCode;
308  explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)
309  : fData(results), fErrorCode(U_ZERO_ERROR) {}
310  explicit FormattedRelativeDateTime(UErrorCode errorCode)
311  : fData(nullptr), fErrorCode(errorCode) {}
312  friend class RelativeDateTimeFormatter;
313 };
314 #endif /* U_HIDE_DRAFT_API */
315 
385 public:
386 
392 
397  RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
398 
410  const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
411 
428  const Locale& locale,
429  NumberFormat *nfToAdopt,
431  UDisplayContext capitalizationContext,
432  UErrorCode& status);
433 
439 
444  RelativeDateTimeFormatter& operator=(
445  const RelativeDateTimeFormatter& other);
446 
451  virtual ~RelativeDateTimeFormatter();
452 
472  UnicodeString& format(
473  double quantity,
474  UDateDirection direction,
475  UDateRelativeUnit unit,
476  UnicodeString& appendTo,
477  UErrorCode& status) const;
478 
479 #ifndef U_HIDE_DRAFT_API
480 
497  FormattedRelativeDateTime formatToValue(
498  double quantity,
499  UDateDirection direction,
500  UDateRelativeUnit unit,
501  UErrorCode& status) const;
502 #endif /* U_HIDE_DRAFT_API */
503 
520  UnicodeString& format(
521  UDateDirection direction,
522  UDateAbsoluteUnit unit,
523  UnicodeString& appendTo,
524  UErrorCode& status) const;
525 
526 #ifndef U_HIDE_DRAFT_API
527 
542  FormattedRelativeDateTime formatToValue(
543  UDateDirection direction,
544  UDateAbsoluteUnit unit,
545  UErrorCode& status) const;
546 #endif /* U_HIDE_DRAFT_API */
547 
568  UnicodeString& formatNumeric(
569  double offset,
571  UnicodeString& appendTo,
572  UErrorCode& status) const;
573 
574 #ifndef U_HIDE_DRAFT_API
575 
593  FormattedRelativeDateTime formatNumericToValue(
594  double offset,
596  UErrorCode& status) const;
597 #endif /* U_HIDE_DRAFT_API */
598 
619  UnicodeString& format(
620  double offset,
622  UnicodeString& appendTo,
623  UErrorCode& status) const;
624 
625 #ifndef U_HIDE_DRAFT_API
626 
644  FormattedRelativeDateTime formatToValue(
645  double offset,
647  UErrorCode& status) const;
648 #endif /* U_HIDE_DRAFT_API */
649 
662  UnicodeString& combineDateAndTime(
663  const UnicodeString& relativeDateString,
664  const UnicodeString& timeString,
665  UnicodeString& appendTo,
666  UErrorCode& status) const;
667 
673  const NumberFormat& getNumberFormat() const;
674 
680  UDisplayContext getCapitalizationContext() const;
681 
687  UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
688 
689 private:
690  const RelativeDateTimeCacheData* fCache;
691  const SharedNumberFormat *fNumberFormat;
692  const SharedPluralRules *fPluralRules;
694  UDisplayContext fContext;
695  const SharedBreakIterator *fOptBreakIterator;
696  Locale fLocale;
697  void init(
698  NumberFormat *nfToAdopt,
699  BreakIterator *brkIter,
700  UErrorCode &status);
701  UnicodeString& adjustForContext(UnicodeString &) const;
702  UBool checkNoAdjustForContext(UErrorCode& status) const;
703 
704  template<typename F, typename... Args>
705  UnicodeString& doFormat(
706  F callback,
707  UnicodeString& appendTo,
708  UErrorCode& status,
709  Args... args) const;
710 
711 #ifndef U_HIDE_DRAFT_API // for FormattedRelativeDateTime
712  template<typename F, typename... Args>
713  FormattedRelativeDateTime doFormatToValue(
714  F callback,
715  UErrorCode& status,
716  Args... args) const;
717 #endif // U_HIDE_DRAFT_API
718 
719  void formatImpl(
720  double quantity,
721  UDateDirection direction,
722  UDateRelativeUnit unit,
723  FormattedRelativeDateTimeData& output,
724  UErrorCode& status) const;
725  void formatAbsoluteImpl(
726  UDateDirection direction,
727  UDateAbsoluteUnit unit,
728  FormattedRelativeDateTimeData& output,
729  UErrorCode& status) const;
730  void formatNumericImpl(
731  double offset,
733  FormattedRelativeDateTimeData& output,
734  UErrorCode& status) const;
735  void formatRelativeImpl(
736  double offset,
738  FormattedRelativeDateTimeData& output,
739  UErrorCode& status) const;
740 };
741 
743 
744 #endif /* !UCONFIG_NO_BREAK_ITERATION */
745 #endif /* !UCONFIG_NO_FORMATTING */
746 #endif /* __RELDATEFMT_H */
C++ API: Abstract operations for localized strings.
An immutable class containing the result of a relative datetime formatting operation.
Definition: reldatefmt.h:261
virtual UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const =0
Iterates over field positions in the FormattedValue.
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
One more than the highest normal UDateAbsoluteUnit value.
Definition: reldatefmt.h:182
URelativeDateTimeUnit
Represents the unit for formatting a relative date.
Definition: ureldatefmt.h:78
virtual UnicodeString toString(UErrorCode &status) const =0
Returns the formatted string as a self-contained UnicodeString.
UDisplayContext
Display context settings.
No error, no warning.
Definition: utypes.h:435
C API: Display context types (enum values)
An abstract formatted value: a string with associated field attributes.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
The BreakIterator class implements methods for finding the location of boundaries in text...
Definition: brkiter.h:102
FormattedRelativeDateTime()
Default constructor; makes an empty FormattedRelativeDateTime.
Definition: reldatefmt.h:267
UDateRelativeDateTimeFormatterStyle
The formatting style.
Definition: ureldatefmt.h:45
UDateDirection
Represents a direction for an absolute unit e.g "Next Tuesday" or "Last Tuesday". ...
Definition: reldatefmt.h:191
Plain, which means the absence of a qualifier.
Definition: reldatefmt.h:227
Formats simple relative dates.
Definition: reldatefmt.h:384
C++ API: Common ICU base class UObject.
Represents a span of a string containing a given field.
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:503
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
One more than the highest normal UDateDirection value.
Definition: reldatefmt.h:234
UDateAbsoluteUnit
Represents an absolute unit.
Definition: reldatefmt.h:93
C++ API: Locale ID object.
Basic definitions for ICU, for both C and C++ APIs.
virtual Appendable & appendTo(Appendable &appendable, UErrorCode &status) const =0
Appends the formatted string to an Appendable.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:289
One more than the highest normal UDateRelativeUnit value.
Definition: reldatefmt.h:85
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UMemory is the common ICU base class.
Definition: uobject.h:112
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:464
UDateRelativeUnit
Represents the unit for formatting a relative date.
Definition: reldatefmt.h:36
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:51
virtual UnicodeString toTempString(UErrorCode &status) const =0
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192