ICU 78.2 78.2

smpdtfmt.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) 1997-2016, International Business Machines Corporation and
5 * others. All Rights Reserved.
6 *******************************************************************************
7 *
8 * File SMPDTFMT.H
9 *
10 * Modification History:
11 *
12 * Date Name Description
13 * 02/19/97 aliu Converted from java.
14 * 07/09/97 helena Make ParsePosition into a class.
15 * 07/21/98 stephen Added GMT_PLUS, GMT_MINUS
16 * Changed setTwoDigitStartDate to set2DigitYearStart
17 * Changed getTwoDigitStartDate to get2DigitYearStart
18 * Removed subParseLong
19 * Removed getZoneIndex (added in DateFormatSymbols)
20 * 06/14/99 stephen Removed fgTimeZoneDataSuffix
21 * 10/14/99 aliu Updated class doc to describe 2-digit year parsing
22 * {j28 4182066}.
23 *******************************************************************************
24 */
25
26 #ifndef SMPDTFMT_H
27 #define SMPDTFMT_H
28
29 #include "unicode/utypes.h"
30
31 #if U_SHOW_CPLUSPLUS_API
32
38 #if !UCONFIG_NO_FORMATTING
39
40 #include "unicode/datefmt.h"
41 #include "unicode/udisplaycontext.h"
42 #include "unicode/tzfmt.h" /* for UTimeZoneFormatTimeType */
43 #include "unicode/brkiter.h"
44
45 U_NAMESPACE_BEGIN
46
47 class DateFormatSymbols;
48 class DateFormat;
49 class MessageFormat;
50 class FieldPositionHandler;
51 class TimeZoneFormat;
52 class SharedNumberFormat;
53 class SimpleDateFormatMutableNFs;
54 class DateIntervalFormat;
55
56 namespace number {
57 class LocalizedNumberFormatter;
58 class SimpleNumberFormatter;
59 }
60
385 class U_I18N_API_CLASS SimpleDateFormat: public DateFormat {
386 public:
396 U_I18N_API SimpleDateFormat(UErrorCode& status);
397
409 U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
410 UErrorCode& status);
411
433 U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
434 const UnicodeString& override,
435 UErrorCode& status);
436
449 U_I18N_API SimpleDateFormat(const UnicodeString& pattern, const Locale& locale, UErrorCode& status);
450
472 U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
473 const UnicodeString& override,
474 const Locale& locale,
475 UErrorCode& status);
476
486 U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
487 DateFormatSymbols* formatDataToAdopt,
488 UErrorCode& status);
489
499 U_I18N_API SimpleDateFormat(const UnicodeString& pattern,
500 const DateFormatSymbols& formatData,
501 UErrorCode& status);
502
507 U_I18N_API SimpleDateFormat(const SimpleDateFormat&);
508
513 U_I18N_API SimpleDateFormat& operator=(const SimpleDateFormat&);
514
519 U_I18N_API virtual ~SimpleDateFormat();
520
527 U_I18N_API virtual SimpleDateFormat* clone() const override;
528
536 U_I18N_API virtual bool operator==(const Format& other) const override;
537
538 using DateFormat::format;
539
556 U_I18N_API virtual UnicodeString& format(Calendar& cal,
557 UnicodeString& appendTo,
558 FieldPosition& pos) const override;
559
578 U_I18N_API virtual UnicodeString& format(Calendar& cal,
579 UnicodeString& appendTo,
580 FieldPositionIterator* posIter,
581 UErrorCode& status) const override;
582
583 using DateFormat::parse;
584
611 U_I18N_API virtual void parse(const UnicodeString& text,
612 Calendar& cal,
613 ParsePosition& pos) const override;
614
631 U_I18N_API virtual void set2DigitYearStart(UDate d, UErrorCode& status);
632
648 U_I18N_API UDate get2DigitYearStart(UErrorCode& status) const;
649
656 U_I18N_API virtual UnicodeString& toPattern(UnicodeString& result) const;
657
680 U_I18N_API virtual UnicodeString& toLocalizedPattern(UnicodeString& result,
681 UErrorCode& status) const;
682
691 U_I18N_API virtual void applyPattern(const UnicodeString& pattern);
692
703 U_I18N_API virtual void applyLocalizedPattern(const UnicodeString& pattern, UErrorCode& status);
704
713 U_I18N_API virtual const DateFormatSymbols* getDateFormatSymbols() const;
714
721 U_I18N_API virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols);
722
728 U_I18N_API virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols);
729
741 U_I18N_API static UClassID getStaticClassID();
742
754 U_I18N_API virtual UClassID getDynamicClassID() const override;
755
765 U_I18N_API virtual void adoptCalendar(Calendar* calendarToAdopt) override;
766
767 /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following methods since they are virtual */
775 U_I18N_API virtual void adoptTimeZoneFormat(TimeZoneFormat* timeZoneFormatToAdopt);
776
782 U_I18N_API virtual void setTimeZoneFormat(const TimeZoneFormat& newTimeZoneFormat);
783
789 U_I18N_API virtual const TimeZoneFormat* getTimeZoneFormat() const;
790
801 U_I18N_API virtual void setContext(UDisplayContext value, UErrorCode& status) override;
802
810 U_I18N_API void adoptNumberFormat(NumberFormat* formatToAdopt) override;
811
827 U_I18N_API void adoptNumberFormat(const UnicodeString& fields,
828 NumberFormat* formatToAdopt,
829 UErrorCode& status);
830
836 U_I18N_API const NumberFormat* getNumberFormatForField(char16_t field) const;
837
838 #ifndef U_HIDE_INTERNAL_API
849 U_I18N_API UBool isFieldUnitIgnored(UCalendarDateFields field) const;
850
862 U_I18N_API static UBool isFieldUnitIgnored(const UnicodeString& pattern, UCalendarDateFields field);
863
872 U_I18N_API const Locale& getSmpFmtLocale() const;
873 #endif /* U_HIDE_INTERNAL_API */
874
875 private:
876 friend class DateFormat;
877 friend class DateIntervalFormat;
878
879 void initializeDefaultCentury();
880
881 void initializeBooleanAttributes();
882
883 SimpleDateFormat() = delete; // default constructor not implemented
884
893 SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
894
903 SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern
904
908 UnicodeString& _format(Calendar& cal, UnicodeString& appendTo, FieldPositionHandler& handler, UErrorCode& status) const;
909
926 void subFormat(UnicodeString &appendTo,
927 char16_t ch,
928 int32_t count,
929 UDisplayContext capitalizationContext,
930 int32_t fieldNum,
931 char16_t fieldToOutput,
932 FieldPositionHandler& handler,
933 Calendar& cal,
934 UErrorCode& status) const; // in case of illegal argument
935
949 void zeroPaddingNumber(const NumberFormat *currentNumberFormat,
950 UnicodeString &appendTo,
951 int32_t value,
952 int32_t minDigits,
953 int32_t maxDigits) const;
954
959 static UBool isNumeric(char16_t formatChar, int32_t count);
960
964 static UBool isAtNumericField(const UnicodeString &pattern, int32_t patternOffset);
965
969 static UBool isAfterNonNumericField(const UnicodeString &pattern, int32_t patternOffset);
970
978 Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
979
989 void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
990
997 void initialize(const Locale& locale, UErrorCode& status);
998
1012 int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1013 const UnicodeString* stringArray, int32_t stringArrayCount,
1014 const UnicodeString* monthPattern, Calendar& cal) const;
1015
1028 int32_t matchAlphaMonthStrings(const UnicodeString& text, int32_t start,
1029 const UnicodeString* wideStringArray, const UnicodeString* shortStringArray,
1030 int32_t stringArrayCount, Calendar& cal) const;
1031
1044 int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1045 const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
1046
1050 int32_t matchDayPeriodStrings(const UnicodeString& text, int32_t start,
1051 const UnicodeString* stringArray, int32_t stringArrayCount,
1052 int32_t &dayPeriod) const;
1053
1069 static UBool matchLiterals(const UnicodeString &pattern, int32_t &patternOffset,
1070 const UnicodeString &text, int32_t &textOffset,
1071 UBool whitespaceLenient, UBool partialMatchLenient, UBool oldLeniency);
1072
1093 int32_t subParse(const UnicodeString& text, int32_t& start, char16_t ch, int32_t count,
1094 UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
1095 int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
1096 int32_t *dayPeriod=nullptr) const;
1097
1098 void parseInt(const UnicodeString& text,
1099 Formattable& number,
1100 ParsePosition& pos,
1101 UBool allowNegative,
1102 const NumberFormat *fmt) const;
1103
1104 void parseInt(const UnicodeString& text,
1105 Formattable& number,
1106 int32_t maxDigits,
1107 ParsePosition& pos,
1108 UBool allowNegative,
1109 const NumberFormat *fmt) const;
1110
1111 int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
1112 int32_t patLoc, UBool isNegative) const;
1113
1122 int32_t countDigits(const UnicodeString& text, int32_t start, int32_t end) const;
1123
1136 static void translatePattern(const UnicodeString& originalPattern,
1137 UnicodeString& translatedPattern,
1138 const UnicodeString& from,
1139 const UnicodeString& to,
1140 UErrorCode& status);
1141
1149 void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
1150
1160 int32_t compareSimpleAffix(const UnicodeString& affix,
1161 const UnicodeString& input,
1162 int32_t pos) const;
1163
1168 int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const;
1169
1174 int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
1175
1179 void initSimpleNumberFormatter(UErrorCode &status);
1180
1184 void initNumberFormatters(const Locale &locale,UErrorCode &status);
1185
1189 void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
1190
1194 static const UCalendarDateFields fgPatternIndexToCalendarField[];
1195
1199 static const UDateFormatField fgPatternIndexToDateFormatField[];
1200
1204 TimeZoneFormat *tzFormat(UErrorCode &status) const;
1205
1206 const NumberFormat* getNumberFormatByIndex(UDateFormatField index) const;
1207
1214 static const int32_t fgCalendarFieldToLevel[];
1215
1219 static int32_t getLevelFromChar(char16_t ch);
1220
1224 static UBool isSyntaxChar(char16_t ch);
1225
1229 UnicodeString fPattern;
1230
1234 UnicodeString fDateOverride;
1235
1239 UnicodeString fTimeOverride;
1240
1241
1245 Locale fLocale;
1246
1251 DateFormatSymbols* fSymbols = nullptr; // Owned
1252
1256 TimeZoneFormat* fTimeZoneFormat = nullptr;
1257
1266 UDate fDefaultCenturyStart;
1267
1268 UBool fHasMinute;
1269 UBool fHasSecond;
1270 UBool fHasHanYearChar; // pattern contains the Han year character \u5E74
1271
1275 void parsePattern();
1276
1280 /*transient*/ int32_t fDefaultCenturyStartYear;
1281
1282 struct NSOverride : public UMemory {
1283 const SharedNumberFormat *snf;
1284 int32_t hash;
1285 NSOverride *next;
1286 void free();
1287 NSOverride() : snf(nullptr), hash(0), next(nullptr) {
1288 }
1289 ~NSOverride();
1290 };
1291
1296 const SharedNumberFormat **fSharedNumberFormatters = nullptr;
1297
1305 const number::SimpleNumberFormatter* fSimpleNumberFormatter = nullptr;
1306
1307 UBool fHaveDefaultCentury;
1308
1309 const BreakIterator* fCapitalizationBrkIter = nullptr;
1310 };
1311
1312 inline UDate
1313 SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const
1314 {
1315 return fDefaultCenturyStart;
1316 }
1317
1318 U_NAMESPACE_END
1319
1320 #endif /* #if !UCONFIG_NO_FORMATTING */
1321
1322 #endif /* U_SHOW_CPLUSPLUS_API */
1323
1324 #endif // _SMPDTFMT
1325 //eof
brkiter.h
C++ API: Break Iterator.
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::DateFormatSymbols
DateFormatSymbols is a public class for encapsulating localizable date-time formatting data – includi...
Definition: dtfmtsym.h:84
icu::DateFormat
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:144
icu::DateFormat::parse
virtual U_I18N_API UDate parse(const UnicodeString &text, UErrorCode &status) const
Parse a date/time string.
icu::DateFormat::format
U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
icu::DateFormat::EStyle
EStyle
Constants for various style patterns.
Definition: datefmt.h:155
icu::DateIntervalFormat
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent man...
Definition: dtitvfmt.h:300
icu::FieldPositionIterator
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
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::Formattable
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:198
icu::MessageFormat
Definition: msgfmt.h:345
icu::NumberFormat
Definition: numfmt.h:175
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:385
icu::SimpleDateFormat::SimpleDateFormat
U_I18N_API SimpleDateFormat(const UnicodeString &pattern, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and the default locale.
icu::SimpleDateFormat::isFieldUnitIgnored
static U_I18N_API UBool isFieldUnitIgnored(const UnicodeString &pattern, UCalendarDateFields field)
This is for ICU internal use only.
icu::SimpleDateFormat::adoptCalendar
virtual U_I18N_API void adoptCalendar(Calendar *calendarToAdopt) override
Set the calendar to be used by this date format.
icu::SimpleDateFormat::getTimeZoneFormat
virtual U_I18N_API const TimeZoneFormat * getTimeZoneFormat() const
Gets the time zone format object associated with this date/time formatter.
icu::SimpleDateFormat::SimpleDateFormat
U_I18N_API SimpleDateFormat(const UnicodeString &pattern, const DateFormatSymbols &formatData, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and locale-specific symbol data.
icu::SimpleDateFormat::setDateFormatSymbols
virtual U_I18N_API void setDateFormatSymbols(const DateFormatSymbols &newFormatSymbols)
Set the date/time formatting data.
icu::SimpleDateFormat::operator==
virtual U_I18N_API bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
icu::SimpleDateFormat::toPattern
virtual U_I18N_API UnicodeString & toPattern(UnicodeString &result) const
Return a pattern string describing this date format.
icu::SimpleDateFormat::setContext
virtual U_I18N_API void setContext(UDisplayContext value, UErrorCode &status) override
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
icu::SimpleDateFormat::setTimeZoneFormat
virtual U_I18N_API void setTimeZoneFormat(const TimeZoneFormat &newTimeZoneFormat)
Sets the TimeZoneFormat to be used by this date/time formatter.
icu::SimpleDateFormat::set2DigitYearStart
virtual U_I18N_API void set2DigitYearStart(UDate d, UErrorCode &status)
Set the start UDate used to interpret two-digit year strings.
icu::SimpleDateFormat::adoptNumberFormat
U_I18N_API void adoptNumberFormat(const UnicodeString &fields, NumberFormat *formatToAdopt, UErrorCode &status)
Allow the user to set the NumberFormat for several fields It can be a single field like: "y"(year) or...
icu::SimpleDateFormat::format
virtual U_I18N_API UnicodeString & format(Calendar &cal, UnicodeString &appendTo, FieldPosition &pos) const override
Format a date or time, which is the standard millis since 24:00 GMT, Jan 1, 1970.
icu::SimpleDateFormat::applyPattern
virtual U_I18N_API void applyPattern(const UnicodeString &pattern)
Apply the given unlocalized pattern string to this date format.
icu::SimpleDateFormat::getSmpFmtLocale
U_I18N_API const Locale & getSmpFmtLocale() const
This is for ICU internal use only.
icu::SimpleDateFormat::SimpleDateFormat
U_I18N_API SimpleDateFormat(UErrorCode &status)
Construct a SimpleDateFormat using the default pattern for the default locale.
icu::SimpleDateFormat::isFieldUnitIgnored
U_I18N_API UBool isFieldUnitIgnored(UCalendarDateFields field) const
This is for ICU internal use only.
icu::SimpleDateFormat::SimpleDateFormat
U_I18N_API SimpleDateFormat(const UnicodeString &pattern, const Locale &locale, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and locale.
icu::SimpleDateFormat::SimpleDateFormat
U_I18N_API SimpleDateFormat(const UnicodeString &pattern, const UnicodeString &override, const Locale &locale, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern, numbering system override, and locale.
icu::SimpleDateFormat::adoptNumberFormat
U_I18N_API void adoptNumberFormat(NumberFormat *formatToAdopt) override
Overrides base class method and This method clears per field NumberFormat instances previously set by...
icu::SimpleDateFormat::SimpleDateFormat
U_I18N_API SimpleDateFormat(const UnicodeString &pattern, const UnicodeString &override, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern, numbering system override, and the default loca...
icu::SimpleDateFormat::getDynamicClassID
virtual U_I18N_API UClassID getDynamicClassID() const override
Returns a unique class ID POLYMORPHICALLY.
icu::SimpleDateFormat::adoptTimeZoneFormat
virtual U_I18N_API void adoptTimeZoneFormat(TimeZoneFormat *timeZoneFormatToAdopt)
Sets the TimeZoneFormat to be used by this date/time formatter.
icu::SimpleDateFormat::format
virtual U_I18N_API UnicodeString & format(Calendar &cal, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const override
Format a date or time, which is the standard millis since 24:00 GMT, Jan 1, 1970.
icu::SimpleDateFormat::parse
virtual U_I18N_API void parse(const UnicodeString &text, Calendar &cal, ParsePosition &pos) const override
Parse a date/time string beginning at the given parse position.
icu::SimpleDateFormat::operator=
U_I18N_API SimpleDateFormat & operator=(const SimpleDateFormat &)
Assignment operator.
icu::SimpleDateFormat::adoptDateFormatSymbols
virtual U_I18N_API void adoptDateFormatSymbols(DateFormatSymbols *newFormatSymbols)
Set the date/time formatting symbols.
icu::SimpleDateFormat::clone
virtual U_I18N_API SimpleDateFormat * clone() const override
Clone this Format object polymorphically.
icu::SimpleDateFormat::SimpleDateFormat
U_I18N_API SimpleDateFormat(const UnicodeString &pattern, DateFormatSymbols *formatDataToAdopt, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and locale-specific symbol data.
icu::SimpleDateFormat::getNumberFormatForField
U_I18N_API const NumberFormat * getNumberFormatForField(char16_t field) const
Get the numbering system to be used for a particular field.
icu::SimpleDateFormat::~SimpleDateFormat
virtual U_I18N_API ~SimpleDateFormat()
Destructor.
icu::SimpleDateFormat::getDateFormatSymbols
virtual U_I18N_API const DateFormatSymbols * getDateFormatSymbols() const
Gets the date/time formatting symbols (this is an object carrying the various strings and other symbo...
icu::SimpleDateFormat::toLocalizedPattern
virtual U_I18N_API UnicodeString & toLocalizedPattern(UnicodeString &result, UErrorCode &status) const
Return a localized pattern string describing this date format.
icu::SimpleDateFormat::getStaticClassID
static U_I18N_API UClassID getStaticClassID()
Return the class ID for this class.
icu::SimpleDateFormat::applyLocalizedPattern
virtual U_I18N_API void applyLocalizedPattern(const UnicodeString &pattern, UErrorCode &status)
Apply the given localized pattern string to this date format.
icu::SimpleDateFormat::SimpleDateFormat
U_I18N_API SimpleDateFormat(const SimpleDateFormat &)
Copy constructor.
icu::TimeZoneFormat
TimeZoneFormat supports time zone display name formatting and parsing.
Definition: tzfmt.h:274
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:303
datefmt.h
C++ API: Abstract class for converting dates.
tzfmt.h
C++ API: TimeZoneFormat.
UTimeZoneFormatTimeType
UTimeZoneFormatTimeType
Constants for time types used by TimeZoneFormat APIs for receiving time type (standard time,...
Definition: tzfmt.h:206
UCalendarDateFields
UCalendarDateFields
Possible fields in a UCalendar.
Definition: ucal.h:202
UDateFormatField
UDateFormatField
FieldPosition and UFieldPosition selectors for format fields defined by DateFormat and UDateFormat.
Definition: udat.h:515
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
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:509
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
UDate
double UDate
Date and Time data type.
Definition: utypes.h:218