ICU 67.1 67.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
35 U_NAMESPACE_BEGIN
36
37
38 class FormattedDateIntervalData;
39 class DateIntervalFormat;
40
58 class U_I18N_API FormattedDateInterval : public UMemory, public FormattedValue {
59 public:
64 FormattedDateInterval() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
65
70 FormattedDateInterval(FormattedDateInterval&& src) U_NOEXCEPT;
71
76 virtual ~FormattedDateInterval() U_OVERRIDE;
77
79 FormattedDateInterval(const FormattedDateInterval&) = delete;
80
82 FormattedDateInterval& operator=(const FormattedDateInterval&) = delete;
83
88 FormattedDateInterval& operator=(FormattedDateInterval&& src) U_NOEXCEPT;
89
91 UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
92
94 UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
95
97 Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
98
100 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
101
102 private:
103 FormattedDateIntervalData *fData;
104 UErrorCode fErrorCode;
105 explicit FormattedDateInterval(FormattedDateIntervalData *results)
106 : fData(results), fErrorCode(U_ZERO_ERROR) {}
107 explicit FormattedDateInterval(UErrorCode errorCode)
108 : fData(nullptr), fErrorCode(errorCode) {}
109 friend class DateIntervalFormat;
110 };
111
112
299 class U_I18N_API DateIntervalFormat : public Format {
300 public:
301
315 static DateIntervalFormat* U_EXPORT2 createInstance(
316 const UnicodeString& skeleton,
317 UErrorCode& status);
318
353 static DateIntervalFormat* U_EXPORT2 createInstance(
354 const UnicodeString& skeleton,
355 const Locale& locale,
356 UErrorCode& status);
357
373 static DateIntervalFormat* U_EXPORT2 createInstance(
374 const UnicodeString& skeleton,
375 const DateIntervalInfo& dtitvinf,
376 UErrorCode& status);
377
418 static DateIntervalFormat* U_EXPORT2 createInstance(
419 const UnicodeString& skeleton,
420 const Locale& locale,
421 const DateIntervalInfo& dtitvinf,
422 UErrorCode& status);
423
428 virtual ~DateIntervalFormat();
429
436 virtual DateIntervalFormat* clone() const;
437
445 virtual UBool operator==(const Format& other) const;
446
454 UBool operator!=(const Format& other) const;
455
456
457 using Format::format;
458
478 virtual UnicodeString& format(const Formattable& obj,
479 UnicodeString& appendTo,
480 FieldPosition& fieldPosition,
481 UErrorCode& status) const ;
482
483
484
500 UnicodeString& format(const DateInterval* dtInterval,
501 UnicodeString& appendTo,
502 FieldPosition& fieldPosition,
503 UErrorCode& status) const ;
504
515 FormattedDateInterval formatToValue(
516 const DateInterval& dtInterval,
517 UErrorCode& status) const;
518
542 UnicodeString& format(Calendar& fromCalendar,
543 Calendar& toCalendar,
544 UnicodeString& appendTo,
545 FieldPosition& fieldPosition,
546 UErrorCode& status) const ;
547
564 FormattedDateInterval formatToValue(
565 Calendar& fromCalendar,
566 Calendar& toCalendar,
567 UErrorCode& status) const;
568
595 virtual void parseObject(const UnicodeString& source,
596 Formattable& result,
597 ParsePosition& parse_pos) const;
598
599
606 const DateIntervalInfo* getDateIntervalInfo(void) const;
607
608
615 void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns,
616 UErrorCode& status);
617
618
630 const DateFormat* getDateFormat(void) const;
631
637 virtual const TimeZone& getTimeZone(void) const;
638
645 virtual void adoptTimeZone(TimeZone* zoneToAdopt);
646
652 virtual void setTimeZone(const TimeZone& zone);
653
665 static UClassID U_EXPORT2 getStaticClassID(void);
666
678 virtual UClassID getDynamicClassID(void) const;
679
680 protected:
681
686 DateIntervalFormat(const DateIntervalFormat&);
687
692 DateIntervalFormat& operator=(const DateIntervalFormat&);
693
694 private:
695
696 /*
697 * This is for ICU internal use only. Please do not use.
698 * Save the interval pattern information.
699 * Interval pattern consists of 2 single date patterns and the separator.
700 * For example, interval pattern "MMM d - MMM d, yyyy" consists
701 * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
702 * and a separator "-".
703 * The pattern is divided into 2 parts. For above example,
704 * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
705 * Also, the first date appears in an interval pattern could be
706 * the earlier date or the later date.
707 * And such information is saved in the interval pattern as well.
708 */
709 struct PatternInfo {
710 UnicodeString firstPart;
711 UnicodeString secondPart;
725 UBool laterDateFirst;
726 };
727
728
733 DateIntervalFormat();
734
753 DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo,
754 const UnicodeString* skeleton, UErrorCode& status);
755
756
769 static DateIntervalFormat* U_EXPORT2 create(const Locale& locale,
770 DateIntervalInfo* dtitvinf,
771 const UnicodeString* skeleton,
772 UErrorCode& status);
773
779 void fallbackFormatRange(
780 Calendar& fromCalendar,
781 Calendar& toCalendar,
782 UnicodeString& appendTo,
783 int8_t& firstIndex,
784 FieldPositionHandler& fphandler,
785 UErrorCode& status) const;
786
809 UnicodeString& fallbackFormat(Calendar& fromCalendar,
810 Calendar& toCalendar,
811 UBool fromToOnSameDay,
812 UnicodeString& appendTo,
813 int8_t& firstIndex,
814 FieldPositionHandler& fphandler,
815 UErrorCode& status) const;
816
817
818
853 void initializePattern(UErrorCode& status);
854
855
856
864 void setFallbackPattern(UCalendarDateFields field,
865 const UnicodeString& skeleton,
866 UErrorCode& status);
867
868
869
894 static void U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton,
895 UnicodeString& date,
896 UnicodeString& normalizedDate,
897 UnicodeString& time,
898 UnicodeString& normalizedTime);
899
900
901
925 UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton,
926 const UnicodeString& timeSkeleton);
927
928
929
930
954 UBool setIntervalPattern(UCalendarDateFields field,
955 const UnicodeString* skeleton,
956 const UnicodeString* bestSkeleton,
957 int8_t differenceInfo,
958 UnicodeString* extendedSkeleton = NULL,
959 UnicodeString* extendedBestSkeleton = NULL);
960
989 static void U_EXPORT2 adjustFieldWidth(
990 const UnicodeString& inputSkeleton,
991 const UnicodeString& bestMatchSkeleton,
992 const UnicodeString& bestMatchIntervalPattern,
993 int8_t differenceInfo,
994 UnicodeString& adjustedIntervalPattern);
995
1007 void concatSingleDate2TimeInterval(UnicodeString& format,
1008 const UnicodeString& datePattern,
1009 UCalendarDateFields field,
1010 UErrorCode& status);
1011
1018 static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field,
1019 const UnicodeString& skeleton);
1020
1021
1027 static int32_t U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern);
1028
1029
1035 void setIntervalPattern(UCalendarDateFields field,
1036 const UnicodeString& intervalPattern);
1037
1038
1045 void setIntervalPattern(UCalendarDateFields field,
1046 const UnicodeString& intervalPattern,
1047 UBool laterDateFirst);
1048
1049
1059 void setPatternInfo(UCalendarDateFields field,
1060 const UnicodeString* firstPart,
1061 const UnicodeString* secondPart,
1062 UBool laterDateFirst);
1063
1089 UnicodeString& formatImpl(Calendar& fromCalendar,
1090 Calendar& toCalendar,
1091 UnicodeString& appendTo,
1092 int8_t& firstIndex,
1093 FieldPositionHandler& fphandler,
1094 UErrorCode& status) const ;
1095
1097 UnicodeString& formatIntervalImpl(const DateInterval& dtInterval,
1098 UnicodeString& appendTo,
1099 int8_t& firstIndex,
1100 FieldPositionHandler& fphandler,
1101 UErrorCode& status) const;
1102
1103
1104 // from calendar field to pattern letter
1105 static const char16_t fgCalendarFieldToPatternLetter[];
1106
1107
1111 DateIntervalInfo* fInfo;
1112
1116 SimpleDateFormat* fDateFormat;
1117
1123 Calendar* fFromCalendar;
1124 Calendar* fToCalendar;
1125
1126 Locale fLocale;
1127
1131 UnicodeString fSkeleton;
1132 PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX];
1133
1137 UnicodeString* fDatePattern;
1138 UnicodeString* fTimePattern;
1139 UnicodeString* fDateTimeFormat;
1140 };
1141
1142 inline UBool
1143 DateIntervalFormat::operator!=(const Format& other) const {
1144 return !operator==(other);
1145 }
1146
1147 U_NAMESPACE_END
1148
1149 #endif /* #if !UCONFIG_NO_FORMATTING */
1150
1151 #endif /* U_SHOW_CPLUSPLUS_API */
1152
1153 #endif // _DTITVFMT_H__
1154 //eof
formattedvalue.h
C++ API: Abstract operations for localized strings.
icu::Format
Base class for all formats.
Definition: format.h:98
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
UCalendarDateFields
UCalendarDateFields
Possible fields in a UCalendar.
Definition: ucal.h:199
icu::DateIntervalFormat
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent man...
Definition: dtitvfmt.h:299
icu::FormattedValue::toString
virtual UnicodeString toString(UErrorCode &status) const =0
Returns the formatted string as a self-contained UnicodeString.
icu::Format::operator!=
UBool operator!=(const Format &other) const
Return true if the given Format objects are not semantically equal.
Definition: format.h:123
ucal.h
C API: Calendar.
icu::Format::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
dtptngen.h
C++ API: Date/Time Pattern Generator.
icu::DateIntervalInfo
DateIntervalInfo is a public class for encapsulating localizable date time interval patterns...
Definition: dtitvinf.h:154
icu::Calendar
Calendar is an abstract base class for converting between a UDate object and a set of integer fields ...
Definition: calendar.h:187
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
UClassID
void * UClassID
UClassID is used to identify classes without using the compiler&#39;s RTTI.
Definition: uobject.h:96
icu::Format::clone
virtual Format * clone() const =0
Clone this object polymorphically.
U_ZERO_ERROR
No error, no warning.
Definition: utypes.h:449
icu::TimeZone
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:133
icu::FormattedValue
An abstract formatted value: a string with associated field attributes.
Definition: formattedvalue.h:241
icu::DateInterval
This class represents a date interval.
Definition: dtintrv.h:36
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
smpdtfmt.h
C++ API: Format and parse dates in a language-independent manner.
icu::operator!=
UBool operator!=(const StringPiece &x, const StringPiece &y)
Global operator != for StringPiece.
Definition: stringpiece.h:347
NULL
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
Definition: utypes.h:188
icu::UObject::getDynamicClassID
virtual UClassID getDynamicClassID() const
ICU4C "poor man&#39;s RTTI", returns a UClassID for the actual ICU class.
icu::ConstrainedFieldPosition
Represents a span of a string containing a given field.
Definition: formattedvalue.h:41
U_NOEXCEPT
#define U_NOEXCEPT
"noexcept" if supported, otherwise empty.
Definition: platform.h:529
dtintrv.h
C++ API: Date Interval data type.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
icu::SimpleDateFormat
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:726
icu::FieldPosition
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
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::DateFormat
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:156
icu::Format::operator==
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
icu::FormattedDateInterval
An immutable class containing the result of a date interval formatting operation. ...
Definition: dtitvfmt.h:58
icu::Format::parseObject
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
dtitvinf.h
C++ API: Date/Time interval patterns for formatting date/time interval.
icu::Format::operator=
Format & operator=(const Format &)
icu::FormattedValue::appendTo
virtual Appendable & appendTo(Appendable &appendable, UErrorCode &status) const =0
Appends the formatted string to an Appendable.
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:294
icu::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
icu::FormattedDateInterval::FormattedDateInterval
FormattedDateInterval()
Default constructor; makes an empty FormattedDateInterval.
Definition: dtitvfmt.h:64
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:115
U_INVALID_STATE_ERROR
Requested operation can not be completed with ICU in its current state.
Definition: utypes.h:478
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.
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195