ICU 76.1  76.1
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 
19 #if U_SHOW_CPLUSPLUS_API
20 
21 #include "unicode/uobject.h"
23 #include "unicode/ureldatefmt.h"
24 #include "unicode/locid.h"
25 #include "unicode/formattedvalue.h"
26 
32 #if !UCONFIG_NO_FORMATTING
33 
39 typedef enum UDateRelativeUnit {
40 
46 
52 
58 
64 
70 
76 
82 
83 #ifndef U_HIDE_DEPRECATED_API
84 
89 #endif // U_HIDE_DEPRECATED_API
91 
96 typedef enum UDateAbsoluteUnit {
97 
98  // Days of week have to remain together and in order from Sunday to
99  // Saturday.
105 
111 
117 
123 
129 
135 
141 
147 
153 
159 
165 
171 
177 
183 
189 
190 #ifndef U_HIDE_DEPRECATED_API
191 
196 #endif // U_HIDE_DEPRECATED_API
198 
204 typedef enum UDateDirection {
205 
211 
217 
223 
229 
235 
241 
242 #ifndef U_HIDE_DEPRECATED_API
243 
248 #endif // U_HIDE_DEPRECATED_API
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 
273  public:
278  FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
279 
285 
290  virtual ~FormattedRelativeDateTime() override;
291 
294 
296  FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
297 
302  FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) noexcept;
303 
305  UnicodeString toString(UErrorCode& status) const override;
306 
308  UnicodeString toTempString(UErrorCode& status) const override;
309 
311  Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
312 
314  UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
315 
316  private:
317  FormattedRelativeDateTimeData *fData;
318  UErrorCode fErrorCode;
319  explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)
320  : fData(results), fErrorCode(U_ZERO_ERROR) {}
321  explicit FormattedRelativeDateTime(UErrorCode errorCode)
322  : fData(nullptr), fErrorCode(errorCode) {}
323  friend class RelativeDateTimeFormatter;
324 };
325 
397 public:
398 
404 
409  RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
410 
422  const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
423 
440  const Locale& locale,
441  NumberFormat *nfToAdopt,
443  UDisplayContext capitalizationContext,
444  UErrorCode& status);
445 
451 
456  RelativeDateTimeFormatter& operator=(
457  const RelativeDateTimeFormatter& other);
458 
463  virtual ~RelativeDateTimeFormatter();
464 
484  UnicodeString& format(
485  double quantity,
486  UDateDirection direction,
487  UDateRelativeUnit unit,
488  UnicodeString& appendTo,
489  UErrorCode& status) const;
490 
508  FormattedRelativeDateTime formatToValue(
509  double quantity,
510  UDateDirection direction,
511  UDateRelativeUnit unit,
512  UErrorCode& status) const;
513 
530  UnicodeString& format(
531  UDateDirection direction,
532  UDateAbsoluteUnit unit,
533  UnicodeString& appendTo,
534  UErrorCode& status) const;
535 
551  FormattedRelativeDateTime formatToValue(
552  UDateDirection direction,
553  UDateAbsoluteUnit unit,
554  UErrorCode& status) const;
555 
576  UnicodeString& formatNumeric(
577  double offset,
579  UnicodeString& appendTo,
580  UErrorCode& status) const;
581 
600  FormattedRelativeDateTime formatNumericToValue(
601  double offset,
603  UErrorCode& status) const;
604 
625  UnicodeString& format(
626  double offset,
628  UnicodeString& appendTo,
629  UErrorCode& status) const;
630 
649  FormattedRelativeDateTime formatToValue(
650  double offset,
652  UErrorCode& status) const;
653 
666  UnicodeString& combineDateAndTime(
667  const UnicodeString& relativeDateString,
668  const UnicodeString& timeString,
669  UnicodeString& appendTo,
670  UErrorCode& status) const;
671 
677  const NumberFormat& getNumberFormat() const;
678 
684  UDisplayContext getCapitalizationContext() const;
685 
691  UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
692 
693 private:
694  const RelativeDateTimeCacheData* fCache;
695  const SharedNumberFormat *fNumberFormat;
696  const SharedPluralRules *fPluralRules;
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,
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,
743  FormattedRelativeDateTimeData& output,
744  UErrorCode& status) const;
745  void formatRelativeImpl(
746  double offset,
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 */
C++ API: Abstract operations for localized strings.
An immutable class containing the result of a relative datetime formatting operation.
Definition: reldatefmt.h:272
virtual UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const =0
Iterates over field positions in the FormattedValue.
One more than the highest normal UDateAbsoluteUnit value.
Definition: reldatefmt.h:195
URelativeDateTimeUnit
Represents the unit for formatting a relative date.
Definition: ureldatefmt.h:81
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:465
C API: Display context types (enum values)
An abstract formatted value: a string with associated field attributes.
#define UCONFIG_NO_BREAK_ITERATION
This switch turns off break iteration.
Definition: uconfig.h:358
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:316
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:106
FormattedRelativeDateTime()
Default constructor; makes an empty FormattedRelativeDateTime.
Definition: reldatefmt.h:278
UDateRelativeDateTimeFormatterStyle
The formatting style.
Definition: ureldatefmt.h:48
UDateDirection
Represents a direction for an absolute unit e.g "Next Tuesday" or "Last Tuesday". ...
Definition: reldatefmt.h:204
Plain, which means the absence of a qualifier.
Definition: reldatefmt.h:240
Formats simple relative dates.
Definition: reldatefmt.h:396
C++ API: Common ICU base class UObject.
Represents a span of a string containing a given field.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:430
One more than the highest normal UDateDirection value.
Definition: reldatefmt.h:247
UDateAbsoluteUnit
Represents an absolute unit.
Definition: reldatefmt.h:96
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:295
One more than the highest normal UDateRelativeUnit value.
Definition: reldatefmt.h:88
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UMemory is the common ICU base class.
Definition: uobject.h:115
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:494
UDateRelativeUnit
Represents the unit for formatting a relative date.
Definition: reldatefmt.h:39
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
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:195