ICU 67.1  67.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 
178 #ifndef U_HIDE_DRAFT_API
179 
184 
190 #endif // U_HIDE_DRAFT_API
191 
192 #ifndef U_HIDE_DEPRECATED_API
193 
198 #endif // U_HIDE_DEPRECATED_API
200 
206 typedef enum UDateDirection {
207 
213 
219 
225 
231 
237 
243 
244 #ifndef U_HIDE_DEPRECATED_API
245 
250 #endif // U_HIDE_DEPRECATED_API
252 
253 #if !UCONFIG_NO_BREAK_ITERATION
254 
255 U_NAMESPACE_BEGIN
256 
257 class BreakIterator;
258 class RelativeDateTimeCacheData;
259 class SharedNumberFormat;
260 class SharedPluralRules;
261 class SharedBreakIterator;
262 class NumberFormat;
263 class UnicodeString;
264 class FormattedRelativeDateTime;
265 class FormattedRelativeDateTimeData;
266 
277  public:
282  FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
283 
289 
295 
298 
300  FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
301 
307 
310 
313 
315  Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
316 
319 
320  private:
321  FormattedRelativeDateTimeData *fData;
322  UErrorCode fErrorCode;
323  explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)
324  : fData(results), fErrorCode(U_ZERO_ERROR) {}
325  explicit FormattedRelativeDateTime(UErrorCode errorCode)
326  : fData(nullptr), fErrorCode(errorCode) {}
327  friend class RelativeDateTimeFormatter;
328 };
329 
399 public:
400 
406 
411  RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
412 
424  const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
425 
442  const Locale& locale,
443  NumberFormat *nfToAdopt,
445  UDisplayContext capitalizationContext,
446  UErrorCode& status);
447 
453 
458  RelativeDateTimeFormatter& operator=(
459  const RelativeDateTimeFormatter& other);
460 
465  virtual ~RelativeDateTimeFormatter();
466 
486  UnicodeString& format(
487  double quantity,
488  UDateDirection direction,
489  UDateRelativeUnit unit,
490  UnicodeString& appendTo,
491  UErrorCode& status) const;
492 
510  FormattedRelativeDateTime formatToValue(
511  double quantity,
512  UDateDirection direction,
513  UDateRelativeUnit unit,
514  UErrorCode& status) const;
515 
532  UnicodeString& format(
533  UDateDirection direction,
534  UDateAbsoluteUnit unit,
535  UnicodeString& appendTo,
536  UErrorCode& status) const;
537 
553  FormattedRelativeDateTime formatToValue(
554  UDateDirection direction,
555  UDateAbsoluteUnit unit,
556  UErrorCode& status) const;
557 
578  UnicodeString& formatNumeric(
579  double offset,
581  UnicodeString& appendTo,
582  UErrorCode& status) const;
583 
602  FormattedRelativeDateTime formatNumericToValue(
603  double offset,
605  UErrorCode& status) const;
606 
627  UnicodeString& format(
628  double offset,
630  UnicodeString& appendTo,
631  UErrorCode& status) const;
632 
651  FormattedRelativeDateTime formatToValue(
652  double offset,
654  UErrorCode& status) const;
655 
668  UnicodeString& combineDateAndTime(
669  const UnicodeString& relativeDateString,
670  const UnicodeString& timeString,
671  UnicodeString& appendTo,
672  UErrorCode& status) const;
673 
679  const NumberFormat& getNumberFormat() const;
680 
686  UDisplayContext getCapitalizationContext() const;
687 
693  UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
694 
695 private:
696  const RelativeDateTimeCacheData* fCache;
697  const SharedNumberFormat *fNumberFormat;
698  const SharedPluralRules *fPluralRules;
700  UDisplayContext fContext;
701  const SharedBreakIterator *fOptBreakIterator;
702  Locale fLocale;
703  void init(
704  NumberFormat *nfToAdopt,
705  BreakIterator *brkIter,
706  UErrorCode &status);
707  UnicodeString& adjustForContext(UnicodeString &) const;
708  UBool checkNoAdjustForContext(UErrorCode& status) const;
709 
710  template<typename F, typename... Args>
711  UnicodeString& doFormat(
712  F callback,
713  UnicodeString& appendTo,
714  UErrorCode& status,
715  Args... args) const;
716 
717  template<typename F, typename... Args>
718  FormattedRelativeDateTime doFormatToValue(
719  F callback,
720  UErrorCode& status,
721  Args... args) const;
722 
723  void formatImpl(
724  double quantity,
725  UDateDirection direction,
726  UDateRelativeUnit unit,
727  FormattedRelativeDateTimeData& output,
728  UErrorCode& status) const;
729  void formatAbsoluteImpl(
730  UDateDirection direction,
731  UDateAbsoluteUnit unit,
732  FormattedRelativeDateTimeData& output,
733  UErrorCode& status) const;
734  void formatNumericImpl(
735  double offset,
737  FormattedRelativeDateTimeData& output,
738  UErrorCode& status) const;
739  void formatRelativeImpl(
740  double offset,
742  FormattedRelativeDateTimeData& output,
743  UErrorCode& status) const;
744 };
745 
746 U_NAMESPACE_END
747 
748 #endif /* !UCONFIG_NO_BREAK_ITERATION */
749 #endif /* !UCONFIG_NO_FORMATTING */
750 
751 #endif /* U_SHOW_CPLUSPLUS_API */
752 
753 #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:276
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:197
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:449
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
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:282
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:206
Plain, which means the absence of a qualifier.
Definition: reldatefmt.h:242
Formats simple relative dates.
Definition: reldatefmt.h:398
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:529
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:249
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:294
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.
Definition: umachine.h:261
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