ICU 74.2  74.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 
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 
395 public:
396 
402 
407  RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
408 
420  const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
421 
438  const Locale& locale,
439  NumberFormat *nfToAdopt,
441  UDisplayContext capitalizationContext,
442  UErrorCode& status);
443 
449 
454  RelativeDateTimeFormatter& operator=(
455  const RelativeDateTimeFormatter& other);
456 
461  virtual ~RelativeDateTimeFormatter();
462 
482  UnicodeString& format(
483  double quantity,
484  UDateDirection direction,
485  UDateRelativeUnit unit,
486  UnicodeString& appendTo,
487  UErrorCode& status) const;
488 
506  FormattedRelativeDateTime formatToValue(
507  double quantity,
508  UDateDirection direction,
509  UDateRelativeUnit unit,
510  UErrorCode& status) const;
511 
528  UnicodeString& format(
529  UDateDirection direction,
530  UDateAbsoluteUnit unit,
531  UnicodeString& appendTo,
532  UErrorCode& status) const;
533 
549  FormattedRelativeDateTime formatToValue(
550  UDateDirection direction,
551  UDateAbsoluteUnit unit,
552  UErrorCode& status) const;
553 
574  UnicodeString& formatNumeric(
575  double offset,
577  UnicodeString& appendTo,
578  UErrorCode& status) const;
579 
598  FormattedRelativeDateTime formatNumericToValue(
599  double offset,
601  UErrorCode& status) const;
602 
623  UnicodeString& format(
624  double offset,
626  UnicodeString& appendTo,
627  UErrorCode& status) const;
628 
647  FormattedRelativeDateTime formatToValue(
648  double offset,
650  UErrorCode& status) const;
651 
664  UnicodeString& combineDateAndTime(
665  const UnicodeString& relativeDateString,
666  const UnicodeString& timeString,
667  UnicodeString& appendTo,
668  UErrorCode& status) const;
669 
675  const NumberFormat& getNumberFormat() const;
676 
682  UDisplayContext getCapitalizationContext() const;
683 
689  UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
690 
691 private:
692  const RelativeDateTimeCacheData* fCache;
693  const SharedNumberFormat *fNumberFormat;
694  const SharedPluralRules *fPluralRules;
696  UDisplayContext fContext;
697 #if !UCONFIG_NO_BREAK_ITERATION
698  const SharedBreakIterator *fOptBreakIterator;
699 #else
700  std::nullptr_t fOptBreakIterator = nullptr;
701 #endif // !UCONFIG_NO_BREAK_ITERATION
702  Locale fLocale;
703  void init(
704  NumberFormat *nfToAdopt,
706  BreakIterator *brkIter,
707 #else
708  std::nullptr_t,
709 #endif // !UCONFIG_NO_BREAK_ITERATION
710  UErrorCode &status);
711  UnicodeString& adjustForContext(UnicodeString &) const;
712  UBool checkNoAdjustForContext(UErrorCode& status) const;
713 
714  template<typename F, typename... Args>
715  UnicodeString& doFormat(
716  F callback,
717  UnicodeString& appendTo,
718  UErrorCode& status,
719  Args... args) const;
720 
721  template<typename F, typename... Args>
722  FormattedRelativeDateTime doFormatToValue(
723  F callback,
724  UErrorCode& status,
725  Args... args) const;
726 
727  void formatImpl(
728  double quantity,
729  UDateDirection direction,
730  UDateRelativeUnit unit,
731  FormattedRelativeDateTimeData& output,
732  UErrorCode& status) const;
733  void formatAbsoluteImpl(
734  UDateDirection direction,
735  UDateAbsoluteUnit unit,
736  FormattedRelativeDateTimeData& output,
737  UErrorCode& status) const;
738  void formatNumericImpl(
739  double offset,
741  FormattedRelativeDateTimeData& output,
742  UErrorCode& status) const;
743  void formatRelativeImpl(
744  double offset,
746  FormattedRelativeDateTimeData& output,
747  UErrorCode& status) const;
748 };
749 
750 U_NAMESPACE_END
751 
752 #endif /* !UCONFIG_NO_FORMATTING */
753 
754 #endif /* U_SHOW_CPLUSPLUS_API */
755 
756 #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:449
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:301
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:394
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:415
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:478
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