ICU 74.1 74.1

dtitvfmt.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 * Copyright (C) 2008-2016, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 *******************************************************************************
7 *
8 * File DTITVFMT.H
9 *
10 *******************************************************************************
11 */
12
13 #ifndef __DTITVFMT_H__
14 #define __DTITVFMT_H__
15
16
17 #include "unicode/utypes.h"
18
19 #if U_SHOW_CPLUSPLUS_API
20
26 #if !UCONFIG_NO_FORMATTING
27
28 #include "unicode/ucal.h"
29 #include "unicode/smpdtfmt.h"
30 #include "unicode/dtintrv.h"
31 #include "unicode/dtitvinf.h"
32 #include "unicode/dtptngen.h"
33 #include "unicode/formattedvalue.h"
34 #include "unicode/udisplaycontext.h"
35
36 U_NAMESPACE_BEGIN
37
38
39 class FormattedDateIntervalData;
40 class DateIntervalFormat;
41
59 class U_I18N_API FormattedDateInterval : public UMemory, public FormattedValue {
60 public:
65 FormattedDateInterval() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
66
71 FormattedDateInterval(FormattedDateInterval&& src) noexcept;
72
77 virtual ~FormattedDateInterval() override;
78
80 FormattedDateInterval(const FormattedDateInterval&) = delete;
81
83 FormattedDateInterval& operator=(const FormattedDateInterval&) = delete;
84
89 FormattedDateInterval& operator=(FormattedDateInterval&& src) noexcept;
90
92 UnicodeString toString(UErrorCode& status) const override;
93
95 UnicodeString toTempString(UErrorCode& status) const override;
96
98 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const override;
99
101 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override;
102
103 private:
104 FormattedDateIntervalData *fData;
105 UErrorCode fErrorCode;
106 explicit FormattedDateInterval(FormattedDateIntervalData *results)
107 : fData(results), fErrorCode(U_ZERO_ERROR) {}
108 explicit FormattedDateInterval(UErrorCode errorCode)
109 : fData(nullptr), fErrorCode(errorCode) {}
110 friend class DateIntervalFormat;
111 };
112
113
300 class U_I18N_API DateIntervalFormat : public Format {
301 public:
302
316 static DateIntervalFormat* U_EXPORT2 createInstance(
317 const UnicodeString& skeleton,
318 UErrorCode& status);
319
354 static DateIntervalFormat* U_EXPORT2 createInstance(
355 const UnicodeString& skeleton,
356 const Locale& locale,
357 UErrorCode& status);
358
374 static DateIntervalFormat* U_EXPORT2 createInstance(
375 const UnicodeString& skeleton,
376 const DateIntervalInfo& dtitvinf,
377 UErrorCode& status);
378
419 static DateIntervalFormat* U_EXPORT2 createInstance(
420 const UnicodeString& skeleton,
421 const Locale& locale,
422 const DateIntervalInfo& dtitvinf,
423 UErrorCode& status);
424
429 virtual ~DateIntervalFormat();
430
437 virtual DateIntervalFormat* clone() const override;
438
446 virtual bool operator==(const Format& other) const override;
447
455 bool operator!=(const Format& other) const;
456
457
458 using Format::format;
459
479 virtual UnicodeString& format(const Formattable& obj,
480 UnicodeString& appendTo,
481 FieldPosition& fieldPosition,
482 UErrorCode& status) const override;
483
484
485
501 UnicodeString& format(const DateInterval* dtInterval,
502 UnicodeString& appendTo,
503 FieldPosition& fieldPosition,
504 UErrorCode& status) const ;
505
516 FormattedDateInterval formatToValue(
517 const DateInterval& dtInterval,
518 UErrorCode& status) const;
519
543 UnicodeString& format(Calendar& fromCalendar,
544 Calendar& toCalendar,
545 UnicodeString& appendTo,
546 FieldPosition& fieldPosition,
547 UErrorCode& status) const ;
548
565 FormattedDateInterval formatToValue(
566 Calendar& fromCalendar,
567 Calendar& toCalendar,
568 UErrorCode& status) const;
569
596 virtual void parseObject(const UnicodeString& source,
597 Formattable& result,
598 ParsePosition& parse_pos) const override;
599
600
607 const DateIntervalInfo* getDateIntervalInfo(void) const;
608
609
616 void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns,
617 UErrorCode& status);
618
619
631 const DateFormat* getDateFormat(void) const;
632
638 virtual const TimeZone& getTimeZone(void) const;
639
646 virtual void adoptTimeZone(TimeZone* zoneToAdopt);
647
653 virtual void setTimeZone(const TimeZone& zone);
654
667 virtual void setContext(UDisplayContext value, UErrorCode& status);
668
679 virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
680
692 static UClassID U_EXPORT2 getStaticClassID(void);
693
705 virtual UClassID getDynamicClassID(void) const override;
706
707 protected:
708
713 DateIntervalFormat(const DateIntervalFormat&);
714
719 DateIntervalFormat& operator=(const DateIntervalFormat&);
720
721 private:
722
723 /*
724 * This is for ICU internal use only. Please do not use.
725 * Save the interval pattern information.
726 * Interval pattern consists of 2 single date patterns and the separator.
727 * For example, interval pattern "MMM d - MMM d, yyyy" consists
728 * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
729 * and a separator "-".
730 * The pattern is divided into 2 parts. For above example,
731 * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
732 * Also, the first date appears in an interval pattern could be
733 * the earlier date or the later date.
734 * And such information is saved in the interval pattern as well.
735 */
736 struct PatternInfo {
737 UnicodeString firstPart;
738 UnicodeString secondPart;
752 UBool laterDateFirst;
753 };
754
755
760 DateIntervalFormat();
761
780 DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo,
781 const UnicodeString* skeleton, UErrorCode& status);
782
783
796 static DateIntervalFormat* U_EXPORT2 create(const Locale& locale,
797 DateIntervalInfo* dtitvinf,
798 const UnicodeString* skeleton,
799 UErrorCode& status);
800
806 void fallbackFormatRange(
807 Calendar& fromCalendar,
808 Calendar& toCalendar,
809 UnicodeString& appendTo,
810 int8_t& firstIndex,
811 FieldPositionHandler& fphandler,
812 UErrorCode& status) const;
813
836 UnicodeString& fallbackFormat(Calendar& fromCalendar,
837 Calendar& toCalendar,
838 UBool fromToOnSameDay,
839 UnicodeString& appendTo,
840 int8_t& firstIndex,
841 FieldPositionHandler& fphandler,
842 UErrorCode& status) const;
843
844
845
880 void initializePattern(UErrorCode& status);
881
882
883
891 void setFallbackPattern(UCalendarDateFields field,
892 const UnicodeString& skeleton,
893 UErrorCode& status);
894
895
896
906 UnicodeString normalizeHourMetacharacters(const UnicodeString& skeleton) const;
907
908
909
934 static void U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton,
935 UnicodeString& date,
936 UnicodeString& normalizedDate,
937 UnicodeString& time,
938 UnicodeString& normalizedTime);
939
940
941
965 UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton,
966 const UnicodeString& timeSkeleton);
967
968
969
970
994 UBool setIntervalPattern(UCalendarDateFields field,
995 const UnicodeString* skeleton,
996 const UnicodeString* bestSkeleton,
997 int8_t differenceInfo,
998 UnicodeString* extendedSkeleton = nullptr,
999 UnicodeString* extendedBestSkeleton = nullptr);
1000
1030 static void U_EXPORT2 adjustFieldWidth(
1031 const UnicodeString& inputSkeleton,
1032 const UnicodeString& bestMatchSkeleton,
1033 const UnicodeString& bestMatchIntervalPattern,
1034 int8_t differenceInfo,
1035 UBool suppressDayPeriodField,
1036 UnicodeString& adjustedIntervalPattern);
1037
1045 static void U_EXPORT2 findReplaceInPattern(UnicodeString& targetString,
1046 const UnicodeString& strToReplace,
1047 const UnicodeString& strToReplaceWith);
1048
1060 void concatSingleDate2TimeInterval(UnicodeString& format,
1061 const UnicodeString& datePattern,
1062 UCalendarDateFields field,
1063 UErrorCode& status);
1064
1071 static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field,
1072 const UnicodeString& skeleton);
1073
1074
1080 static int32_t U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern);
1081
1082
1088 void setIntervalPattern(UCalendarDateFields field,
1089 const UnicodeString& intervalPattern);
1090
1091
1098 void setIntervalPattern(UCalendarDateFields field,
1099 const UnicodeString& intervalPattern,
1100 UBool laterDateFirst);
1101
1102
1112 void setPatternInfo(UCalendarDateFields field,
1113 const UnicodeString* firstPart,
1114 const UnicodeString* secondPart,
1115 UBool laterDateFirst);
1116
1142 UnicodeString& formatImpl(Calendar& fromCalendar,
1143 Calendar& toCalendar,
1144 UnicodeString& appendTo,
1145 int8_t& firstIndex,
1146 FieldPositionHandler& fphandler,
1147 UErrorCode& status) const ;
1148
1150 UnicodeString& formatIntervalImpl(const DateInterval& dtInterval,
1151 UnicodeString& appendTo,
1152 int8_t& firstIndex,
1153 FieldPositionHandler& fphandler,
1154 UErrorCode& status) const;
1155
1156
1157 // from calendar field to pattern letter
1158 static const char16_t fgCalendarFieldToPatternLetter[];
1159
1160
1164 DateIntervalInfo* fInfo;
1165
1169 SimpleDateFormat* fDateFormat;
1170
1176 Calendar* fFromCalendar;
1177 Calendar* fToCalendar;
1178
1179 Locale fLocale;
1180
1184 UnicodeString fSkeleton;
1185 PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX];
1186
1190 UnicodeString* fDatePattern;
1191 UnicodeString* fTimePattern;
1192 UnicodeString* fDateTimeFormat;
1193
1197 UDisplayContext fCapitalizationContext;
1198 };
1199
1200 inline bool
1201 DateIntervalFormat::operator!=(const Format& other) const {
1202 return !operator==(other);
1203 }
1204
1205 U_NAMESPACE_END
1206
1207 #endif /* #if !UCONFIG_NO_FORMATTING */
1208
1209 #endif /* U_SHOW_CPLUSPLUS_API */
1210
1211 #endif // _DTITVFMT_H__
1212 //eof
icu::Appendable
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
icu::Calendar
Calendar is an abstract base class for converting between a UDate object and a set of integer fields ...
Definition: calendar.h:189
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:41
icu::DateFormat
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:156
icu::DateIntervalFormat
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent man...
Definition: dtitvfmt.h:300
icu::DateIntervalFormat::getDateIntervalInfo
const DateIntervalInfo * getDateIntervalInfo(void) const
Gets the date time interval patterns.
icu::DateIntervalFormat::setTimeZone
virtual void setTimeZone(const TimeZone &zone)
Sets the time zone for the calendar used by this DateIntervalFormat object.
icu::DateIntervalFormat::format
UnicodeString & format(Calendar &fromCalendar, Calendar &toCalendar, UnicodeString &appendTo, FieldPosition &fieldPosition, UErrorCode &status) const
Format 2 Calendars to produce a string.
icu::DateIntervalFormat::getTimeZone
virtual const TimeZone & getTimeZone(void) const
Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
icu::DateIntervalFormat::createInstance
static DateIntervalFormat * createInstance(const UnicodeString &skeleton, const Locale &locale, UErrorCode &status)
Construct a DateIntervalFormat from skeleton and a given locale.
icu::DateIntervalFormat::getDateFormat
const DateFormat * getDateFormat(void) const
Gets the date formatter.
icu::DateIntervalFormat::format
UnicodeString & format(const DateInterval *dtInterval, UnicodeString &appendTo, FieldPosition &fieldPosition, UErrorCode &status) const
Format a DateInterval to produce a string.
icu::DateIntervalFormat::clone
virtual DateIntervalFormat * clone() const override
Clone this Format object polymorphically.
icu::DateIntervalFormat::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const override
Date interval parsing is not supported.
icu::DateIntervalFormat::~DateIntervalFormat
virtual ~DateIntervalFormat()
Destructor.
icu::DateIntervalFormat::setContext
virtual void setContext(UDisplayContext value, UErrorCode &status)
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
icu::DateIntervalFormat::createInstance
static DateIntervalFormat * createInstance(const UnicodeString &skeleton, const Locale &locale, const DateIntervalInfo &dtitvinf, UErrorCode &status)
Construct a DateIntervalFormat from skeleton a DateIntervalInfo, and the given locale.
icu::DateIntervalFormat::operator=
DateIntervalFormat & operator=(const DateIntervalFormat &)
Assignment operator.
icu::DateIntervalFormat::format
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &fieldPosition, UErrorCode &status) const override
Format an object to produce a string.
icu::DateIntervalFormat::createInstance
static DateIntervalFormat * createInstance(const UnicodeString &skeleton, UErrorCode &status)
Construct a DateIntervalFormat from skeleton and the default locale.
icu::DateIntervalFormat::getDynamicClassID
virtual UClassID getDynamicClassID(void) const override
Returns a unique class ID POLYMORPHICALLY.
icu::DateIntervalFormat::adoptTimeZone
virtual void adoptTimeZone(TimeZone *zoneToAdopt)
Sets the time zone for the calendar used by this DateIntervalFormat object.
icu::DateIntervalFormat::getStaticClassID
static UClassID getStaticClassID(void)
Return the class ID for this class.
icu::DateIntervalFormat::getContext
virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode &status) const
Get the formatter's UDisplayContext value for the specified UDisplayContextType, such as UDISPCTX_TYP...
icu::DateIntervalFormat::operator==
virtual bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
icu::DateIntervalFormat::formatToValue
FormattedDateInterval formatToValue(Calendar &fromCalendar, Calendar &toCalendar, UErrorCode &status) const
Format 2 Calendars to produce a FormattedDateInterval.
icu::DateIntervalFormat::DateIntervalFormat
DateIntervalFormat(const DateIntervalFormat &)
Copy constructor.
icu::DateIntervalFormat::createInstance
static DateIntervalFormat * createInstance(const UnicodeString &skeleton, const DateIntervalInfo &dtitvinf, UErrorCode &status)
Construct a DateIntervalFormat from skeleton DateIntervalInfo, and default locale.
icu::DateIntervalFormat::formatToValue
FormattedDateInterval formatToValue(const DateInterval &dtInterval, UErrorCode &status) const
Format a DateInterval to produce a FormattedDateInterval.
icu::DateIntervalFormat::setDateIntervalInfo
void setDateIntervalInfo(const DateIntervalInfo &newIntervalPatterns, UErrorCode &status)
Set the date time interval patterns.
icu::DateIntervalInfo
DateIntervalInfo is a public class for encapsulating localizable date time interval patterns.
Definition: dtitvinf.h:154
icu::DateInterval
This class represents a date interval.
Definition: dtintrv.h:36
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
icu::Format
Base class for all formats.
Definition: format.h:98
icu::Format::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
icu::FormattedDateInterval
An immutable class containing the result of a date interval formatting operation.
Definition: dtitvfmt.h:59
icu::FormattedDateInterval::operator=
FormattedDateInterval & operator=(FormattedDateInterval &&src) noexcept
Move assignment: Leaves the source FormattedDateInterval in an undefined state.
icu::FormattedDateInterval::FormattedDateInterval
FormattedDateInterval(const FormattedDateInterval &)=delete
Copying not supported; use move constructor instead.
icu::FormattedDateInterval::appendTo
Appendable & appendTo(Appendable &appendable, UErrorCode &status) const override
Appends the formatted string to an Appendable.
icu::FormattedDateInterval::nextPosition
UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const override
Iterates over field positions in the FormattedValue.
icu::FormattedDateInterval::~FormattedDateInterval
virtual ~FormattedDateInterval() override
Destruct an instance of FormattedDateInterval.
icu::FormattedDateInterval::FormattedDateInterval
FormattedDateInterval()
Default constructor; makes an empty FormattedDateInterval.
Definition: dtitvfmt.h:65
icu::FormattedDateInterval::toString
UnicodeString toString(UErrorCode &status) const override
Returns the formatted string as a self-contained UnicodeString.
icu::FormattedDateInterval::toTempString
UnicodeString toTempString(UErrorCode &status) const override
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
icu::FormattedDateInterval::operator=
FormattedDateInterval & operator=(const FormattedDateInterval &)=delete
Copying not supported; use move assignment instead.
icu::FormattedDateInterval::FormattedDateInterval
FormattedDateInterval(FormattedDateInterval &&src) noexcept
Move constructor: Leaves the source FormattedDateInterval in an undefined state.
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:195
icu::ParsePosition
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
icu::SimpleDateFormat
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:727
icu::TimeZone
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:133
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
dtintrv.h
C++ API: Date Interval data type.
dtitvinf.h
C++ API: Date/Time interval patterns for formatting date/time interval.
dtptngen.h
C++ API: Date/Time Pattern Generator.
formattedvalue.h
C++ API: Abstract operations for localized strings.
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::operator!=
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:335
smpdtfmt.h
C++ API: Format and parse dates in a language-independent manner.
ucal.h
C API: Calendar.
UCalendarDateFields
UCalendarDateFields
Possible fields in a UCalendar.
Definition: ucal.h:202
udisplaycontext.h
C API: Display context types (enum values)
UDisplayContextType
UDisplayContextType
Display context types, for getting values of a particular setting.
Definition: udisplaycontext.h:27
UDisplayContext
UDisplayContext
Display context settings.
Definition: udisplaycontext.h:64
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
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:415
U_INVALID_STATE_ERROR
@ U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
U_ZERO_ERROR
@ U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
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