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 
301 
307 
309  UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
310 
312  UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
313 
315  Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
316 
318  UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
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 
459  const RelativeDateTimeFormatter& other);
460 
466 
487  double quantity,
488  UDateDirection direction,
489  UDateRelativeUnit unit,
490  UnicodeString& appendTo,
491  UErrorCode& status) const;
492 
511  double quantity,
512  UDateDirection direction,
513  UDateRelativeUnit unit,
514  UErrorCode& status) const;
515 
533  UDateDirection direction,
534  UDateAbsoluteUnit unit,
535  UnicodeString& appendTo,
536  UErrorCode& status) const;
537 
554  UDateDirection direction,
555  UDateAbsoluteUnit unit,
556  UErrorCode& status) const;
557 
579  double offset,
581  UnicodeString& appendTo,
582  UErrorCode& status) const;
583 
603  double offset,
605  UErrorCode& status) const;
606 
628  double offset,
630  UnicodeString& appendTo,
631  UErrorCode& status) const;
632 
652  double offset,
654  UErrorCode& status) const;
655 
669  const UnicodeString& relativeDateString,
670  const UnicodeString& timeString,
671  UnicodeString& appendTo,
672  UErrorCode& status) const;
673 
680 
687 
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 */
UDAT_ABSOLUTE_WEDNESDAY
@ UDAT_ABSOLUTE_WEDNESDAY
Wednesday.
Definition: reldatefmt.h:122
UDAT_DIRECTION_LAST
@ UDAT_DIRECTION_LAST
Last.
Definition: reldatefmt.h:218
icu::RelativeDateTimeFormatter::getFormatStyle
UDateRelativeDateTimeFormatterStyle getFormatStyle() const
Returns the format style.
UDAT_RELATIVE_SECONDS
@ UDAT_RELATIVE_SECONDS
Seconds.
Definition: reldatefmt.h:45
icu::RelativeDateTimeFormatter::getNumberFormat
const NumberFormat & getNumberFormat() const
Returns the NumberFormat this object is using.
icu::BreakIterator
The BreakIterator class implements methods for finding the location of boundaries in text.
Definition: brkiter.h:106
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
RelativeDateTimeFormatter(const Locale &locale, NumberFormat *nfToAdopt, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale and NumberFormat.
icu::NumberFormat
Definition: numfmt.h:175
icu::RelativeDateTimeFormatter
Formats simple relative dates.
Definition: reldatefmt.h:398
UDAT_RELATIVE_MONTHS
@ UDAT_RELATIVE_MONTHS
Months.
Definition: reldatefmt.h:75
UDAT_ABSOLUTE_TUESDAY
@ UDAT_ABSOLUTE_TUESDAY
Tuesday.
Definition: reldatefmt.h:116
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime(FormattedRelativeDateTime &&src) U_NOEXCEPT
Move constructor: Leaves the source FormattedRelativeDateTime in an undefined state.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::RelativeDateTimeFormatter::formatNumeric
UnicodeString & formatNumeric(double offset, URelativeDateTimeUnit unit, UnicodeString &appendTo, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a numeric style,...
UDAT_RELATIVE_MINUTES
@ UDAT_RELATIVE_MINUTES
Minutes.
Definition: reldatefmt.h:51
UDAT_ABSOLUTE_MINUTE
@ UDAT_ABSOLUTE_MINUTE
Minute.
Definition: reldatefmt.h:189
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime()
Default constructor; makes an empty FormattedRelativeDateTime.
Definition: reldatefmt.h:282
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition: formattedvalue.h:241
URelativeDateTimeUnit
URelativeDateTimeUnit
Represents the unit for formatting a relative date.
Definition: ureldatefmt.h:78
icu::RelativeDateTimeFormatter::formatToValue
FormattedRelativeDateTime formatToValue(double quantity, UDateDirection direction, UDateRelativeUnit unit, UErrorCode &status) const
Formats a relative date with a quantity such as "in 5 days" or "3 months ago".
icu::RelativeDateTimeFormatter::formatToValue
FormattedRelativeDateTime formatToValue(double offset, URelativeDateTimeUnit unit, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a text style if possible,...
U_OVERRIDE
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
icu::RelativeDateTimeFormatter::format
UnicodeString & format(double offset, URelativeDateTimeUnit unit, UnicodeString &appendTo, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a text style if possible,...
UDAT_ABSOLUTE_YEAR
@ UDAT_ABSOLUTE_YEAR
Year.
Definition: reldatefmt.h:164
UDAT_RELATIVE_YEARS
@ UDAT_RELATIVE_YEARS
Years.
Definition: reldatefmt.h:81
icu::FormattedRelativeDateTime::~FormattedRelativeDateTime
virtual ~FormattedRelativeDateTime() U_OVERRIDE
Destruct an instance of FormattedRelativeDateTime.
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
RelativeDateTimeFormatter(UErrorCode &status)
Create RelativeDateTimeFormatter with default locale.
UDAT_RELATIVE_HOURS
@ UDAT_RELATIVE_HOURS
Hours.
Definition: reldatefmt.h:57
UDAT_DIRECTION_COUNT
@ UDAT_DIRECTION_COUNT
One more than the highest normal UDateDirection value.
Definition: reldatefmt.h:249
icu::RelativeDateTimeFormatter::formatNumericToValue
FormattedRelativeDateTime formatNumericToValue(double offset, URelativeDateTimeUnit unit, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a numeric style,...
UDAT_ABSOLUTE_QUARTER
@ UDAT_ABSOLUTE_QUARTER
Quarter.
Definition: reldatefmt.h:176
UDAT_ABSOLUTE_MONDAY
@ UDAT_ABSOLUTE_MONDAY
Monday.
Definition: reldatefmt.h:110
UDAT_ABSOLUTE_WEEK
@ UDAT_ABSOLUTE_WEEK
Week.
Definition: reldatefmt.h:152
UDAT_RELATIVE_DAYS
@ UDAT_RELATIVE_DAYS
Days.
Definition: reldatefmt.h:63
UDAT_ABSOLUTE_FRIDAY
@ UDAT_ABSOLUTE_FRIDAY
Friday.
Definition: reldatefmt.h:134
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:295
ureldatefmt.h
C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
UDateDirection
UDateDirection
Represents a direction for an absolute unit e.g "Next Tuesday" or "Last Tuesday".
Definition: reldatefmt.h:206
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
RelativeDateTimeFormatter(const RelativeDateTimeFormatter &other)
Copy constructor.
icu::RelativeDateTimeFormatter::getCapitalizationContext
UDisplayContext getCapitalizationContext() const
Returns the capitalization context.
icu::RelativeDateTimeFormatter::formatToValue
FormattedRelativeDateTime formatToValue(UDateDirection direction, UDateAbsoluteUnit unit, UErrorCode &status) const
Formats a relative date without a quantity.
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
UDAT_ABSOLUTE_DAY
@ UDAT_ABSOLUTE_DAY
Day.
Definition: reldatefmt.h:146
UDAT_DIRECTION_THIS
@ UDAT_DIRECTION_THIS
This.
Definition: reldatefmt.h:224
icu::FormattedRelativeDateTime
An immutable class containing the result of a relative datetime formatting operation.
Definition: reldatefmt.h:276
icu::RelativeDateTimeFormatter::~RelativeDateTimeFormatter
virtual ~RelativeDateTimeFormatter()
Destructor.
icu::RelativeDateTimeFormatter::operator=
RelativeDateTimeFormatter & operator=(const RelativeDateTimeFormatter &other)
Assignment operator.
UDAT_ABSOLUTE_SUNDAY
@ UDAT_ABSOLUTE_SUNDAY
Sunday.
Definition: reldatefmt.h:104
UDAT_DIRECTION_PLAIN
@ UDAT_DIRECTION_PLAIN
Plain, which means the absence of a qualifier.
Definition: reldatefmt.h:242
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
formattedvalue.h
C++ API: Abstract operations for localized strings.
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
RelativeDateTimeFormatter(const Locale &locale, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale.
UDAT_ABSOLUTE_SATURDAY
@ UDAT_ABSOLUTE_SATURDAY
Saturday.
Definition: reldatefmt.h:140
UDAT_RELATIVE_UNIT_COUNT
@ UDAT_RELATIVE_UNIT_COUNT
One more than the highest normal UDateRelativeUnit value.
Definition: reldatefmt.h:88
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
UDAT_ABSOLUTE_HOUR
@ UDAT_ABSOLUTE_HOUR
Hour.
Definition: reldatefmt.h:183
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
RelativeDateTimeFormatter(const Locale &locale, NumberFormat *nfToAdopt, UDateRelativeDateTimeFormatterStyle style, UDisplayContext capitalizationContext, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale, NumberFormat, and capitalization context.
UDAT_RELATIVE_WEEKS
@ UDAT_RELATIVE_WEEKS
Weeks.
Definition: reldatefmt.h:69
UDisplayContext
UDisplayContext
Display context settings.
Definition: udisplaycontext.h:64
UDAT_ABSOLUTE_MONTH
@ UDAT_ABSOLUTE_MONTH
Month.
Definition: reldatefmt.h:158
UDAT_DIRECTION_LAST_2
@ UDAT_DIRECTION_LAST_2
Two before.
Definition: reldatefmt.h:212
UDateAbsoluteUnit
UDateAbsoluteUnit
Represents an absolute unit.
Definition: reldatefmt.h:96
UDAT_DIRECTION_NEXT
@ UDAT_DIRECTION_NEXT
Next.
Definition: reldatefmt.h:230
icu::RelativeDateTimeFormatter::format
UnicodeString & format(double quantity, UDateDirection direction, UDateRelativeUnit unit, UnicodeString &appendTo, UErrorCode &status) const
Formats a relative date with a quantity such as "in 5 days" or "3 months ago".
UDAT_ABSOLUTE_UNIT_COUNT
@ UDAT_ABSOLUTE_UNIT_COUNT
One more than the highest normal UDateAbsoluteUnit value.
Definition: reldatefmt.h:197
udisplaycontext.h
C API: Display context types (enum values)
locid.h
C++ API: Locale ID object.
UDateRelativeDateTimeFormatterStyle
UDateRelativeDateTimeFormatterStyle
The formatting style.
Definition: ureldatefmt.h:45
UDateRelativeUnit
UDateRelativeUnit
Represents the unit for formatting a relative date.
Definition: reldatefmt.h:39
uobject.h
C++ API: Common ICU base class UObject.
U_INVALID_STATE_ERROR
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
icu::RelativeDateTimeFormatter::format
UnicodeString & format(UDateDirection direction, UDateAbsoluteUnit unit, UnicodeString &appendTo, UErrorCode &status) const
Formats a relative date without a quantity.
UDAT_ABSOLUTE_NOW
@ UDAT_ABSOLUTE_NOW
Now.
Definition: reldatefmt.h:170
icu::RelativeDateTimeFormatter::combineDateAndTime
UnicodeString & combineDateAndTime(const UnicodeString &relativeDateString, const UnicodeString &timeString, UnicodeString &appendTo, UErrorCode &status) const
Combines a relative date string and a time string in this object's locale.
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:41
UDAT_DIRECTION_NEXT_2
@ UDAT_DIRECTION_NEXT_2
Two after.
Definition: reldatefmt.h:236
UDAT_ABSOLUTE_THURSDAY
@ UDAT_ABSOLUTE_THURSDAY
Thursday.
Definition: reldatefmt.h:128