ICU 72.1 72.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"
23#include "unicode/ureldatefmt.h"
24#include "unicode/locid.h"
26
32#if !UCONFIG_NO_FORMATTING
33
39typedef enum UDateRelativeUnit {
40
46
52
58
64
70
76
82
83#ifndef U_HIDE_DEPRECATED_API
89#endif // U_HIDE_DEPRECATED_API
91
96typedef enum UDateAbsoluteUnit {
97
98 // Days of week have to remain together and in order from Sunday to
99 // Saturday.
105
111
117
123
129
135
141
147
153
159
165
171
177
183
189
190#ifndef U_HIDE_DEPRECATED_API
196#endif // U_HIDE_DEPRECATED_API
198
204typedef enum UDateDirection {
205
211
217
223
229
235
241
242#ifndef U_HIDE_DEPRECATED_API
248#endif // U_HIDE_DEPRECATED_API
250
251#if !UCONFIG_NO_BREAK_ITERATION
252
253U_NAMESPACE_BEGIN
254
255class BreakIterator;
256class RelativeDateTimeCacheData;
257class SharedNumberFormat;
258class SharedPluralRules;
259class SharedBreakIterator;
260class NumberFormat;
261class UnicodeString;
262class FormattedRelativeDateTime;
263class FormattedRelativeDateTimeData;
264
275 public:
280 FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
281
287
293
296
299
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
397public:
398
404
410
422 const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
423
440 const Locale& locale,
441 NumberFormat *nfToAdopt,
443 UDisplayContext capitalizationContext,
444 UErrorCode& status);
445
451
457 const RelativeDateTimeFormatter& other);
458
464
485 double quantity,
486 UDateDirection direction,
488 UnicodeString& appendTo,
489 UErrorCode& status) const;
490
509 double quantity,
510 UDateDirection direction,
512 UErrorCode& status) const;
513
531 UDateDirection direction,
533 UnicodeString& appendTo,
534 UErrorCode& status) const;
535
552 UDateDirection direction,
554 UErrorCode& status) const;
555
577 double offset,
579 UnicodeString& appendTo,
580 UErrorCode& status) const;
581
601 double offset,
603 UErrorCode& status) const;
604
626 double offset,
628 UnicodeString& appendTo,
629 UErrorCode& status) const;
630
650 double offset,
652 UErrorCode& status) const;
653
667 const UnicodeString& relativeDateString,
668 const UnicodeString& timeString,
669 UnicodeString& appendTo,
670 UErrorCode& status) const;
671
678
685
692
693private:
694 const RelativeDateTimeCacheData* fCache;
695 const SharedNumberFormat *fNumberFormat;
696 const SharedPluralRules *fPluralRules;
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,
725 FormattedRelativeDateTimeData& output,
726 UErrorCode& status) const;
727 void formatAbsoluteImpl(
728 UDateDirection direction,
730 FormattedRelativeDateTimeData& output,
731 UErrorCode& status) const;
732 void formatNumericImpl(
733 double offset,
735 FormattedRelativeDateTimeData& output,
736 UErrorCode& status) const;
737 void formatRelativeImpl(
738 double offset,
740 FormattedRelativeDateTimeData& output,
741 UErrorCode& status) const;
742};
743
744U_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 */
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
The BreakIterator class implements methods for finding the location of boundaries in text.
Definition: brkiter.h:106
Represents a span of a string containing a given field.
An immutable class containing the result of a relative datetime formatting operation.
Definition: reldatefmt.h:274
FormattedRelativeDateTime()
Default constructor; makes an empty FormattedRelativeDateTime.
Definition: reldatefmt.h:280
virtual ~FormattedRelativeDateTime() U_OVERRIDE
Destruct an instance of FormattedRelativeDateTime.
FormattedRelativeDateTime(FormattedRelativeDateTime &&src) U_NOEXCEPT
Move constructor: Leaves the source FormattedRelativeDateTime in an undefined state.
An abstract formatted value: a string with associated field attributes.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
Formats simple relative dates.
Definition: reldatefmt.h:396
virtual ~RelativeDateTimeFormatter()
Destructor.
FormattedRelativeDateTime formatNumericToValue(double offset, URelativeDateTimeUnit unit, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a numeric style,...
UnicodeString & formatNumeric(double offset, URelativeDateTimeUnit unit, UnicodeString &appendTo, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a numeric style,...
UDateRelativeDateTimeFormatterStyle getFormatStyle() const
Returns the format style.
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,...
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".
RelativeDateTimeFormatter(UErrorCode &status)
Create RelativeDateTimeFormatter with default locale.
RelativeDateTimeFormatter(const Locale &locale, NumberFormat *nfToAdopt, UDateRelativeDateTimeFormatterStyle style, UDisplayContext capitalizationContext, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale, NumberFormat, and capitalization context.
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.
RelativeDateTimeFormatter(const Locale &locale, NumberFormat *nfToAdopt, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale and NumberFormat.
UnicodeString & format(UDateDirection direction, UDateAbsoluteUnit unit, UnicodeString &appendTo, UErrorCode &status) const
Formats a relative date without a quantity.
RelativeDateTimeFormatter(const RelativeDateTimeFormatter &other)
Copy constructor.
FormattedRelativeDateTime formatToValue(double offset, URelativeDateTimeUnit unit, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a text style if possible,...
UDisplayContext getCapitalizationContext() const
Returns the capitalization context.
RelativeDateTimeFormatter & operator=(const RelativeDateTimeFormatter &other)
Assignment operator.
RelativeDateTimeFormatter(const Locale &locale, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale.
const NumberFormat & getNumberFormat() const
Returns the NumberFormat this object is using.
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".
FormattedRelativeDateTime formatToValue(UDateDirection direction, UDateAbsoluteUnit unit, UErrorCode &status) const
Formats a relative date without a quantity.
UMemory is the common ICU base class.
Definition: uobject.h:115
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
C++ API: Abstract operations for localized strings.
C++ API: Locale ID object.
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
UDateAbsoluteUnit
Represents an absolute unit.
Definition: reldatefmt.h:96
@ UDAT_ABSOLUTE_DAY
Day.
Definition: reldatefmt.h:146
@ UDAT_ABSOLUTE_FRIDAY
Friday.
Definition: reldatefmt.h:134
@ UDAT_ABSOLUTE_YEAR
Year.
Definition: reldatefmt.h:164
@ UDAT_ABSOLUTE_WEDNESDAY
Wednesday.
Definition: reldatefmt.h:122
@ UDAT_ABSOLUTE_HOUR
Hour.
Definition: reldatefmt.h:182
@ UDAT_ABSOLUTE_WEEK
Week.
Definition: reldatefmt.h:152
@ UDAT_ABSOLUTE_NOW
Now.
Definition: reldatefmt.h:170
@ UDAT_ABSOLUTE_MINUTE
Minute.
Definition: reldatefmt.h:188
@ UDAT_ABSOLUTE_THURSDAY
Thursday.
Definition: reldatefmt.h:128
@ UDAT_ABSOLUTE_TUESDAY
Tuesday.
Definition: reldatefmt.h:116
@ UDAT_ABSOLUTE_MONTH
Month.
Definition: reldatefmt.h:158
@ UDAT_ABSOLUTE_SUNDAY
Sunday.
Definition: reldatefmt.h:104
@ UDAT_ABSOLUTE_UNIT_COUNT
One more than the highest normal UDateAbsoluteUnit value.
Definition: reldatefmt.h:195
@ UDAT_ABSOLUTE_MONDAY
Monday.
Definition: reldatefmt.h:110
@ UDAT_ABSOLUTE_SATURDAY
Saturday.
Definition: reldatefmt.h:140
@ UDAT_ABSOLUTE_QUARTER
Quarter.
Definition: reldatefmt.h:176
UDateRelativeUnit
Represents the unit for formatting a relative date.
Definition: reldatefmt.h:39
@ UDAT_RELATIVE_DAYS
Days.
Definition: reldatefmt.h:63
@ UDAT_RELATIVE_UNIT_COUNT
One more than the highest normal UDateRelativeUnit value.
Definition: reldatefmt.h:88
@ UDAT_RELATIVE_HOURS
Hours.
Definition: reldatefmt.h:57
@ UDAT_RELATIVE_MONTHS
Months.
Definition: reldatefmt.h:75
@ UDAT_RELATIVE_WEEKS
Weeks.
Definition: reldatefmt.h:69
@ UDAT_RELATIVE_YEARS
Years.
Definition: reldatefmt.h:81
@ UDAT_RELATIVE_MINUTES
Minutes.
Definition: reldatefmt.h:51
@ UDAT_RELATIVE_SECONDS
Seconds.
Definition: reldatefmt.h:45
UDateDirection
Represents a direction for an absolute unit e.g "Next Tuesday" or "Last Tuesday".
Definition: reldatefmt.h:204
@ UDAT_DIRECTION_PLAIN
Plain, which means the absence of a qualifier.
Definition: reldatefmt.h:240
@ UDAT_DIRECTION_THIS
This.
Definition: reldatefmt.h:222
@ UDAT_DIRECTION_LAST_2
Two before.
Definition: reldatefmt.h:210
@ UDAT_DIRECTION_LAST
Last.
Definition: reldatefmt.h:216
@ UDAT_DIRECTION_NEXT
Next.
Definition: reldatefmt.h:228
@ UDAT_DIRECTION_NEXT_2
Two after.
Definition: reldatefmt.h:234
@ UDAT_DIRECTION_COUNT
One more than the highest normal UDateDirection value.
Definition: reldatefmt.h:247
C API: Display context types (enum values)
UDisplayContext
Display context settings.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
#define U_OVERRIDE
Defined to the C++11 "override" keyword if available.
Definition: umachine.h:130
C++ API: Common ICU base class UObject.
C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
UDateRelativeDateTimeFormatterStyle
The formatting style.
Definition: ureldatefmt.h:48
URelativeDateTimeUnit
Represents the unit for formatting a relative date.
Definition: ureldatefmt.h:81
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301