ICU 78.3  78.3
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"
35 
36 U_NAMESPACE_BEGIN
37 
38 
39 class FormattedDateIntervalData;
40 class DateIntervalFormat;
41 
60  public:
65  FormattedDateInterval() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
66 
72 
77  virtual ~FormattedDateInterval() override;
78 
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 
301 public:
302 
316  U_I18N_API static DateIntervalFormat* createInstance(const UnicodeString& skeleton,
317  UErrorCode& status);
318 
348  U_I18N_API static DateIntervalFormat* createInstance(const UnicodeString& skeleton,
349  const Locale& locale,
350  UErrorCode& status);
351 
367  U_I18N_API static DateIntervalFormat* createInstance(const UnicodeString& skeleton,
368  const DateIntervalInfo& dtitvinf,
369  UErrorCode& status);
370 
406  U_I18N_API static DateIntervalFormat* createInstance(const UnicodeString& skeleton,
407  const Locale& locale,
408  const DateIntervalInfo& dtitvinf,
409  UErrorCode& status);
410 
415  U_I18N_API virtual ~DateIntervalFormat();
416 
423  U_I18N_API virtual DateIntervalFormat* clone() const override;
424 
432  U_I18N_API virtual bool operator==(const Format& other) const override;
433 
441  U_I18N_API bool operator!=(const Format& other) const;
442 
443  using Format::format;
444 
464  U_I18N_API virtual UnicodeString& format(const Formattable& obj,
465  UnicodeString& appendTo,
466  FieldPosition& fieldPosition,
467  UErrorCode& status) const override;
468 
484  U_I18N_API UnicodeString& format(const DateInterval* dtInterval,
485  UnicodeString& appendTo,
486  FieldPosition& fieldPosition,
487  UErrorCode& status) const;
488 
499  U_I18N_API FormattedDateInterval formatToValue(const DateInterval& dtInterval,
500  UErrorCode& status) const;
501 
525  U_I18N_API UnicodeString& format(Calendar& fromCalendar,
526  Calendar& toCalendar,
527  UnicodeString& appendTo,
528  FieldPosition& fieldPosition,
529  UErrorCode& status) const;
530 
547  U_I18N_API FormattedDateInterval formatToValue(Calendar& fromCalendar,
548  Calendar& toCalendar,
549  UErrorCode& status) const;
550 
577  U_I18N_API virtual void parseObject(const UnicodeString& source,
578  Formattable& result,
579  ParsePosition& parse_pos) const override;
580 
587  U_I18N_API const DateIntervalInfo* getDateIntervalInfo() const;
588 
595  U_I18N_API void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns, UErrorCode& status);
596 
608  U_I18N_API const DateFormat* getDateFormat() const;
609 
615  U_I18N_API virtual const TimeZone& getTimeZone() const;
616 
623  U_I18N_API virtual void adoptTimeZone(TimeZone* zoneToAdopt);
624 
630  U_I18N_API virtual void setTimeZone(const TimeZone& zone);
631 
644  U_I18N_API virtual void setContext(UDisplayContext value, UErrorCode& status);
645 
656  U_I18N_API virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
657 
669  U_I18N_API static UClassID getStaticClassID();
670 
682  U_I18N_API virtual UClassID getDynamicClassID() const override;
683 
684 protected:
685 
691 
697 
698 private:
699 
700  /*
701  * This is for ICU internal use only. Please do not use.
702  * Save the interval pattern information.
703  * Interval pattern consists of 2 single date patterns and the separator.
704  * For example, interval pattern "MMM d - MMM d, yyyy" consists
705  * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
706  * and a separator "-".
707  * The pattern is divided into 2 parts. For above example,
708  * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
709  * Also, the first date appears in an interval pattern could be
710  * the earlier date or the later date.
711  * And such information is saved in the interval pattern as well.
712  */
713  struct PatternInfo {
714  UnicodeString firstPart;
715  UnicodeString secondPart;
729  UBool laterDateFirst;
730  };
731 
732 
737 
756  DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo,
757  const UnicodeString* skeleton, UErrorCode& status);
758 
759 
772  U_I18N_API static DateIntervalFormat* create(const Locale& locale,
773  DateIntervalInfo* dtitvinf,
774  const UnicodeString* skeleton,
775  UErrorCode& status);
776 
782  void fallbackFormatRange(
783  Calendar& fromCalendar,
784  Calendar& toCalendar,
785  UnicodeString& appendTo,
786  int8_t& firstIndex,
787  FieldPositionHandler& fphandler,
788  UErrorCode& status) const;
789 
811  UnicodeString& fallbackFormat(Calendar& fromCalendar,
812  Calendar& toCalendar,
813  UBool fromToOnSameDay,
814  UnicodeString& appendTo,
815  int8_t& firstIndex,
816  FieldPositionHandler& fphandler,
817  UErrorCode& status) const;
818 
819 
820 
855  void initializePattern(UErrorCode& status);
856 
857 
858 
866  void setFallbackPattern(UCalendarDateFields field,
867  const UnicodeString& skeleton,
868  UErrorCode& status);
869 
870 
871 
881  UnicodeString normalizeHourMetacharacters(const UnicodeString& skeleton) const;
882 
883 
884 
909  U_I18N_API static void getDateTimeSkeleton(const UnicodeString& skeleton,
910  UnicodeString& date,
911  UnicodeString& normalizedDate,
912  UnicodeString& time,
913  UnicodeString& normalizedTime);
914 
938  UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton,
939  const UnicodeString& timeSkeleton);
940 
941 
942 
943 
967  UBool setIntervalPattern(UCalendarDateFields field,
968  const UnicodeString* skeleton,
969  const UnicodeString* bestSkeleton,
970  int8_t differenceInfo,
971  UnicodeString* extendedSkeleton = nullptr,
972  UnicodeString* extendedBestSkeleton = nullptr);
973 
1003  U_I18N_API static void adjustFieldWidth(const UnicodeString& inputSkeleton,
1004  const UnicodeString& bestMatchSkeleton,
1005  const UnicodeString& bestMatchIntervalPattern,
1006  int8_t differenceInfo,
1007  UBool suppressDayPeriodField,
1008  UnicodeString& adjustedIntervalPattern);
1009 
1017  U_I18N_API static void findReplaceInPattern(UnicodeString& targetString,
1018  const UnicodeString& strToReplace,
1019  const UnicodeString& strToReplaceWith);
1020 
1032  void concatSingleDate2TimeInterval(UnicodeString& format,
1033  const UnicodeString& datePattern,
1034  UCalendarDateFields field,
1035  UErrorCode& status);
1036 
1043  U_I18N_API static UBool fieldExistsInSkeleton(UCalendarDateFields field,
1044  const UnicodeString& skeleton);
1045 
1051  U_I18N_API static int32_t splitPatternInto2Part(const UnicodeString& intervalPattern);
1052 
1058  void setIntervalPattern(UCalendarDateFields field,
1059  const UnicodeString& intervalPattern);
1060 
1061 
1068  void setIntervalPattern(UCalendarDateFields field,
1069  const UnicodeString& intervalPattern,
1070  UBool laterDateFirst);
1071 
1072 
1082  void setPatternInfo(UCalendarDateFields field,
1083  const UnicodeString* firstPart,
1084  const UnicodeString* secondPart,
1085  UBool laterDateFirst);
1086 
1111  UnicodeString& formatImpl(Calendar& fromCalendar,
1112  Calendar& toCalendar,
1113  UnicodeString& appendTo,
1114  int8_t& firstIndex,
1115  FieldPositionHandler& fphandler,
1116  UErrorCode& status) const ;
1117 
1119  UnicodeString& formatIntervalImpl(const DateInterval& dtInterval,
1120  UnicodeString& appendTo,
1121  int8_t& firstIndex,
1122  FieldPositionHandler& fphandler,
1123  UErrorCode& status) const;
1124 
1125 
1126  // from calendar field to pattern letter
1127  static const char16_t fgCalendarFieldToPatternLetter[];
1128 
1129 
1133  DateIntervalInfo* fInfo;
1134 
1138  SimpleDateFormat* fDateFormat;
1139 
1145  Calendar* fFromCalendar;
1146  Calendar* fToCalendar;
1147 
1148  Locale fLocale;
1149 
1153  UnicodeString fSkeleton;
1154  PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX];
1155 
1159  UnicodeString* fDatePattern;
1160  UnicodeString* fTimePattern;
1161  UnicodeString* fDateTimeFormat;
1162 
1166  UDisplayContext fCapitalizationContext;
1167 };
1168 
1169 inline bool
1171  return !operator==(other);
1172 }
1173 
1174 U_NAMESPACE_END
1175 
1176 #endif /* #if !UCONFIG_NO_FORMATTING */
1177 
1178 #endif /* U_SHOW_CPLUSPLUS_API */
1179 
1180 #endif // _DTITVFMT_H__
1181 //eof
C++ API: Abstract operations for localized strings.
Base class for all formats.
Definition: format.h:98
virtual UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const =0
Iterates over field positions in the FormattedValue.
bool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:346
UCalendarDateFields
Possible fields in a UCalendar.
Definition: ucal.h:202
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent man...
Definition: dtitvfmt.h:300
virtual UnicodeString toString(UErrorCode &status) const =0
Returns the formatted string as a self-contained UnicodeString.
U_COMMON_API UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
C API: Calendar.
C++ API: Date/Time Pattern Generator.
DateIntervalInfo is a public class for encapsulating localizable date time interval patterns...
Definition: dtitvinf.h:154
U_I18N_API bool operator!=(const Format &other) const
Return true if the given Format objects are not semantically equal.
Definition: format.h:123
Calendar is an abstract base class for converting between a UDate object and a set of integer fields ...
Definition: calendar.h:189
UDisplayContext
Display context settings.
U_I18N_API Format & operator=(const Format &)
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
No error, no warning.
Definition: utypes.h:544
virtual U_I18N_API Format * clone() const =0
Clone this object polymorphically.
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:133
C API: Display context types (enum values)
An abstract formatted value: a string with associated field attributes.
This class represents a date interval.
Definition: dtintrv.h:36
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:316
#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
C++ API: Format and parse dates in a language-independent manner.
U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
Represents a span of a string containing a given field.
UDisplayContextType
Display context types, for getting values of a particular setting.
C++ API: Date Interval data type.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:509
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:385
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
virtual U_I18N_API void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:144
An immutable class containing the result of a date interval formatting operation. ...
Definition: dtitvfmt.h:59
Basic definitions for ICU, for both C and C++ APIs.
C++ API: Date/Time interval patterns for formatting date/time interval.
virtual Appendable & appendTo(Appendable &appendable, UErrorCode &status) const =0
Appends the formatted string to an Appendable.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:302
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
FormattedDateInterval()
Default constructor; makes an empty FormattedDateInterval.
Definition: dtitvfmt.h:65
UMemory is the common ICU base class.
Definition: uobject.h:115
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:573
virtual U_I18N_API bool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
Base class for objects to which Unicode characters and strings can be appended.
Definition: appendable.h:54
virtual UnicodeString toTempString(UErrorCode &status) const =0
Returns the formatted string as a read-only alias to memory owned by the FormattedValue.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:198