ICU 74.1 74.1

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
727 class U_I18N_API SimpleDateFormat: public DateFormat {
728 public:
738 SimpleDateFormat(UErrorCode& status);
739
751 SimpleDateFormat(const UnicodeString& pattern,
752 UErrorCode& status);
753
775 SimpleDateFormat(const UnicodeString& pattern,
776 const UnicodeString& override,
777 UErrorCode& status);
778
791 SimpleDateFormat(const UnicodeString& pattern,
792 const Locale& locale,
793 UErrorCode& status);
794
816 SimpleDateFormat(const UnicodeString& pattern,
817 const UnicodeString& override,
818 const Locale& locale,
819 UErrorCode& status);
820
830 SimpleDateFormat(const UnicodeString& pattern,
831 DateFormatSymbols* formatDataToAdopt,
832 UErrorCode& status);
833
843 SimpleDateFormat(const UnicodeString& pattern,
844 const DateFormatSymbols& formatData,
845 UErrorCode& status);
846
851 SimpleDateFormat(const SimpleDateFormat&);
852
857 SimpleDateFormat& operator=(const SimpleDateFormat&);
858
863 virtual ~SimpleDateFormat();
864
871 virtual SimpleDateFormat* clone() const override;
872
880 virtual bool operator==(const Format& other) const override;
881
882
883 using DateFormat::format;
884
901 virtual UnicodeString& format( Calendar& cal,
902 UnicodeString& appendTo,
903 FieldPosition& pos) const override;
904
923 virtual UnicodeString& format( Calendar& cal,
924 UnicodeString& appendTo,
925 FieldPositionIterator* posIter,
926 UErrorCode& status) const override;
927
928 using DateFormat::parse;
929
956 virtual void parse( const UnicodeString& text,
957 Calendar& cal,
958 ParsePosition& pos) const override;
959
960
977 virtual void set2DigitYearStart(UDate d, UErrorCode& status);
978
994 UDate get2DigitYearStart(UErrorCode& status) const;
995
1002 virtual UnicodeString& toPattern(UnicodeString& result) const;
1003
1026 virtual UnicodeString& toLocalizedPattern(UnicodeString& result,
1027 UErrorCode& status) const;
1028
1037 virtual void applyPattern(const UnicodeString& pattern);
1038
1049 virtual void applyLocalizedPattern(const UnicodeString& pattern,
1050 UErrorCode& status);
1051
1060 virtual const DateFormatSymbols* getDateFormatSymbols(void) const;
1061
1068 virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols);
1069
1075 virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols);
1076
1088 static UClassID U_EXPORT2 getStaticClassID(void);
1089
1101 virtual UClassID getDynamicClassID(void) const override;
1102
1112 virtual void adoptCalendar(Calendar* calendarToAdopt) override;
1113
1114 /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following methods since they are virtual */
1122 virtual void adoptTimeZoneFormat(TimeZoneFormat* timeZoneFormatToAdopt);
1123
1129 virtual void setTimeZoneFormat(const TimeZoneFormat& newTimeZoneFormat);
1130
1136 virtual const TimeZoneFormat* getTimeZoneFormat(void) const;
1137
1148 virtual void setContext(UDisplayContext value, UErrorCode& status) override;
1149
1157 void adoptNumberFormat(NumberFormat *formatToAdopt) override;
1158
1174 void adoptNumberFormat(const UnicodeString& fields, NumberFormat *formatToAdopt, UErrorCode &status);
1175
1181 const NumberFormat * getNumberFormatForField(char16_t field) const;
1182
1183 #ifndef U_HIDE_INTERNAL_API
1194 UBool isFieldUnitIgnored(UCalendarDateFields field) const;
1195
1196
1208 static UBool isFieldUnitIgnored(const UnicodeString& pattern,
1209 UCalendarDateFields field);
1210
1219 const Locale& getSmpFmtLocale(void) const;
1220 #endif /* U_HIDE_INTERNAL_API */
1221
1222 private:
1223 friend class DateFormat;
1224 friend class DateIntervalFormat;
1225
1226 void initializeDefaultCentury(void);
1227
1228 void initializeBooleanAttributes(void);
1229
1230 SimpleDateFormat() = delete; // default constructor not implemented
1231
1240 SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1241
1250 SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern
1251
1255 UnicodeString& _format(Calendar& cal, UnicodeString& appendTo, FieldPositionHandler& handler, UErrorCode& status) const;
1256
1273 void subFormat(UnicodeString &appendTo,
1274 char16_t ch,
1275 int32_t count,
1276 UDisplayContext capitalizationContext,
1277 int32_t fieldNum,
1278 char16_t fieldToOutput,
1279 FieldPositionHandler& handler,
1280 Calendar& cal,
1281 UErrorCode& status) const; // in case of illegal argument
1282
1296 void zeroPaddingNumber(const NumberFormat *currentNumberFormat,
1297 UnicodeString &appendTo,
1298 int32_t value,
1299 int32_t minDigits,
1300 int32_t maxDigits) const;
1301
1306 static UBool isNumeric(char16_t formatChar, int32_t count);
1307
1311 static UBool isAtNumericField(const UnicodeString &pattern, int32_t patternOffset);
1312
1316 static UBool isAfterNonNumericField(const UnicodeString &pattern, int32_t patternOffset);
1317
1325 Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
1326
1336 void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1337
1344 void initialize(const Locale& locale, UErrorCode& status);
1345
1359 int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1360 const UnicodeString* stringArray, int32_t stringArrayCount,
1361 const UnicodeString* monthPattern, Calendar& cal) const;
1362
1375 int32_t matchAlphaMonthStrings(const UnicodeString& text, int32_t start,
1376 const UnicodeString* wideStringArray, const UnicodeString* shortStringArray,
1377 int32_t stringArrayCount, Calendar& cal) const;
1378
1391 int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1392 const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
1393
1397 int32_t matchDayPeriodStrings(const UnicodeString& text, int32_t start,
1398 const UnicodeString* stringArray, int32_t stringArrayCount,
1399 int32_t &dayPeriod) const;
1400
1416 static UBool matchLiterals(const UnicodeString &pattern, int32_t &patternOffset,
1417 const UnicodeString &text, int32_t &textOffset,
1418 UBool whitespaceLenient, UBool partialMatchLenient, UBool oldLeniency);
1419
1440 int32_t subParse(const UnicodeString& text, int32_t& start, char16_t ch, int32_t count,
1441 UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
1442 int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
1443 int32_t *dayPeriod=nullptr) const;
1444
1445 void parseInt(const UnicodeString& text,
1446 Formattable& number,
1447 ParsePosition& pos,
1448 UBool allowNegative,
1449 const NumberFormat *fmt) const;
1450
1451 void parseInt(const UnicodeString& text,
1452 Formattable& number,
1453 int32_t maxDigits,
1454 ParsePosition& pos,
1455 UBool allowNegative,
1456 const NumberFormat *fmt) const;
1457
1458 int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
1459 int32_t patLoc, UBool isNegative) const;
1460
1469 int32_t countDigits(const UnicodeString& text, int32_t start, int32_t end) const;
1470
1483 static void translatePattern(const UnicodeString& originalPattern,
1484 UnicodeString& translatedPattern,
1485 const UnicodeString& from,
1486 const UnicodeString& to,
1487 UErrorCode& status);
1488
1496 void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
1497
1507 int32_t compareSimpleAffix(const UnicodeString& affix,
1508 const UnicodeString& input,
1509 int32_t pos) const;
1510
1515 int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const;
1516
1521 int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
1522
1526 void initSimpleNumberFormatter(UErrorCode &status);
1527
1531 void initNumberFormatters(const Locale &locale,UErrorCode &status);
1532
1536 void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
1537
1541 static const UCalendarDateFields fgPatternIndexToCalendarField[];
1542
1546 static const UDateFormatField fgPatternIndexToDateFormatField[];
1547
1551 TimeZoneFormat *tzFormat(UErrorCode &status) const;
1552
1553 const NumberFormat* getNumberFormatByIndex(UDateFormatField index) const;
1554
1561 static const int32_t fgCalendarFieldToLevel[];
1562
1566 static int32_t getLevelFromChar(char16_t ch);
1567
1571 static UBool isSyntaxChar(char16_t ch);
1572
1576 UnicodeString fPattern;
1577
1581 UnicodeString fDateOverride;
1582
1586 UnicodeString fTimeOverride;
1587
1588
1592 Locale fLocale;
1593
1598 DateFormatSymbols* fSymbols = nullptr; // Owned
1599
1603 TimeZoneFormat* fTimeZoneFormat = nullptr;
1604
1613 UDate fDefaultCenturyStart;
1614
1615 UBool fHasMinute;
1616 UBool fHasSecond;
1617 UBool fHasHanYearChar; // pattern contains the Han year character \u5E74
1618
1622 void parsePattern();
1623
1627 /*transient*/ int32_t fDefaultCenturyStartYear;
1628
1629 struct NSOverride : public UMemory {
1630 const SharedNumberFormat *snf;
1631 int32_t hash;
1632 NSOverride *next;
1633 void free();
1634 NSOverride() : snf(nullptr), hash(0), next(nullptr) {
1635 }
1636 ~NSOverride();
1637 };
1638
1643 const SharedNumberFormat **fSharedNumberFormatters = nullptr;
1644
1652 const number::SimpleNumberFormatter* fSimpleNumberFormatter = nullptr;
1653
1654 UBool fHaveDefaultCentury;
1655
1656 const BreakIterator* fCapitalizationBrkIter = nullptr;
1657 };
1658
1659 inline UDate
1660 SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const
1661 {
1662 return fDefaultCenturyStart;
1663 }
1664
1665 U_NAMESPACE_END
1666
1667 #endif /* #if !UCONFIG_NO_FORMATTING */
1668
1669 #endif /* U_SHOW_CPLUSPLUS_API */
1670
1671 #endif // _SMPDTFMT
1672 //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:156
icu::DateFormat::parse
virtual UDate parse(const UnicodeString &text, UErrorCode &status) const
Parse a date/time string.
icu::DateFormat::EStyle
EStyle
Constants for various style patterns.
Definition: datefmt.h:167
icu::DateFormat::format
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
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:64
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
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:727
icu::SimpleDateFormat::format
virtual 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::operator==
virtual bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
icu::SimpleDateFormat::parse
virtual void parse(const UnicodeString &text, Calendar &cal, ParsePosition &pos) const override
Parse a date/time string beginning at the given parse position.
icu::SimpleDateFormat::SimpleDateFormat
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::getDateFormatSymbols
virtual const DateFormatSymbols * getDateFormatSymbols(void) const
Gets the date/time formatting symbols (this is an object carrying the various strings and other symbo...
icu::SimpleDateFormat::applyPattern
virtual void applyPattern(const UnicodeString &pattern)
Apply the given unlocalized pattern string to this date format.
icu::SimpleDateFormat::SimpleDateFormat
SimpleDateFormat(const SimpleDateFormat &)
Copy constructor.
icu::SimpleDateFormat::adoptDateFormatSymbols
virtual void adoptDateFormatSymbols(DateFormatSymbols *newFormatSymbols)
Set the date/time formatting symbols.
icu::SimpleDateFormat::SimpleDateFormat
SimpleDateFormat(const UnicodeString &pattern, DateFormatSymbols *formatDataToAdopt, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and locale-specific symbol data.
icu::SimpleDateFormat::setDateFormatSymbols
virtual void setDateFormatSymbols(const DateFormatSymbols &newFormatSymbols)
Set the date/time formatting data.
icu::SimpleDateFormat::toPattern
virtual UnicodeString & toPattern(UnicodeString &result) const
Return a pattern string describing this date format.
icu::SimpleDateFormat::getStaticClassID
static UClassID getStaticClassID(void)
Return the class ID for this class.
icu::SimpleDateFormat::SimpleDateFormat
SimpleDateFormat(const UnicodeString &pattern, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and the default locale.
icu::SimpleDateFormat::getTimeZoneFormat
virtual const TimeZoneFormat * getTimeZoneFormat(void) const
Gets the time zone format object associated with this date/time formatter.
icu::SimpleDateFormat::toLocalizedPattern
virtual UnicodeString & toLocalizedPattern(UnicodeString &result, UErrorCode &status) const
Return a localized pattern string describing this date format.
icu::SimpleDateFormat::getNumberFormatForField
const NumberFormat * getNumberFormatForField(char16_t field) const
Get the numbering system to be used for a particular field.
icu::SimpleDateFormat::setContext
virtual void setContext(UDisplayContext value, UErrorCode &status) override
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
icu::SimpleDateFormat::adoptNumberFormat
void adoptNumberFormat(NumberFormat *formatToAdopt) override
Overrides base class method and This method clears per field NumberFormat instances previously set by...
icu::SimpleDateFormat::isFieldUnitIgnored
UBool isFieldUnitIgnored(UCalendarDateFields field) const
This is for ICU internal use only.
icu::SimpleDateFormat::SimpleDateFormat
SimpleDateFormat(const UnicodeString &pattern, const DateFormatSymbols &formatData, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and locale-specific symbol data.
icu::SimpleDateFormat::getDynamicClassID
virtual UClassID getDynamicClassID(void) const override
Returns a unique class ID POLYMORPHICALLY.
icu::SimpleDateFormat::setTimeZoneFormat
virtual void setTimeZoneFormat(const TimeZoneFormat &newTimeZoneFormat)
Sets the TimeZoneFormat to be used by this date/time formatter.
icu::SimpleDateFormat::operator=
SimpleDateFormat & operator=(const SimpleDateFormat &)
Assignment operator.
icu::SimpleDateFormat::adoptCalendar
virtual void adoptCalendar(Calendar *calendarToAdopt) override
Set the calendar to be used by this date format.
icu::SimpleDateFormat::SimpleDateFormat
SimpleDateFormat(UErrorCode &status)
Construct a SimpleDateFormat using the default pattern for the default locale.
icu::SimpleDateFormat::format
virtual 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::set2DigitYearStart
virtual void set2DigitYearStart(UDate d, UErrorCode &status)
Set the start UDate used to interpret two-digit year strings.
icu::SimpleDateFormat::~SimpleDateFormat
virtual ~SimpleDateFormat()
Destructor.
icu::SimpleDateFormat::adoptNumberFormat
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::SimpleDateFormat
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::getSmpFmtLocale
const Locale & getSmpFmtLocale(void) const
This is for ICU internal use only.
icu::SimpleDateFormat::SimpleDateFormat
SimpleDateFormat(const UnicodeString &pattern, const Locale &locale, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and locale.
icu::SimpleDateFormat::isFieldUnitIgnored
static UBool isFieldUnitIgnored(const UnicodeString &pattern, UCalendarDateFields field)
This is for ICU internal use only.
icu::SimpleDateFormat::adoptTimeZoneFormat
virtual void adoptTimeZoneFormat(TimeZoneFormat *timeZoneFormatToAdopt)
Sets the TimeZoneFormat to be used by this date/time formatter.
icu::SimpleDateFormat::applyLocalizedPattern
virtual void applyLocalizedPattern(const UnicodeString &pattern, UErrorCode &status)
Apply the given localized pattern string to this date format.
icu::SimpleDateFormat::clone
virtual SimpleDateFormat * clone() const override
Clone this Format object polymorphically.
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:296
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: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_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
UDate
double UDate
Date and Time data type.
Definition: utypes.h:203