ICU 75.1 75.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"
22 #include "unicode/udisplaycontext.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
45 UDAT_RELATIVE_SECONDS,
46
51 UDAT_RELATIVE_MINUTES,
52
57 UDAT_RELATIVE_HOURS,
58
63 UDAT_RELATIVE_DAYS,
64
69 UDAT_RELATIVE_WEEKS,
70
75 UDAT_RELATIVE_MONTHS,
76
81 UDAT_RELATIVE_YEARS,
82
83 #ifndef U_HIDE_DEPRECATED_API
88 UDAT_RELATIVE_UNIT_COUNT
89 #endif // U_HIDE_DEPRECATED_API
90 } UDateRelativeUnit;
91
96 typedef enum UDateAbsoluteUnit {
97
98 // Days of week have to remain together and in order from Sunday to
99 // Saturday.
104 UDAT_ABSOLUTE_SUNDAY,
105
110 UDAT_ABSOLUTE_MONDAY,
111
116 UDAT_ABSOLUTE_TUESDAY,
117
122 UDAT_ABSOLUTE_WEDNESDAY,
123
128 UDAT_ABSOLUTE_THURSDAY,
129
134 UDAT_ABSOLUTE_FRIDAY,
135
140 UDAT_ABSOLUTE_SATURDAY,
141
146 UDAT_ABSOLUTE_DAY,
147
152 UDAT_ABSOLUTE_WEEK,
153
158 UDAT_ABSOLUTE_MONTH,
159
164 UDAT_ABSOLUTE_YEAR,
165
170 UDAT_ABSOLUTE_NOW,
171
176 UDAT_ABSOLUTE_QUARTER,
177
182 UDAT_ABSOLUTE_HOUR,
183
188 UDAT_ABSOLUTE_MINUTE,
189
190 #ifndef U_HIDE_DEPRECATED_API
195 UDAT_ABSOLUTE_UNIT_COUNT = UDAT_ABSOLUTE_NOW + 4
196 #endif // U_HIDE_DEPRECATED_API
197 } UDateAbsoluteUnit;
198
204 typedef enum UDateDirection {
205
210 UDAT_DIRECTION_LAST_2,
211
216 UDAT_DIRECTION_LAST,
217
222 UDAT_DIRECTION_THIS,
223
228 UDAT_DIRECTION_NEXT,
229
234 UDAT_DIRECTION_NEXT_2,
235
240 UDAT_DIRECTION_PLAIN,
241
242 #ifndef U_HIDE_DEPRECATED_API
247 UDAT_DIRECTION_COUNT
248 #endif // U_HIDE_DEPRECATED_API
249 } UDateDirection;
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
272 class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue {
273 public:
278 FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
279
284 FormattedRelativeDateTime(FormattedRelativeDateTime&& src) noexcept;
285
290 virtual ~FormattedRelativeDateTime() override;
291
293 FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete;
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
394 class U_I18N_API RelativeDateTimeFormatter : public UObject {
395 public:
396
401 RelativeDateTimeFormatter(UErrorCode& status);
402
407 RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
408
419 RelativeDateTimeFormatter(
420 const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
421
437 RelativeDateTimeFormatter(
438 const Locale& locale,
439 NumberFormat *nfToAdopt,
440 UDateRelativeDateTimeFormatterStyle style,
441 UDisplayContext capitalizationContext,
442 UErrorCode& status);
443
448 RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
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,
576 URelativeDateTimeUnit unit,
577 UnicodeString& appendTo,
578 UErrorCode& status) const;
579
598 FormattedRelativeDateTime formatNumericToValue(
599 double offset,
600 URelativeDateTimeUnit unit,
601 UErrorCode& status) const;
602
623 UnicodeString& format(
624 double offset,
625 URelativeDateTimeUnit unit,
626 UnicodeString& appendTo,
627 UErrorCode& status) const;
628
647 FormattedRelativeDateTime formatToValue(
648 double offset,
649 URelativeDateTimeUnit unit,
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;
695 UDateRelativeDateTimeFormatterStyle fStyle;
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,
705 #if !UCONFIG_NO_BREAK_ITERATION
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,
740 URelativeDateTimeUnit unit,
741 FormattedRelativeDateTimeData& output,
742 UErrorCode& status) const;
743 void formatRelativeImpl(
744 double offset,
745 URelativeDateTimeUnit unit,
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 */
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
icu::BreakIterator
The BreakIterator class implements methods for finding the location of boundaries in text.
Definition: brkiter.h:106
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:41
icu::FormattedRelativeDateTime
An immutable class containing the result of a relative datetime formatting operation.
Definition: reldatefmt.h:272
icu::FormattedRelativeDateTime::operator=
FormattedRelativeDateTime & operator=(const FormattedRelativeDateTime &)=delete
Copying not supported; use move assignment instead.
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime(FormattedRelativeDateTime &&src) noexcept
Move constructor: Leaves the source FormattedRelativeDateTime in an undefined state.
icu::FormattedRelativeDateTime::operator=
FormattedRelativeDateTime & operator=(FormattedRelativeDateTime &&src) noexcept
Move assignment: Leaves the source FormattedRelativeDateTime in an undefined state.
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime()
Default constructor; makes an empty FormattedRelativeDateTime.
Definition: reldatefmt.h:278
icu::FormattedRelativeDateTime::appendTo
Appendable & appendTo(Appendable &appendable, UErrorCode &status) const override
Appends the formatted string to an Appendable.
icu::FormattedRelativeDateTime::toString
UnicodeString toString(UErrorCode &status) const override
Returns the formatted string as a self-contained UnicodeString.
icu::FormattedRelativeDateTime::nextPosition
UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const override
Iterates over field positions in the FormattedValue.
icu::FormattedRelativeDateTime::~FormattedRelativeDateTime
virtual ~FormattedRelativeDateTime() override
Destruct an instance of FormattedRelativeDateTime.
icu::FormattedRelativeDateTime::toTempString
UnicodeString toTempString(UErrorCode &status) const override
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime(const FormattedRelativeDateTime &)=delete
Copying not supported; use move constructor instead.
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition: formattedvalue.h:241
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
icu::NumberFormat
Definition: numfmt.h:175
icu::RelativeDateTimeFormatter
Formats simple relative dates.
Definition: reldatefmt.h:394
icu::RelativeDateTimeFormatter::~RelativeDateTimeFormatter
virtual ~RelativeDateTimeFormatter()
Destructor.
icu::RelativeDateTimeFormatter::formatNumericToValue
FormattedRelativeDateTime formatNumericToValue(double offset, URelativeDateTimeUnit unit, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a numeric style,...
icu::RelativeDateTimeFormatter::format
UnicodeString & format(UDateDirection direction, UDateAbsoluteUnit unit, UnicodeString &appendTo, UErrorCode &status) const
Formats a relative date without a quantity.
icu::RelativeDateTimeFormatter::getFormatStyle
UDateRelativeDateTimeFormatterStyle getFormatStyle() const
Returns the format style.
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,...
icu::RelativeDateTimeFormatter::getNumberFormat
const NumberFormat & getNumberFormat() const
Returns the NumberFormat this object is using.
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::RelativeDateTimeFormatter
RelativeDateTimeFormatter(UErrorCode &status)
Create RelativeDateTimeFormatter with default locale.
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
RelativeDateTimeFormatter(const Locale &locale, NumberFormat *nfToAdopt, UDateRelativeDateTimeFormatterStyle style, UDisplayContext capitalizationContext, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale, NumberFormat, and capitalization context.
icu::RelativeDateTimeFormatter::operator=
RelativeDateTimeFormatter & operator=(const RelativeDateTimeFormatter &other)
Assignment operator.
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::RelativeDateTimeFormatter::RelativeDateTimeFormatter
RelativeDateTimeFormatter(const Locale &locale, NumberFormat *nfToAdopt, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale and NumberFormat.
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,...
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
RelativeDateTimeFormatter(const RelativeDateTimeFormatter &other)
Copy constructor.
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,...
icu::RelativeDateTimeFormatter::getCapitalizationContext
UDisplayContext getCapitalizationContext() const
Returns the capitalization context.
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".
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
RelativeDateTimeFormatter(const Locale &locale, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale.
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
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
formattedvalue.h
C++ API: Abstract operations for localized strings.
locid.h
C++ API: Locale ID object.
UDateAbsoluteUnit
UDateAbsoluteUnit
Represents an absolute unit.
Definition: reldatefmt.h:96
UDAT_ABSOLUTE_DAY
@ UDAT_ABSOLUTE_DAY
Day.
Definition: reldatefmt.h:146
UDAT_ABSOLUTE_FRIDAY
@ UDAT_ABSOLUTE_FRIDAY
Friday.
Definition: reldatefmt.h:134
UDAT_ABSOLUTE_YEAR
@ UDAT_ABSOLUTE_YEAR
Year.
Definition: reldatefmt.h:164
UDAT_ABSOLUTE_WEDNESDAY
@ UDAT_ABSOLUTE_WEDNESDAY
Wednesday.
Definition: reldatefmt.h:122
UDAT_ABSOLUTE_HOUR
@ UDAT_ABSOLUTE_HOUR
Hour.
Definition: reldatefmt.h:182
UDAT_ABSOLUTE_WEEK
@ UDAT_ABSOLUTE_WEEK
Week.
Definition: reldatefmt.h:152
UDAT_ABSOLUTE_NOW
@ UDAT_ABSOLUTE_NOW
Now.
Definition: reldatefmt.h:170
UDAT_ABSOLUTE_MINUTE
@ UDAT_ABSOLUTE_MINUTE
Minute.
Definition: reldatefmt.h:188
UDAT_ABSOLUTE_THURSDAY
@ UDAT_ABSOLUTE_THURSDAY
Thursday.
Definition: reldatefmt.h:128
UDAT_ABSOLUTE_TUESDAY
@ UDAT_ABSOLUTE_TUESDAY
Tuesday.
Definition: reldatefmt.h:116
UDAT_ABSOLUTE_MONTH
@ UDAT_ABSOLUTE_MONTH
Month.
Definition: reldatefmt.h:158
UDAT_ABSOLUTE_SUNDAY
@ UDAT_ABSOLUTE_SUNDAY
Sunday.
Definition: reldatefmt.h:104
UDAT_ABSOLUTE_UNIT_COUNT
@ UDAT_ABSOLUTE_UNIT_COUNT
One more than the highest normal UDateAbsoluteUnit value.
Definition: reldatefmt.h:195
UDAT_ABSOLUTE_MONDAY
@ UDAT_ABSOLUTE_MONDAY
Monday.
Definition: reldatefmt.h:110
UDAT_ABSOLUTE_SATURDAY
@ UDAT_ABSOLUTE_SATURDAY
Saturday.
Definition: reldatefmt.h:140
UDAT_ABSOLUTE_QUARTER
@ UDAT_ABSOLUTE_QUARTER
Quarter.
Definition: reldatefmt.h:176
UDateRelativeUnit
UDateRelativeUnit
Represents the unit for formatting a relative date.
Definition: reldatefmt.h:39
UDAT_RELATIVE_DAYS
@ UDAT_RELATIVE_DAYS
Days.
Definition: reldatefmt.h:63
UDAT_RELATIVE_UNIT_COUNT
@ UDAT_RELATIVE_UNIT_COUNT
One more than the highest normal UDateRelativeUnit value.
Definition: reldatefmt.h:88
UDAT_RELATIVE_HOURS
@ UDAT_RELATIVE_HOURS
Hours.
Definition: reldatefmt.h:57
UDAT_RELATIVE_MONTHS
@ UDAT_RELATIVE_MONTHS
Months.
Definition: reldatefmt.h:75
UDAT_RELATIVE_WEEKS
@ UDAT_RELATIVE_WEEKS
Weeks.
Definition: reldatefmt.h:69
UDAT_RELATIVE_YEARS
@ UDAT_RELATIVE_YEARS
Years.
Definition: reldatefmt.h:81
UDAT_RELATIVE_MINUTES
@ UDAT_RELATIVE_MINUTES
Minutes.
Definition: reldatefmt.h:51
UDAT_RELATIVE_SECONDS
@ UDAT_RELATIVE_SECONDS
Seconds.
Definition: reldatefmt.h:45
UDateDirection
UDateDirection
Represents a direction for an absolute unit e.g "Next Tuesday" or "Last Tuesday".
Definition: reldatefmt.h:204
UDAT_DIRECTION_PLAIN
@ UDAT_DIRECTION_PLAIN
Plain, which means the absence of a qualifier.
Definition: reldatefmt.h:240
UDAT_DIRECTION_THIS
@ UDAT_DIRECTION_THIS
This.
Definition: reldatefmt.h:222
UDAT_DIRECTION_LAST_2
@ UDAT_DIRECTION_LAST_2
Two before.
Definition: reldatefmt.h:210
UDAT_DIRECTION_LAST
@ UDAT_DIRECTION_LAST
Last.
Definition: reldatefmt.h:216
UDAT_DIRECTION_NEXT
@ UDAT_DIRECTION_NEXT
Next.
Definition: reldatefmt.h:228
UDAT_DIRECTION_NEXT_2
@ UDAT_DIRECTION_NEXT_2
Two after.
Definition: reldatefmt.h:234
UDAT_DIRECTION_COUNT
@ UDAT_DIRECTION_COUNT
One more than the highest normal UDateDirection value.
Definition: reldatefmt.h:247
UCONFIG_NO_BREAK_ITERATION
#define UCONFIG_NO_BREAK_ITERATION
This switch turns off break iteration.
Definition: uconfig.h:358
udisplaycontext.h
C API: Display context types (enum values)
UDisplayContext
UDisplayContext
Display context settings.
Definition: udisplaycontext.h:64
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
uobject.h
C++ API: Common ICU base class UObject.
ureldatefmt.h
C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
UDateRelativeDateTimeFormatterStyle
UDateRelativeDateTimeFormatterStyle
The formatting style.
Definition: ureldatefmt.h:48
URelativeDateTimeUnit
URelativeDateTimeUnit
Represents the unit for formatting a relative date.
Definition: ureldatefmt.h:81
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
U_INVALID_STATE_ERROR
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:479
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:450
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