Loading...
Searching...
No Matches
reldatefmt.h
Go to the documentation of this file.
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*****************************************************************************
* Copyright (C) 2014-2016, International Business Machines Corporation and
* others.
* All Rights Reserved.
*****************************************************************************
*
* File RELDATEFMT.H
*****************************************************************************
*/
#ifndef __RELDATEFMT_H
#define __RELDATEFMT_H
#include "unicode/utypes.h"
#if U_SHOW_CPLUSPLUS_API
#include "unicode/uobject.h"
#include "unicode/udisplaycontext.h"
#include "unicode/ureldatefmt.h"
#include "unicode/locid.h"
#include "unicode/formattedvalue.h"
#if !UCONFIG_NO_FORMATTING
typedef enum UDateRelativeUnit {
UDAT_RELATIVE_SECONDS,
UDAT_RELATIVE_MINUTES,
UDAT_RELATIVE_HOURS,
UDAT_RELATIVE_DAYS,
UDAT_RELATIVE_WEEKS,
UDAT_RELATIVE_MONTHS,
UDAT_RELATIVE_YEARS,
#ifndef U_HIDE_DEPRECATED_API
UDAT_RELATIVE_UNIT_COUNT
#endif // U_HIDE_DEPRECATED_API
} UDateRelativeUnit;
typedef enum UDateAbsoluteUnit {
// Days of week have to remain together and in order from Sunday to
// Saturday.
UDAT_ABSOLUTE_SUNDAY,
UDAT_ABSOLUTE_MONDAY,
UDAT_ABSOLUTE_TUESDAY,
UDAT_ABSOLUTE_WEDNESDAY,
UDAT_ABSOLUTE_THURSDAY,
UDAT_ABSOLUTE_FRIDAY,
UDAT_ABSOLUTE_SATURDAY,
UDAT_ABSOLUTE_DAY,
UDAT_ABSOLUTE_WEEK,
UDAT_ABSOLUTE_MONTH,
UDAT_ABSOLUTE_YEAR,
UDAT_ABSOLUTE_NOW,
UDAT_ABSOLUTE_QUARTER,
UDAT_ABSOLUTE_HOUR,
UDAT_ABSOLUTE_MINUTE,
#ifndef U_HIDE_DEPRECATED_API
UDAT_ABSOLUTE_UNIT_COUNT = UDAT_ABSOLUTE_NOW + 4
#endif // U_HIDE_DEPRECATED_API
} UDateAbsoluteUnit;
typedef enum UDateDirection {
UDAT_DIRECTION_LAST_2,
UDAT_DIRECTION_LAST,
UDAT_DIRECTION_THIS,
UDAT_DIRECTION_NEXT,
UDAT_DIRECTION_NEXT_2,
UDAT_DIRECTION_PLAIN,
#ifndef U_HIDE_DEPRECATED_API
UDAT_DIRECTION_COUNT
#endif // U_HIDE_DEPRECATED_API
} UDateDirection;
U_NAMESPACE_BEGIN
class BreakIterator;
class RelativeDateTimeCacheData;
class SharedNumberFormat;
class SharedPluralRules;
class SharedBreakIterator;
class NumberFormat;
class UnicodeString;
class FormattedRelativeDateTime;
class FormattedRelativeDateTimeData;
class U_I18N_API FormattedRelativeDateTime : public UMemory, public FormattedValue {
public:
FormattedRelativeDateTime() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
FormattedRelativeDateTime(FormattedRelativeDateTime&& src) noexcept;
virtual ~FormattedRelativeDateTime() override;
FormattedRelativeDateTime(const FormattedRelativeDateTime&) = delete;
FormattedRelativeDateTime& operator=(const FormattedRelativeDateTime&) = delete;
FormattedRelativeDateTime& operator=(FormattedRelativeDateTime&& src) noexcept;
UnicodeString toString(UErrorCode& status) const override;
UnicodeString toTempString(UErrorCode& status) const override;
Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
private:
FormattedRelativeDateTimeData *fData;
UErrorCode fErrorCode;
explicit FormattedRelativeDateTime(FormattedRelativeDateTimeData *results)
: fData(results), fErrorCode(U_ZERO_ERROR) {}
explicit FormattedRelativeDateTime(UErrorCode errorCode)
: fData(nullptr), fErrorCode(errorCode) {}
friend class RelativeDateTimeFormatter;
};
class U_I18N_API_CLASS RelativeDateTimeFormatter : public UObject {
public:
U_I18N_API RelativeDateTimeFormatter(UErrorCode& status);
U_I18N_API RelativeDateTimeFormatter(const Locale& locale, UErrorCode& status);
U_I18N_API RelativeDateTimeFormatter(
const Locale& locale, NumberFormat *nfToAdopt, UErrorCode& status);
U_I18N_API RelativeDateTimeFormatter(
const Locale& locale,
NumberFormat *nfToAdopt,
UDateRelativeDateTimeFormatterStyle style,
UDisplayContext capitalizationContext,
UErrorCode& status);
U_I18N_API RelativeDateTimeFormatter(const RelativeDateTimeFormatter& other);
U_I18N_API RelativeDateTimeFormatter& operator=(
const RelativeDateTimeFormatter& other);
U_I18N_API virtual ~RelativeDateTimeFormatter();
U_I18N_API UnicodeString& format(
double quantity,
UDateDirection direction,
UDateRelativeUnit unit,
UnicodeString& appendTo,
UErrorCode& status) const;
U_I18N_API FormattedRelativeDateTime formatToValue(
double quantity,
UDateDirection direction,
UDateRelativeUnit unit,
UErrorCode& status) const;
U_I18N_API UnicodeString& format(
UDateDirection direction,
UDateAbsoluteUnit unit,
UnicodeString& appendTo,
UErrorCode& status) const;
U_I18N_API FormattedRelativeDateTime formatToValue(
UDateDirection direction,
UDateAbsoluteUnit unit,
UErrorCode& status) const;
U_I18N_API UnicodeString& formatNumeric(
double offset,
URelativeDateTimeUnit unit,
UnicodeString& appendTo,
UErrorCode& status) const;
U_I18N_API FormattedRelativeDateTime formatNumericToValue(
double offset,
URelativeDateTimeUnit unit,
UErrorCode& status) const;
U_I18N_API UnicodeString& format(
double offset,
URelativeDateTimeUnit unit,
UnicodeString& appendTo,
UErrorCode& status) const;
U_I18N_API FormattedRelativeDateTime formatToValue(
double offset,
URelativeDateTimeUnit unit,
UErrorCode& status) const;
U_I18N_API UnicodeString& combineDateAndTime(
const UnicodeString& relativeDateString,
const UnicodeString& timeString,
UnicodeString& appendTo,
UErrorCode& status) const;
U_I18N_API const NumberFormat& getNumberFormat() const;
U_I18N_API UDisplayContext getCapitalizationContext() const;
U_I18N_API UDateRelativeDateTimeFormatterStyle getFormatStyle() const;
private:
const RelativeDateTimeCacheData* fCache;
const SharedNumberFormat *fNumberFormat;
const SharedPluralRules *fPluralRules;
UDateRelativeDateTimeFormatterStyle fStyle;
UDisplayContext fContext;
#if !UCONFIG_NO_BREAK_ITERATION
const SharedBreakIterator *fOptBreakIterator;
#else
std::nullptr_t fOptBreakIterator = nullptr;
#endif // !UCONFIG_NO_BREAK_ITERATION
Locale fLocale;
void init(
NumberFormat *nfToAdopt,
#if !UCONFIG_NO_BREAK_ITERATION
BreakIterator *brkIter,
#else
std::nullptr_t,
#endif // !UCONFIG_NO_BREAK_ITERATION
UErrorCode &status);
UnicodeString& adjustForContext(UnicodeString &) const;
UBool checkNoAdjustForContext(UErrorCode& status) const;
template<typename F, typename... Args>
UnicodeString& doFormat(
F callback,
UnicodeString& appendTo,
UErrorCode& status,
Args... args) const;
template<typename F, typename... Args>
FormattedRelativeDateTime doFormatToValue(
F callback,
UErrorCode& status,
Args... args) const;
void formatImpl(
double quantity,
UDateDirection direction,
UDateRelativeUnit unit,
FormattedRelativeDateTimeData& output,
UErrorCode& status) const;
void formatAbsoluteImpl(
UDateDirection direction,
UDateAbsoluteUnit unit,
FormattedRelativeDateTimeData& output,
UErrorCode& status) const;
void formatNumericImpl(
double offset,
URelativeDateTimeUnit unit,
FormattedRelativeDateTimeData& output,
UErrorCode& status) const;
void formatRelativeImpl(
double offset,
URelativeDateTimeUnit unit,
FormattedRelativeDateTimeData& output,
UErrorCode& status) const;
};
U_NAMESPACE_END
#endif /* !UCONFIG_NO_FORMATTING */
#endif /* U_SHOW_CPLUSPLUS_API */
#endif /* __RELDATEFMT_H */
icu::BreakIterator
The BreakIterator class implements methods for finding the location of boundaries in text.
Definition brkiter.h:106
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition formattedvalue.h:41
icu::FormattedRelativeDateTime
An immutable class containing the result of a relative datetime formatting operation.
Definition reldatefmt.h:272
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime(FormattedRelativeDateTime &&src) noexcept
Move constructor: Leaves the source FormattedRelativeDateTime in an undefined state.
icu::FormattedRelativeDateTime::operator=
FormattedRelativeDateTime & operator=(const FormattedRelativeDateTime &)=delete
Copying not supported; use move assignment instead.
icu::FormattedRelativeDateTime::operator=
FormattedRelativeDateTime & operator=(FormattedRelativeDateTime &&src) noexcept
Move assignment: Leaves the source FormattedRelativeDateTime in an undefined state.
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime()
Default constructor; makes an empty FormattedRelativeDateTime.
Definition reldatefmt.h:278
icu::FormattedRelativeDateTime::toString
UnicodeString toString(UErrorCode &status) const override
Returns the formatted string as a self-contained UnicodeString.
icu::FormattedRelativeDateTime::nextPosition
UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const override
Iterates over field positions in the FormattedValue.
icu::FormattedRelativeDateTime::~FormattedRelativeDateTime
virtual ~FormattedRelativeDateTime() override
Destruct an instance of FormattedRelativeDateTime.
icu::FormattedRelativeDateTime::toTempString
UnicodeString toTempString(UErrorCode &status) const override
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
icu::FormattedRelativeDateTime::FormattedRelativeDateTime
FormattedRelativeDateTime(const FormattedRelativeDateTime &)=delete
Copying not supported; use move constructor instead.
icu::FormattedRelativeDateTime::appendTo
Appendable & appendTo(Appendable &appendable, UErrorCode &status) const override
Appends the formatted string to an Appendable.
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition formattedvalue.h:241
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:198
icu::NumberFormat
Definition numfmt.h:175
icu::RelativeDateTimeFormatter::formatToValue
U_I18N_API FormattedRelativeDateTime formatToValue(UDateDirection direction, UDateAbsoluteUnit unit, UErrorCode &status) const
Formats a relative date without a quantity.
icu::RelativeDateTimeFormatter::formatToValue
U_I18N_API 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".
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
U_I18N_API RelativeDateTimeFormatter(const Locale &locale, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale.
icu::RelativeDateTimeFormatter::format
U_I18N_API 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".
icu::RelativeDateTimeFormatter::formatToValue
U_I18N_API FormattedRelativeDateTime formatToValue(double offset, URelativeDateTimeUnit unit, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a text style if possible,...
icu::RelativeDateTimeFormatter::format
U_I18N_API UnicodeString & format(UDateDirection direction, UDateAbsoluteUnit unit, UnicodeString &appendTo, UErrorCode &status) const
Formats a relative date without a quantity.
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
U_I18N_API RelativeDateTimeFormatter(UErrorCode &status)
Create RelativeDateTimeFormatter with default locale.
icu::RelativeDateTimeFormatter::~RelativeDateTimeFormatter
virtual U_I18N_API ~RelativeDateTimeFormatter()
Destructor.
icu::RelativeDateTimeFormatter::format
U_I18N_API 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,...
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
U_I18N_API RelativeDateTimeFormatter(const Locale &locale, NumberFormat *nfToAdopt, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale and NumberFormat.
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
U_I18N_API RelativeDateTimeFormatter(const Locale &locale, NumberFormat *nfToAdopt, UDateRelativeDateTimeFormatterStyle style, UDisplayContext capitalizationContext, UErrorCode &status)
Create RelativeDateTimeFormatter with given locale, NumberFormat, and capitalization context.
icu::RelativeDateTimeFormatter::getNumberFormat
U_I18N_API const NumberFormat & getNumberFormat() const
Returns the NumberFormat this object is using.
icu::RelativeDateTimeFormatter::operator=
U_I18N_API RelativeDateTimeFormatter & operator=(const RelativeDateTimeFormatter &other)
Assignment operator.
icu::RelativeDateTimeFormatter::combineDateAndTime
U_I18N_API 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.
icu::RelativeDateTimeFormatter::formatNumeric
U_I18N_API UnicodeString & formatNumeric(double offset, URelativeDateTimeUnit unit, UnicodeString &appendTo, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a numeric style,...
icu::RelativeDateTimeFormatter::getCapitalizationContext
U_I18N_API UDisplayContext getCapitalizationContext() const
Returns the capitalization context.
icu::RelativeDateTimeFormatter::getFormatStyle
U_I18N_API UDateRelativeDateTimeFormatterStyle getFormatStyle() const
Returns the format style.
icu::RelativeDateTimeFormatter::formatNumericToValue
U_I18N_API FormattedRelativeDateTime formatNumericToValue(double offset, URelativeDateTimeUnit unit, UErrorCode &status) const
Format a combination of URelativeDateTimeUnit and numeric offset using a numeric style,...
icu::RelativeDateTimeFormatter::RelativeDateTimeFormatter
U_I18N_API RelativeDateTimeFormatter(const RelativeDateTimeFormatter &other)
Copy constructor.
icu::UMemory
UMemory is the common ICU base class.
Definition uobject.h:115
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition uobject.h:222
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
formattedvalue.h
C++ API: Abstract operations for localized strings.
locid.h
C++ API: Locale ID object.
UDateAbsoluteUnit
UDateAbsoluteUnit
Represents an absolute unit.
Definition reldatefmt.h:96
UDAT_ABSOLUTE_DAY
@ UDAT_ABSOLUTE_DAY
Day.
Definition reldatefmt.h:146
UDAT_ABSOLUTE_FRIDAY
@ UDAT_ABSOLUTE_FRIDAY
Friday.
Definition reldatefmt.h:134
UDAT_ABSOLUTE_YEAR
@ UDAT_ABSOLUTE_YEAR
Year.
Definition reldatefmt.h:164
UDAT_ABSOLUTE_WEDNESDAY
@ UDAT_ABSOLUTE_WEDNESDAY
Wednesday.
Definition reldatefmt.h:122
UDAT_ABSOLUTE_HOUR
@ UDAT_ABSOLUTE_HOUR
Hour.
Definition reldatefmt.h:182
UDAT_ABSOLUTE_WEEK
@ UDAT_ABSOLUTE_WEEK
Week.
Definition reldatefmt.h:152
UDAT_ABSOLUTE_NOW
@ UDAT_ABSOLUTE_NOW
Now.
Definition reldatefmt.h:170
UDAT_ABSOLUTE_MINUTE
@ UDAT_ABSOLUTE_MINUTE
Minute.
Definition reldatefmt.h:188
UDAT_ABSOLUTE_THURSDAY
@ UDAT_ABSOLUTE_THURSDAY
Thursday.
Definition reldatefmt.h:128
UDAT_ABSOLUTE_TUESDAY
@ UDAT_ABSOLUTE_TUESDAY
Tuesday.
Definition reldatefmt.h:116
UDAT_ABSOLUTE_MONTH
@ UDAT_ABSOLUTE_MONTH
Month.
Definition reldatefmt.h:158
UDAT_ABSOLUTE_SUNDAY
@ UDAT_ABSOLUTE_SUNDAY
Sunday.
Definition reldatefmt.h:104
UDAT_ABSOLUTE_UNIT_COUNT
@ UDAT_ABSOLUTE_UNIT_COUNT
One more than the highest normal UDateAbsoluteUnit value.
Definition reldatefmt.h:195
UDAT_ABSOLUTE_MONDAY
@ UDAT_ABSOLUTE_MONDAY
Monday.
Definition reldatefmt.h:110
UDAT_ABSOLUTE_SATURDAY
@ UDAT_ABSOLUTE_SATURDAY
Saturday.
Definition reldatefmt.h:140
UDAT_ABSOLUTE_QUARTER
@ UDAT_ABSOLUTE_QUARTER
Quarter.
Definition reldatefmt.h:176
UDateRelativeUnit
UDateRelativeUnit
Represents the unit for formatting a relative date.
Definition reldatefmt.h:39
UDAT_RELATIVE_DAYS
@ UDAT_RELATIVE_DAYS
Days.
Definition reldatefmt.h:63
UDAT_RELATIVE_UNIT_COUNT
@ UDAT_RELATIVE_UNIT_COUNT
One more than the highest normal UDateRelativeUnit value.
Definition reldatefmt.h:88
UDAT_RELATIVE_HOURS
@ UDAT_RELATIVE_HOURS
Hours.
Definition reldatefmt.h:57
UDAT_RELATIVE_MONTHS
@ UDAT_RELATIVE_MONTHS
Months.
Definition reldatefmt.h:75
UDAT_RELATIVE_WEEKS
@ UDAT_RELATIVE_WEEKS
Weeks.
Definition reldatefmt.h:69
UDAT_RELATIVE_YEARS
@ UDAT_RELATIVE_YEARS
Years.
Definition reldatefmt.h:81
UDAT_RELATIVE_MINUTES
@ UDAT_RELATIVE_MINUTES
Minutes.
Definition reldatefmt.h:51
UDAT_RELATIVE_SECONDS
@ UDAT_RELATIVE_SECONDS
Seconds.
Definition reldatefmt.h:45
UDateDirection
UDateDirection
Represents a direction for an absolute unit e.g "Next Tuesday" or "Last Tuesday".
Definition reldatefmt.h:204
UDAT_DIRECTION_PLAIN
@ UDAT_DIRECTION_PLAIN
Plain, which means the absence of a qualifier.
Definition reldatefmt.h:240
UDAT_DIRECTION_THIS
@ UDAT_DIRECTION_THIS
This.
Definition reldatefmt.h:222
UDAT_DIRECTION_LAST_2
@ UDAT_DIRECTION_LAST_2
Two before.
Definition reldatefmt.h:210
UDAT_DIRECTION_LAST
@ UDAT_DIRECTION_LAST
Last.
Definition reldatefmt.h:216
UDAT_DIRECTION_NEXT
@ UDAT_DIRECTION_NEXT
Next.
Definition reldatefmt.h:228
UDAT_DIRECTION_NEXT_2
@ UDAT_DIRECTION_NEXT_2
Two after.
Definition reldatefmt.h:234
UDAT_DIRECTION_COUNT
@ UDAT_DIRECTION_COUNT
One more than the highest normal UDateDirection value.
Definition reldatefmt.h:247
UCONFIG_NO_BREAK_ITERATION
#define UCONFIG_NO_BREAK_ITERATION
This switch turns off break iteration.
Definition uconfig.h:358
udisplaycontext.h
C API: Display context types (enum values).
UDisplayContext
UDisplayContext
Display context settings.
Definition udisplaycontext.h:64
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
uobject.h
C++ API: Common ICU base class UObject.
ureldatefmt.h
C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
UDateRelativeDateTimeFormatterStyle
UDateRelativeDateTimeFormatterStyle
The formatting style.
Definition ureldatefmt.h:48
URelativeDateTimeUnit
URelativeDateTimeUnit
Represents the unit for formatting a relative date.
Definition ureldatefmt.h:81
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:509
U_INVALID_STATE_ERROR
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition utypes.h:573
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition utypes.h:544
U_I18N_API_CLASS
#define U_I18N_API_CLASS
Set to export library symbols from inside the i18n library, and to import them from outside,...
Definition utypes.h:457
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:316