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"
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
178 #ifndef U_HIDE_DRAFT_API
179
183 UDAT_ABSOLUTE_HOUR,
184
189 UDAT_ABSOLUTE_MINUTE,
190 #endif // U_HIDE_DRAFT_API
191
192 #ifndef U_HIDE_DEPRECATED_API
193
197 UDAT_ABSOLUTE_UNIT_COUNT = UDAT_ABSOLUTE_NOW + 4
198 #endif // U_HIDE_DEPRECATED_API
199 } UDateAbsoluteUnit;
200
206 typedef enum UDateDirection {
207
212 UDAT_DIRECTION_LAST_2,
213
218 UDAT_DIRECTION_LAST,
219
224 UDAT_DIRECTION_THIS,
225
230 UDAT_DIRECTION_NEXT,
231
236 UDAT_DIRECTION_NEXT_2,
237
242 UDAT_DIRECTION_PLAIN,
243
244 #ifndef U_HIDE_DEPRECATED_API
245
249 UDAT_DIRECTION_COUNT
250 #endif // U_HIDE_DEPRECATED_API
251 } UDateDirection;
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
276 class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue {
277 public:
282 FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
283
288 FormattedRelativeDateTime(FormattedRelativeDateTime&& src) U_NOEXCEPT;
289
294 virtual ~FormattedRelativeDateTime() U_OVERRIDE;
295
297 FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete;
298
300 FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
301
306 FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) U_NOEXCEPT;
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
398 class U_I18N_API RelativeDateTimeFormatter : public UObject {
399 public:
400
405 RelativeDateTimeFormatter(UErrorCode& status);
406
411 RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
412
423 RelativeDateTimeFormatter(
424 const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
425
441 RelativeDateTimeFormatter(
442 const Locale& locale,
443 NumberFormat *nfToAdopt,
444 UDateRelativeDateTimeFormatterStyle style,
445 UDisplayContext capitalizationContext,
446 UErrorCode& status);
447
452 RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
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,
580 URelativeDateTimeUnit unit,
581 UnicodeString& appendTo,
582 UErrorCode& status) const;
583
602 FormattedRelativeDateTime formatNumericToValue(
603 double offset,
604 URelativeDateTimeUnit unit,
605 UErrorCode& status) const;
606
627 UnicodeString& format(
628 double offset,
629 URelativeDateTimeUnit unit,
630 UnicodeString& appendTo,
631 UErrorCode& status) const;
632
651 FormattedRelativeDateTime formatToValue(
652 double offset,
653 URelativeDateTimeUnit unit,
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;
699 UDateRelativeDateTimeFormatterStyle fStyle;
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,
736 URelativeDateTimeUnit unit,
737 FormattedRelativeDateTimeData& output,
738 UErrorCode& status) const;
739 void formatRelativeImpl(
740 double offset,
741 URelativeDateTimeUnit unit,
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 */
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:276
UDAT_ABSOLUTE_DAY
Day.
Definition: reldatefmt.h:146
UDAT_RELATIVE_MINUTES
Minutes.
Definition: reldatefmt.h:51
icu::FormattedValue::nextPosition
virtual UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const =0
Iterates over field positions in the FormattedValue.
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:197
URelativeDateTimeUnit
URelativeDateTimeUnit
Represents the unit for formatting a relative date.
Definition: ureldatefmt.h:78
icu::FormattedValue::toString
virtual UnicodeString toString(UErrorCode &status) const =0
Returns the formatted string as a self-contained UnicodeString.
UDAT_ABSOLUTE_THURSDAY
Thursday.
Definition: reldatefmt.h:128
UDAT_DIRECTION_NEXT_2
Two after.
Definition: reldatefmt.h:236
icu::NumberFormat
Definition: numfmt.h:175
UDAT_ABSOLUTE_QUARTER
Quarter.
Definition: reldatefmt.h:176
UDisplayContext
UDisplayContext
Display context settings.
Definition: udisplaycontext.h:64
UDAT_RELATIVE_WEEKS
Weeks.
Definition: reldatefmt.h:69
UDAT_ABSOLUTE_MONDAY
Monday.
Definition: reldatefmt.h:110
UDAT_ABSOLUTE_FRIDAY
Friday.
Definition: reldatefmt.h:134
UDAT_RELATIVE_YEARS
Years.
Definition: reldatefmt.h:81
U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
udisplaycontext.h
C API: Display context types (enum values)
UDAT_ABSOLUTE_SUNDAY
Sunday.
Definition: reldatefmt.h:104
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:212
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
ureldatefmt.h
C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
UDAT_ABSOLUTE_MINUTE
Minute.
Definition: reldatefmt.h:189
icu::BreakIterator
The BreakIterator class implements methods for finding the location of boundaries in text...
Definition: brkiter.h:106
UDAT_RELATIVE_DAYS
Days.
Definition: reldatefmt.h:63
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime()
Default constructor; makes an empty FormattedRelativeDateTime.
Definition: reldatefmt.h:282
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:206
UDAT_DIRECTION_PLAIN
Plain, which means the absence of a qualifier.
Definition: reldatefmt.h:242
icu::RelativeDateTimeFormatter
Formats simple relative dates.
Definition: reldatefmt.h:398
uobject.h
C++ API: Common ICU base class UObject.
UDAT_ABSOLUTE_TUESDAY
Tuesday.
Definition: reldatefmt.h:116
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:41
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
UDAT_ABSOLUTE_WEEK
Week.
Definition: reldatefmt.h:152
UDAT_DIRECTION_COUNT
One more than the highest normal UDateDirection value.
Definition: reldatefmt.h:249
UDateAbsoluteUnit
UDateAbsoluteUnit
Represents an absolute unit.
Definition: reldatefmt.h:96
UDAT_ABSOLUTE_YEAR
Year.
Definition: reldatefmt.h:164
UDAT_ABSOLUTE_SATURDAY
Saturday.
Definition: reldatefmt.h:140
locid.h
C++ API: Locale ID object.
UDAT_ABSOLUTE_NOW
Now.
Definition: reldatefmt.h:170
UDAT_DIRECTION_THIS
This.
Definition: reldatefmt.h:224
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UDAT_RELATIVE_HOURS
Hours.
Definition: reldatefmt.h:57
icu::FormattedValue::appendTo
virtual Appendable & appendTo(Appendable &appendable, UErrorCode &status) const =0
Appends the formatted string to an Appendable.
UDAT_ABSOLUTE_WEDNESDAY
Wednesday.
Definition: reldatefmt.h:122
UDAT_RELATIVE_SECONDS
Seconds.
Definition: reldatefmt.h:45
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
UDAT_RELATIVE_MONTHS
Months.
Definition: reldatefmt.h:75
UDAT_RELATIVE_UNIT_COUNT
One more than the highest normal UDateRelativeUnit value.
Definition: reldatefmt.h:88
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:115
UDAT_ABSOLUTE_HOUR
Hour.
Definition: reldatefmt.h:183
U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
UDAT_ABSOLUTE_MONTH
Month.
Definition: reldatefmt.h:158
UDateRelativeUnit
UDateRelativeUnit
Represents the unit for formatting a relative date.
Definition: reldatefmt.h:39
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:261
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
icu::FormattedValue::toTempString
virtual UnicodeString toTempString(UErrorCode &status) const =0
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
UDAT_DIRECTION_LAST
Last.
Definition: reldatefmt.h:218
UDAT_DIRECTION_NEXT
Next.
Definition: reldatefmt.h:230
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195