ICU 64.2 64.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 #include "unicode/uobject.h"
19 #include "unicode/udisplaycontext.h"
20 #include "unicode/ureldatefmt.h"
21 #include "unicode/locid.h"
22 #include "unicode/formattedvalue.h"
23
29 #if !UCONFIG_NO_FORMATTING
30
36 typedef enum UDateRelativeUnit {
37
42 UDAT_RELATIVE_SECONDS,
43
48 UDAT_RELATIVE_MINUTES,
49
54 UDAT_RELATIVE_HOURS,
55
60 UDAT_RELATIVE_DAYS,
61
66 UDAT_RELATIVE_WEEKS,
67
72 UDAT_RELATIVE_MONTHS,
73
78 UDAT_RELATIVE_YEARS,
79
80 #ifndef U_HIDE_DEPRECATED_API
81
85 UDAT_RELATIVE_UNIT_COUNT
86 #endif // U_HIDE_DEPRECATED_API
87 } UDateRelativeUnit;
88
93 typedef enum UDateAbsoluteUnit {
94
95 // Days of week have to remain together and in order from Sunday to
96 // Saturday.
101 UDAT_ABSOLUTE_SUNDAY,
102
107 UDAT_ABSOLUTE_MONDAY,
108
113 UDAT_ABSOLUTE_TUESDAY,
114
119 UDAT_ABSOLUTE_WEDNESDAY,
120
125 UDAT_ABSOLUTE_THURSDAY,
126
131 UDAT_ABSOLUTE_FRIDAY,
132
137 UDAT_ABSOLUTE_SATURDAY,
138
143 UDAT_ABSOLUTE_DAY,
144
149 UDAT_ABSOLUTE_WEEK,
150
155 UDAT_ABSOLUTE_MONTH,
156
161 UDAT_ABSOLUTE_YEAR,
162
167 UDAT_ABSOLUTE_NOW,
168
169 #ifndef U_HIDE_DRAFT_API
170
174 UDAT_ABSOLUTE_QUARTER,
175 #endif // U_HIDE_DRAFT_API
176
177 #ifndef U_HIDE_DEPRECATED_API
178
182 UDAT_ABSOLUTE_UNIT_COUNT = UDAT_ABSOLUTE_NOW + 2
183 #endif // U_HIDE_DEPRECATED_API
184 } UDateAbsoluteUnit;
185
191 typedef enum UDateDirection {
192
197 UDAT_DIRECTION_LAST_2,
198
203 UDAT_DIRECTION_LAST,
204
209 UDAT_DIRECTION_THIS,
210
215 UDAT_DIRECTION_NEXT,
216
221 UDAT_DIRECTION_NEXT_2,
222
227 UDAT_DIRECTION_PLAIN,
228
229 #ifndef U_HIDE_DEPRECATED_API
230
234 UDAT_DIRECTION_COUNT
235 #endif // U_HIDE_DEPRECATED_API
236 } UDateDirection;
237
238 #if !UCONFIG_NO_BREAK_ITERATION
239
240 U_NAMESPACE_BEGIN
241
242 class BreakIterator;
243 class RelativeDateTimeCacheData;
244 class SharedNumberFormat;
245 class SharedPluralRules;
246 class SharedBreakIterator;
247 class NumberFormat;
248 class UnicodeString;
249 class FormattedRelativeDateTimeData;
250
251 #ifndef U_HIDE_DRAFT_API
252
261 class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue {
262 public:
267 FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
268
273 FormattedRelativeDateTime(FormattedRelativeDateTime&& src) U_NOEXCEPT;
274
279 virtual ~FormattedRelativeDateTime() U_OVERRIDE;
280
282 FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete;
283
285 FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
286
291 FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) U_NOEXCEPT;
292
294 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
295
297 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
298
300 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
301
303 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
304
305 private:
306 FormattedRelativeDateTimeData *fData;
307 UErrorCode fErrorCode;
308 explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)
309 : fData(results), fErrorCode(U_ZERO_ERROR) {}
310 explicit FormattedRelativeDateTime(UErrorCode errorCode)
311 : fData(nullptr), fErrorCode(errorCode) {}
312 friend class RelativeDateTimeFormatter;
313 };
314 #endif /* U_HIDE_DRAFT_API */
315
384 class U_I18N_API RelativeDateTimeFormatter : public UObject {
385 public:
386
391 RelativeDateTimeFormatter(UErrorCode& status);
392
397 RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
398
409 RelativeDateTimeFormatter(
410 const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
411
427 RelativeDateTimeFormatter(
428 const Locale& locale,
429 NumberFormat *nfToAdopt,
430 UDateRelativeDateTimeFormatterStyle style,
431 UDisplayContext capitalizationContext,
432 UErrorCode& status);
433
438 RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
439
444 RelativeDateTimeFormatter& operator=(
445 const RelativeDateTimeFormatter& other);
446
451 virtual ~RelativeDateTimeFormatter();
452
472 UnicodeString& format(
473 double quantity,
474 UDateDirection direction,
475 UDateRelativeUnit unit,
476 UnicodeString& appendTo,
477 UErrorCode& status) const;
478
479 #ifndef U_HIDE_DRAFT_API
480
497 FormattedRelativeDateTime formatToValue(
498 double quantity,
499 UDateDirection direction,
500 UDateRelativeUnit unit,
501 UErrorCode& status) const;
502 #endif /* U_HIDE_DRAFT_API */
503
520 UnicodeString& format(
521 UDateDirection direction,
522 UDateAbsoluteUnit unit,
523 UnicodeString& appendTo,
524 UErrorCode& status) const;
525
526 #ifndef U_HIDE_DRAFT_API
527
542 FormattedRelativeDateTime formatToValue(
543 UDateDirection direction,
544 UDateAbsoluteUnit unit,
545 UErrorCode& status) const;
546 #endif /* U_HIDE_DRAFT_API */
547
568 UnicodeString& formatNumeric(
569 double offset,
570 URelativeDateTimeUnit unit,
571 UnicodeString& appendTo,
572 UErrorCode& status) const;
573
574 #ifndef U_HIDE_DRAFT_API
575
593 FormattedRelativeDateTime formatNumericToValue(
594 double offset,
595 URelativeDateTimeUnit unit,
596 UErrorCode& status) const;
597 #endif /* U_HIDE_DRAFT_API */
598
619 UnicodeString& format(
620 double offset,
621 URelativeDateTimeUnit unit,
622 UnicodeString& appendTo,
623 UErrorCode& status) const;
624
625 #ifndef U_HIDE_DRAFT_API
626
644 FormattedRelativeDateTime formatToValue(
645 double offset,
646 URelativeDateTimeUnit unit,
647 UErrorCode& status) const;
648 #endif /* U_HIDE_DRAFT_API */
649
662 UnicodeString& combineDateAndTime(
663 const UnicodeString& relativeDateString,
664 const UnicodeString& timeString,
665 UnicodeString& appendTo,
666 UErrorCode& status) const;
667
673 const NumberFormat& getNumberFormat() const;
674
680 UDisplayContext getCapitalizationContext() const;
681
687 UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
688
689 private:
690 const RelativeDateTimeCacheData* fCache;
691 const SharedNumberFormat *fNumberFormat;
692 const SharedPluralRules *fPluralRules;
693 UDateRelativeDateTimeFormatterStyle fStyle;
694 UDisplayContext fContext;
695 const SharedBreakIterator *fOptBreakIterator;
696 Locale fLocale;
697 void init(
698 NumberFormat *nfToAdopt,
699 BreakIterator *brkIter,
700 UErrorCode &status);
701 UnicodeString& adjustForContext(UnicodeString &) const;
702 UBool checkNoAdjustForContext(UErrorCode& status) const;
703
704 template<typename F, typename... Args>
705 UnicodeString& doFormat(
706 F callback,
707 UnicodeString& appendTo,
708 UErrorCode& status,
709 Args... args) const;
710
711 #ifndef U_HIDE_DRAFT_API // for FormattedRelativeDateTime
712 template<typename F, typename... Args>
713 FormattedRelativeDateTime doFormatToValue(
714 F callback,
715 UErrorCode& status,
716 Args... args) const;
717 #endif // U_HIDE_DRAFT_API
718
719 void formatImpl(
720 double quantity,
721 UDateDirection direction,
722 UDateRelativeUnit unit,
723 FormattedRelativeDateTimeData& output,
724 UErrorCode& status) const;
725 void formatAbsoluteImpl(
726 UDateDirection direction,
727 UDateAbsoluteUnit unit,
728 FormattedRelativeDateTimeData& output,
729 UErrorCode& status) const;
730 void formatNumericImpl(
731 double offset,
732 URelativeDateTimeUnit unit,
733 FormattedRelativeDateTimeData& output,
734 UErrorCode& status) const;
735 void formatRelativeImpl(
736 double offset,
737 URelativeDateTimeUnit unit,
738 FormattedRelativeDateTimeData& output,
739 UErrorCode& status) const;
740 };
741
742 U_NAMESPACE_END
743
744 #endif /* !UCONFIG_NO_BREAK_ITERATION */
745 #endif /* !UCONFIG_NO_FORMATTING */
746 #endif /* __RELDATEFMT_H */
formattedvalue.h
C++ API: Abstract operations for localized strings.
icu::FormattedRelativeDateTime
An immutable class containing the result of a relative datetime formatting operation.
Definition: reldatefmt.h:261
UDAT_ABSOLUTE_DAY
Day.
Definition: reldatefmt.h:143
UDAT_RELATIVE_MINUTES
Minutes.
Definition: reldatefmt.h:48
U_OVERRIDE
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:129
UDAT_ABSOLUTE_UNIT_COUNT
One more than the highest normal UDateAbsoluteUnit value.
Definition: reldatefmt.h:182
URelativeDateTimeUnit
URelativeDateTimeUnit
Represents the unit for formatting a relative date.
Definition: ureldatefmt.h:78
UDAT_ABSOLUTE_THURSDAY
Thursday.
Definition: reldatefmt.h:125
UDAT_DIRECTION_NEXT_2
Two after.
Definition: reldatefmt.h:221
icu::NumberFormat
Definition: numfmt.h:173
UDAT_ABSOLUTE_QUARTER
Quarter.
Definition: reldatefmt.h:174
UDisplayContext
UDisplayContext
Display context settings.
Definition: udisplaycontext.h:64
UDAT_RELATIVE_WEEKS
Weeks.
Definition: reldatefmt.h:66
UDAT_ABSOLUTE_MONDAY
Monday.
Definition: reldatefmt.h:107
UDAT_ABSOLUTE_FRIDAY
Friday.
Definition: reldatefmt.h:131
UDAT_RELATIVE_YEARS
Years.
Definition: reldatefmt.h:78
U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:435
udisplaycontext.h
C API: Display context types (enum values)
UDAT_ABSOLUTE_SUNDAY
Sunday.
Definition: reldatefmt.h:101
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition: formattedvalue.h:241
UDAT_DIRECTION_LAST_2
Two before.
Definition: reldatefmt.h:197
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
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
ureldatefmt.h
C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
icu::BreakIterator
The BreakIterator class implements methods for finding the location of boundaries in text.
Definition: brkiter.h:102
UDAT_RELATIVE_DAYS
Days.
Definition: reldatefmt.h:60
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime()
Default constructor; makes an empty FormattedRelativeDateTime.
Definition: reldatefmt.h:267
UDateRelativeDateTimeFormatterStyle
UDateRelativeDateTimeFormatterStyle
The formatting style.
Definition: ureldatefmt.h:45
UDateDirection
UDateDirection
Represents a direction for an absolute unit e.g "Next Tuesday" or "Last Tuesday".
Definition: reldatefmt.h:191
UDAT_DIRECTION_PLAIN
Plain, which means the absence of a qualifier.
Definition: reldatefmt.h:227
icu::RelativeDateTimeFormatter
Formats simple relative dates.
Definition: reldatefmt.h:384
uobject.h
C++ API: Common ICU base class UObject.
UDAT_ABSOLUTE_TUESDAY
Tuesday.
Definition: reldatefmt.h:113
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:40
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:503
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UErrorCode
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers,...
Definition: utypes.h:401
UDAT_ABSOLUTE_WEEK
Week.
Definition: reldatefmt.h:149
UDAT_DIRECTION_COUNT
One more than the highest normal UDateDirection value.
Definition: reldatefmt.h:234
UDateAbsoluteUnit
UDateAbsoluteUnit
Represents an absolute unit.
Definition: reldatefmt.h:93
UDAT_ABSOLUTE_YEAR
Year.
Definition: reldatefmt.h:161
UDAT_ABSOLUTE_SATURDAY
Saturday.
Definition: reldatefmt.h:137
locid.h
C++ API: Locale ID object.
UDAT_ABSOLUTE_NOW
Now.
Definition: reldatefmt.h:167
UDAT_DIRECTION_THIS
This.
Definition: reldatefmt.h:209
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UDAT_RELATIVE_HOURS
Hours.
Definition: reldatefmt.h:54
UDAT_ABSOLUTE_WEDNESDAY
Wednesday.
Definition: reldatefmt.h:119
UDAT_RELATIVE_SECONDS
Seconds.
Definition: reldatefmt.h:42
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:289
UDAT_RELATIVE_MONTHS
Months.
Definition: reldatefmt.h:72
UDAT_RELATIVE_UNIT_COUNT
One more than the highest normal UDateRelativeUnit value.
Definition: reldatefmt.h:85
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:112
U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:464
UDAT_ABSOLUTE_MONTH
Month.
Definition: reldatefmt.h:155
UDateRelativeUnit
UDateRelativeUnit
Represents the unit for formatting a relative date.
Definition: reldatefmt.h:36
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:51
UDAT_DIRECTION_LAST
Last.
Definition: reldatefmt.h:203
UDAT_DIRECTION_NEXT
Next.
Definition: reldatefmt.h:215
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192