ICU 65.1 65.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
267 #ifndef U_HIDE_DRAFT_API
268
277 class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue {
278 public:
283 FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
284
289 FormattedRelativeDateTime(FormattedRelativeDateTime&& src) U_NOEXCEPT;
290
295 virtual ~FormattedRelativeDateTime() U_OVERRIDE;
296
298 FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete;
299
301 FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
302
307 FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) U_NOEXCEPT;
308
310 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
311
313 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
314
316 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
317
319 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
320
321 private:
322 FormattedRelativeDateTimeData *fData;
323 UErrorCode fErrorCode;
324 explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)
325 : fData(results), fErrorCode(U_ZERO_ERROR) {}
326 explicit FormattedRelativeDateTime(UErrorCode errorCode)
327 : fData(nullptr), fErrorCode(errorCode) {}
328 friend class RelativeDateTimeFormatter;
329 };
330 #endif /* U_HIDE_DRAFT_API */
331
400 class U_I18N_API RelativeDateTimeFormatter : public UObject {
401 public:
402
407 RelativeDateTimeFormatter(UErrorCode& status);
408
413 RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
414
425 RelativeDateTimeFormatter(
426 const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
427
443 RelativeDateTimeFormatter(
444 const Locale& locale,
445 NumberFormat *nfToAdopt,
446 UDateRelativeDateTimeFormatterStyle style,
447 UDisplayContext capitalizationContext,
448 UErrorCode& status);
449
454 RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
455
460 RelativeDateTimeFormatter& operator=(
461 const RelativeDateTimeFormatter& other);
462
467 virtual ~RelativeDateTimeFormatter();
468
488 UnicodeString& format(
489 double quantity,
490 UDateDirection direction,
491 UDateRelativeUnit unit,
492 UnicodeString& appendTo,
493 UErrorCode& status) const;
494
495 #ifndef U_HIDE_DRAFT_API
496
513 FormattedRelativeDateTime formatToValue(
514 double quantity,
515 UDateDirection direction,
516 UDateRelativeUnit unit,
517 UErrorCode& status) const;
518 #endif /* U_HIDE_DRAFT_API */
519
536 UnicodeString& format(
537 UDateDirection direction,
538 UDateAbsoluteUnit unit,
539 UnicodeString& appendTo,
540 UErrorCode& status) const;
541
542 #ifndef U_HIDE_DRAFT_API
543
558 FormattedRelativeDateTime formatToValue(
559 UDateDirection direction,
560 UDateAbsoluteUnit unit,
561 UErrorCode& status) const;
562 #endif /* U_HIDE_DRAFT_API */
563
584 UnicodeString& formatNumeric(
585 double offset,
586 URelativeDateTimeUnit unit,
587 UnicodeString& appendTo,
588 UErrorCode& status) const;
589
590 #ifndef U_HIDE_DRAFT_API
591
609 FormattedRelativeDateTime formatNumericToValue(
610 double offset,
611 URelativeDateTimeUnit unit,
612 UErrorCode& status) const;
613 #endif /* U_HIDE_DRAFT_API */
614
635 UnicodeString& format(
636 double offset,
637 URelativeDateTimeUnit unit,
638 UnicodeString& appendTo,
639 UErrorCode& status) const;
640
641 #ifndef U_HIDE_DRAFT_API
642
660 FormattedRelativeDateTime formatToValue(
661 double offset,
662 URelativeDateTimeUnit unit,
663 UErrorCode& status) const;
664 #endif /* U_HIDE_DRAFT_API */
665
678 UnicodeString& combineDateAndTime(
679 const UnicodeString& relativeDateString,
680 const UnicodeString& timeString,
681 UnicodeString& appendTo,
682 UErrorCode& status) const;
683
689 const NumberFormat& getNumberFormat() const;
690
696 UDisplayContext getCapitalizationContext() const;
697
703 UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
704
705 private:
706 const RelativeDateTimeCacheData* fCache;
707 const SharedNumberFormat *fNumberFormat;
708 const SharedPluralRules *fPluralRules;
709 UDateRelativeDateTimeFormatterStyle fStyle;
710 UDisplayContext fContext;
711 const SharedBreakIterator *fOptBreakIterator;
712 Locale fLocale;
713 void init(
714 NumberFormat *nfToAdopt,
715 BreakIterator *brkIter,
716 UErrorCode &status);
717 UnicodeString& adjustForContext(UnicodeString &) const;
718 UBool checkNoAdjustForContext(UErrorCode& status) const;
719
720 template<typename F, typename... Args>
721 UnicodeString& doFormat(
722 F callback,
723 UnicodeString& appendTo,
724 UErrorCode& status,
725 Args... args) const;
726
727 #ifndef U_HIDE_DRAFT_API // for FormattedRelativeDateTime
728 template<typename F, typename... Args>
729 FormattedRelativeDateTime doFormatToValue(
730 F callback,
731 UErrorCode& status,
732 Args... args) const;
733 #endif // U_HIDE_DRAFT_API
734
735 void formatImpl(
736 double quantity,
737 UDateDirection direction,
738 UDateRelativeUnit unit,
739 FormattedRelativeDateTimeData& output,
740 UErrorCode& status) const;
741 void formatAbsoluteImpl(
742 UDateDirection direction,
743 UDateAbsoluteUnit unit,
744 FormattedRelativeDateTimeData& output,
745 UErrorCode& status) const;
746 void formatNumericImpl(
747 double offset,
748 URelativeDateTimeUnit unit,
749 FormattedRelativeDateTimeData& output,
750 UErrorCode& status) const;
751 void formatRelativeImpl(
752 double offset,
753 URelativeDateTimeUnit unit,
754 FormattedRelativeDateTimeData& output,
755 UErrorCode& status) const;
756 };
757
758 U_NAMESPACE_END
759
760 #endif /* !UCONFIG_NO_BREAK_ITERATION */
761 #endif /* !UCONFIG_NO_FORMATTING */
762
763 #endif /* U_SHOW_CPLUSPLUS_API */
764
765 #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:277
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:254
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:283
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:400
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:46
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