ICU 71.1 71.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
84
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
191
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
243
247 UDAT_DIRECTION_COUNT
248 #endif // U_HIDE_DEPRECATED_API
249 } UDateDirection;
250
251 #if !UCONFIG_NO_BREAK_ITERATION
252
253 U_NAMESPACE_BEGIN
254
255 class BreakIterator;
256 class RelativeDateTimeCacheData;
257 class SharedNumberFormat;
258 class SharedPluralRules;
259 class SharedBreakIterator;
260 class NumberFormat;
261 class UnicodeString;
262 class FormattedRelativeDateTime;
263 class FormattedRelativeDateTimeData;
264
274 class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue {
275 public:
280 FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
281
286 FormattedRelativeDateTime(FormattedRelativeDateTime&& src) U_NOEXCEPT;
287
292 virtual ~FormattedRelativeDateTime() U_OVERRIDE;
293
295 FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete;
296
298 FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
299
304 FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) U_NOEXCEPT;
305
307 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
308
310 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
311
313 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
314
316 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
317
318 private:
319 FormattedRelativeDateTimeData *fData;
320 UErrorCode fErrorCode;
321 explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)
322 : fData(results), fErrorCode(U_ZERO_ERROR) {}
323 explicit FormattedRelativeDateTime(UErrorCode errorCode)
324 : fData(nullptr), fErrorCode(errorCode) {}
325 friend class RelativeDateTimeFormatter;
326 };
327
396 class U_I18N_API RelativeDateTimeFormatter : public UObject {
397 public:
398
403 RelativeDateTimeFormatter(UErrorCode& status);
404
409 RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
410
421 RelativeDateTimeFormatter(
422 const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
423
439 RelativeDateTimeFormatter(
440 const Locale& locale,
441 NumberFormat *nfToAdopt,
442 UDateRelativeDateTimeFormatterStyle style,
443 UDisplayContext capitalizationContext,
444 UErrorCode& status);
445
450 RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
451
456 RelativeDateTimeFormatter& operator=(
457 const RelativeDateTimeFormatter& other);
458
463 virtual ~RelativeDateTimeFormatter();
464
484 UnicodeString& format(
485 double quantity,
486 UDateDirection direction,
487 UDateRelativeUnit unit,
488 UnicodeString& appendTo,
489 UErrorCode& status) const;
490
508 FormattedRelativeDateTime formatToValue(
509 double quantity,
510 UDateDirection direction,
511 UDateRelativeUnit unit,
512 UErrorCode& status) const;
513
530 UnicodeString& format(
531 UDateDirection direction,
532 UDateAbsoluteUnit unit,
533 UnicodeString& appendTo,
534 UErrorCode& status) const;
535
551 FormattedRelativeDateTime formatToValue(
552 UDateDirection direction,
553 UDateAbsoluteUnit unit,
554 UErrorCode& status) const;
555
576 UnicodeString& formatNumeric(
577 double offset,
578 URelativeDateTimeUnit unit,
579 UnicodeString& appendTo,
580 UErrorCode& status) const;
581
600 FormattedRelativeDateTime formatNumericToValue(
601 double offset,
602 URelativeDateTimeUnit unit,
603 UErrorCode& status) const;
604
625 UnicodeString& format(
626 double offset,
627 URelativeDateTimeUnit unit,
628 UnicodeString& appendTo,
629 UErrorCode& status) const;
630
649 FormattedRelativeDateTime formatToValue(
650 double offset,
651 URelativeDateTimeUnit unit,
652 UErrorCode& status) const;
653
666 UnicodeString& combineDateAndTime(
667 const UnicodeString& relativeDateString,
668 const UnicodeString& timeString,
669 UnicodeString& appendTo,
670 UErrorCode& status) const;
671
677 const NumberFormat& getNumberFormat() const;
678
684 UDisplayContext getCapitalizationContext() const;
685
691 UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
692
693 private:
694 const RelativeDateTimeCacheData* fCache;
695 const SharedNumberFormat *fNumberFormat;
696 const SharedPluralRules *fPluralRules;
697 UDateRelativeDateTimeFormatterStyle fStyle;
698 UDisplayContext fContext;
699 const SharedBreakIterator *fOptBreakIterator;
700 Locale fLocale;
701 void init(
702 NumberFormat *nfToAdopt,
703 BreakIterator *brkIter,
704 UErrorCode &status);
705 UnicodeString& adjustForContext(UnicodeString &) const;
706 UBool checkNoAdjustForContext(UErrorCode& status) const;
707
708 template<typename F, typename... Args>
709 UnicodeString& doFormat(
710 F callback,
711 UnicodeString& appendTo,
712 UErrorCode& status,
713 Args... args) const;
714
715 template<typename F, typename... Args>
716 FormattedRelativeDateTime doFormatToValue(
717 F callback,
718 UErrorCode& status,
719 Args... args) const;
720
721 void formatImpl(
722 double quantity,
723 UDateDirection direction,
724 UDateRelativeUnit unit,
725 FormattedRelativeDateTimeData& output,
726 UErrorCode& status) const;
727 void formatAbsoluteImpl(
728 UDateDirection direction,
729 UDateAbsoluteUnit unit,
730 FormattedRelativeDateTimeData& output,
731 UErrorCode& status) const;
732 void formatNumericImpl(
733 double offset,
734 URelativeDateTimeUnit unit,
735 FormattedRelativeDateTimeData& output,
736 UErrorCode& status) const;
737 void formatRelativeImpl(
738 double offset,
739 URelativeDateTimeUnit unit,
740 FormattedRelativeDateTimeData& output,
741 UErrorCode& status) const;
742 };
743
744 U_NAMESPACE_END
745
746 #endif /* !UCONFIG_NO_BREAK_ITERATION */
747 #endif /* !UCONFIG_NO_FORMATTING */
748
749 #endif /* U_SHOW_CPLUSPLUS_API */
750
751 #endif /* __RELDATEFMT_H */
UDAT_ABSOLUTE_WEDNESDAY
@ UDAT_ABSOLUTE_WEDNESDAY
Wednesday.
Definition: reldatefmt.h:122
UDAT_DIRECTION_LAST
@ UDAT_DIRECTION_LAST
Last.
Definition: reldatefmt.h:216
UDAT_RELATIVE_SECONDS
@ UDAT_RELATIVE_SECONDS
Seconds.
Definition: reldatefmt.h:45
icu::BreakIterator
The BreakIterator class implements methods for finding the location of boundaries in text.
Definition: brkiter.h:106
icu::NumberFormat
Definition: numfmt.h:175
icu::RelativeDateTimeFormatter
Formats simple relative dates.
Definition: reldatefmt.h:396
UDAT_RELATIVE_MONTHS
@ UDAT_RELATIVE_MONTHS
Months.
Definition: reldatefmt.h:75
UDAT_ABSOLUTE_TUESDAY
@ UDAT_ABSOLUTE_TUESDAY
Tuesday.
Definition: reldatefmt.h:116
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UDAT_RELATIVE_MINUTES
@ UDAT_RELATIVE_MINUTES
Minutes.
Definition: reldatefmt.h:51
UDAT_ABSOLUTE_MINUTE
@ UDAT_ABSOLUTE_MINUTE
Minute.
Definition: reldatefmt.h:188
U_I18N_API
#define U_I18N_API
Definition: utypes.h:301
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime()
Default constructor; makes an empty FormattedRelativeDateTime.
Definition: reldatefmt.h:280
U_NOEXCEPT
#define U_NOEXCEPT
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:81
U_OVERRIDE
#define U_OVERRIDE
Definition: umachine.h:130
UDAT_ABSOLUTE_YEAR
@ UDAT_ABSOLUTE_YEAR
Year.
Definition: reldatefmt.h:164
UDAT_RELATIVE_YEARS
@ UDAT_RELATIVE_YEARS
Years.
Definition: reldatefmt.h:81
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:247
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:204
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::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:222
icu::FormattedRelativeDateTime
An immutable class containing the result of a relative datetime formatting operation.
Definition: reldatefmt.h:274
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:240
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
formattedvalue.h
C++ API: Abstract operations for localized strings.
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:182
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:210
UDateAbsoluteUnit
UDateAbsoluteUnit
Represents an absolute unit.
Definition: reldatefmt.h:96
UDAT_DIRECTION_NEXT
@ UDAT_DIRECTION_NEXT
Next.
Definition: reldatefmt.h:228
UDAT_ABSOLUTE_UNIT_COUNT
@ UDAT_ABSOLUTE_UNIT_COUNT
One more than the highest normal UDateAbsoluteUnit value.
Definition: reldatefmt.h:195
udisplaycontext.h
C API: Display context types (enum values)
locid.h
C++ API: Locale ID object.
UDateRelativeDateTimeFormatterStyle
UDateRelativeDateTimeFormatterStyle
The formatting style.
Definition: ureldatefmt.h:48
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
UDAT_ABSOLUTE_NOW
@ UDAT_ABSOLUTE_NOW
Now.
Definition: reldatefmt.h:170
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:234
UDAT_ABSOLUTE_THURSDAY
@ UDAT_ABSOLUTE_THURSDAY
Thursday.
Definition: reldatefmt.h:128