ICU 72.1  72.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"
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 }
59 
727 public:
738 
751  UErrorCode& status);
752 
775  const UnicodeString& override,
776  UErrorCode& status);
777 
791  const Locale& locale,
792  UErrorCode& status);
793 
816  const UnicodeString& override,
817  const Locale& locale,
818  UErrorCode& status);
819 
830  DateFormatSymbols* formatDataToAdopt,
831  UErrorCode& status);
832 
843  const DateFormatSymbols& formatData,
844  UErrorCode& status);
845 
851 
857 
862  virtual ~SimpleDateFormat();
863 
870  virtual SimpleDateFormat* clone() const override;
871 
879  virtual bool operator==(const Format& other) const override;
880 
881 
882  using DateFormat::format;
883 
900  virtual UnicodeString& format( Calendar& cal,
901  UnicodeString& appendTo,
902  FieldPosition& pos) const override;
903 
922  virtual UnicodeString& format( Calendar& cal,
923  UnicodeString& appendTo,
924  FieldPositionIterator* posIter,
925  UErrorCode& status) const override;
926 
927  using DateFormat::parse;
928 
955  virtual void parse( const UnicodeString& text,
956  Calendar& cal,
957  ParsePosition& pos) const override;
958 
959 
976  virtual void set2DigitYearStart(UDate d, UErrorCode& status);
977 
993  UDate get2DigitYearStart(UErrorCode& status) const;
994 
1001  virtual UnicodeString& toPattern(UnicodeString& result) const;
1002 
1026  UErrorCode& status) const;
1027 
1036  virtual void applyPattern(const UnicodeString& pattern);
1037 
1048  virtual void applyLocalizedPattern(const UnicodeString& pattern,
1049  UErrorCode& status);
1050 
1059  virtual const DateFormatSymbols* getDateFormatSymbols(void) const;
1060 
1067  virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols);
1068 
1074  virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols);
1075 
1087  static UClassID U_EXPORT2 getStaticClassID(void);
1088 
1100  virtual UClassID getDynamicClassID(void) const override;
1101 
1111  virtual void adoptCalendar(Calendar* calendarToAdopt) override;
1112 
1113  /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following methods since they are virtual */
1121  virtual void adoptTimeZoneFormat(TimeZoneFormat* timeZoneFormatToAdopt);
1122 
1128  virtual void setTimeZoneFormat(const TimeZoneFormat& newTimeZoneFormat);
1129 
1135  virtual const TimeZoneFormat* getTimeZoneFormat(void) const;
1136 
1147  virtual void setContext(UDisplayContext value, UErrorCode& status) override;
1148 
1156  void adoptNumberFormat(NumberFormat *formatToAdopt) override;
1157 
1173  void adoptNumberFormat(const UnicodeString& fields, NumberFormat *formatToAdopt, UErrorCode &status);
1174 
1180  const NumberFormat * getNumberFormatForField(char16_t field) const;
1181 
1182 #ifndef U_HIDE_INTERNAL_API
1194 
1195 
1207  static UBool isFieldUnitIgnored(const UnicodeString& pattern,
1208  UCalendarDateFields field);
1209 
1218  const Locale& getSmpFmtLocale(void) const;
1219 #endif /* U_HIDE_INTERNAL_API */
1220 
1221 private:
1222  friend class DateFormat;
1223  friend class DateIntervalFormat;
1224 
1225  void initializeDefaultCentury(void);
1226 
1227  void initializeBooleanAttributes(void);
1228 
1229  SimpleDateFormat() = delete; // default constructor not implemented
1230 
1239  SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1240 
1249  SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern
1250 
1254  UnicodeString& _format(Calendar& cal, UnicodeString& appendTo, FieldPositionHandler& handler, UErrorCode& status) const;
1255 
1272  void subFormat(UnicodeString &appendTo,
1273  char16_t ch,
1274  int32_t count,
1275  UDisplayContext capitalizationContext,
1276  int32_t fieldNum,
1277  char16_t fieldToOutput,
1278  FieldPositionHandler& handler,
1279  Calendar& cal,
1280  UErrorCode& status) const; // in case of illegal argument
1281 
1295  void zeroPaddingNumber(const NumberFormat *currentNumberFormat,
1296  UnicodeString &appendTo,
1297  int32_t value,
1298  int32_t minDigits,
1299  int32_t maxDigits) const;
1300 
1305  static UBool isNumeric(char16_t formatChar, int32_t count);
1306 
1310  static UBool isAtNumericField(const UnicodeString &pattern, int32_t patternOffset);
1311 
1315  static UBool isAfterNonNumericField(const UnicodeString &pattern, int32_t patternOffset);
1316 
1324  Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
1325 
1335  void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1336 
1343  void initialize(const Locale& locale, UErrorCode& status);
1344 
1358  int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1359  const UnicodeString* stringArray, int32_t stringArrayCount,
1360  const UnicodeString* monthPattern, Calendar& cal) const;
1361 
1374  int32_t matchAlphaMonthStrings(const UnicodeString& text, int32_t start,
1375  const UnicodeString* wideStringArray, const UnicodeString* shortStringArray,
1376  int32_t stringArrayCount, Calendar& cal) const;
1377 
1390  int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1391  const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
1392 
1396  int32_t matchDayPeriodStrings(const UnicodeString& text, int32_t start,
1397  const UnicodeString* stringArray, int32_t stringArrayCount,
1398  int32_t &dayPeriod) const;
1399 
1415  static UBool matchLiterals(const UnicodeString &pattern, int32_t &patternOffset,
1416  const UnicodeString &text, int32_t &textOffset,
1417  UBool whitespaceLenient, UBool partialMatchLenient, UBool oldLeniency);
1418 
1439  int32_t subParse(const UnicodeString& text, int32_t& start, char16_t ch, int32_t count,
1440  UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
1441  int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
1442  int32_t *dayPeriod=NULL) const;
1443 
1444  void parseInt(const UnicodeString& text,
1445  Formattable& number,
1446  ParsePosition& pos,
1447  UBool allowNegative,
1448  const NumberFormat *fmt) const;
1449 
1450  void parseInt(const UnicodeString& text,
1451  Formattable& number,
1452  int32_t maxDigits,
1453  ParsePosition& pos,
1454  UBool allowNegative,
1455  const NumberFormat *fmt) const;
1456 
1457  int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
1458  int32_t patLoc, UBool isNegative) const;
1459 
1468  int32_t countDigits(const UnicodeString& text, int32_t start, int32_t end) const;
1469 
1482  static void translatePattern(const UnicodeString& originalPattern,
1483  UnicodeString& translatedPattern,
1484  const UnicodeString& from,
1485  const UnicodeString& to,
1486  UErrorCode& status);
1487 
1495  void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
1496 
1506  int32_t compareSimpleAffix(const UnicodeString& affix,
1507  const UnicodeString& input,
1508  int32_t pos) const;
1509 
1514  int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const;
1515 
1520  int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
1521 
1525  void initFastNumberFormatters(UErrorCode& status);
1526 
1530  void freeFastNumberFormatters();
1531 
1535  void initNumberFormatters(const Locale &locale,UErrorCode &status);
1536 
1540  void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
1541 
1545  static const UCalendarDateFields fgPatternIndexToCalendarField[];
1546 
1550  static const UDateFormatField fgPatternIndexToDateFormatField[];
1551 
1555  TimeZoneFormat *tzFormat(UErrorCode &status) const;
1556 
1557  const NumberFormat* getNumberFormatByIndex(UDateFormatField index) const;
1558 
1565  static const int32_t fgCalendarFieldToLevel[];
1566 
1570  static int32_t getLevelFromChar(char16_t ch);
1571 
1575  static UBool isSyntaxChar(char16_t ch);
1576 
1580  UnicodeString fPattern;
1581 
1585  UnicodeString fDateOverride;
1586 
1590  UnicodeString fTimeOverride;
1591 
1592 
1596  Locale fLocale;
1597 
1602  DateFormatSymbols* fSymbols; // Owned
1603 
1607  TimeZoneFormat* fTimeZoneFormat;
1608 
1617  UDate fDefaultCenturyStart;
1618 
1619  UBool fHasMinute;
1620  UBool fHasSecond;
1621  UBool fHasHanYearChar; // pattern contains the Han year character \u5E74
1622 
1626  void parsePattern();
1627 
1631  /*transient*/ int32_t fDefaultCenturyStartYear;
1632 
1633  struct NSOverride : public UMemory {
1634  const SharedNumberFormat *snf;
1635  int32_t hash;
1636  NSOverride *next;
1637  void free();
1638  NSOverride() : snf(NULL), hash(0), next(NULL) {
1639  }
1640  ~NSOverride();
1641  };
1642 
1647  const SharedNumberFormat **fSharedNumberFormatters;
1648 
1649  enum NumberFormatterKey {
1650  SMPDTFMT_NF_1x10,
1651  SMPDTFMT_NF_2x10,
1652  SMPDTFMT_NF_3x10,
1653  SMPDTFMT_NF_4x10,
1654  SMPDTFMT_NF_2x2,
1655  SMPDTFMT_NF_COUNT
1656  };
1657 
1661  const number::LocalizedNumberFormatter* fFastNumberFormatters[SMPDTFMT_NF_COUNT] = {};
1662 
1663  UBool fHaveDefaultCentury;
1664 
1665  const BreakIterator* fCapitalizationBrkIter;
1666 };
1667 
1668 inline UDate
1669 SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const
1670 {
1671  return fDefaultCenturyStart;
1672 }
1673 
1674 U_NAMESPACE_END
1675 
1676 #endif /* #if !UCONFIG_NO_FORMATTING */
1677 
1678 #endif /* U_SHOW_CPLUSPLUS_API */
1679 
1680 #endif // _SMPDTFMT
1681 //eof
C++ API: Break Iterator.
Calendar is an abstract base class for converting between a UDate object and a set of integer fields ...
Definition: calendar.h:189
DateFormatSymbols is a public class for encapsulating localizable date-time formatting data – includi...
Definition: dtfmtsym.h:84
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:156
virtual UDate parse(const UnicodeString &text, UErrorCode &status) const
Parse a date/time string.
EStyle
Constants for various style patterns.
Definition: datefmt.h:167
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent man...
Definition: dtitvfmt.h:300
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
Base class for all formats.
Definition: format.h:98
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:726
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.
virtual bool operator==(const Format &other) const override
Return true if the given Format objects are semantically equal.
virtual void parse(const UnicodeString &text, Calendar &cal, ParsePosition &pos) const override
Parse a date/time string beginning at the given parse position.
SimpleDateFormat(const UnicodeString &pattern, const UnicodeString &override, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern, numbering system override, and the default loca...
virtual const DateFormatSymbols * getDateFormatSymbols(void) const
Gets the date/time formatting symbols (this is an object carrying the various strings and other symbo...
virtual void applyPattern(const UnicodeString &pattern)
Apply the given unlocalized pattern string to this date format.
SimpleDateFormat(const SimpleDateFormat &)
Copy constructor.
virtual void adoptDateFormatSymbols(DateFormatSymbols *newFormatSymbols)
Set the date/time formatting symbols.
SimpleDateFormat(const UnicodeString &pattern, DateFormatSymbols *formatDataToAdopt, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and locale-specific symbol data.
virtual void setDateFormatSymbols(const DateFormatSymbols &newFormatSymbols)
Set the date/time formatting data.
virtual UnicodeString & toPattern(UnicodeString &result) const
Return a pattern string describing this date format.
static UClassID getStaticClassID(void)
Return the class ID for this class.
SimpleDateFormat(const UnicodeString &pattern, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and the default locale.
virtual const TimeZoneFormat * getTimeZoneFormat(void) const
Gets the time zone format object associated with this date/time formatter.
virtual UnicodeString & toLocalizedPattern(UnicodeString &result, UErrorCode &status) const
Return a localized pattern string describing this date format.
const NumberFormat * getNumberFormatForField(char16_t field) const
Get the numbering system to be used for a particular field.
virtual void setContext(UDisplayContext value, UErrorCode &status) override
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
void adoptNumberFormat(NumberFormat *formatToAdopt) override
Overrides base class method and This method clears per field NumberFormat instances previously set by...
UBool isFieldUnitIgnored(UCalendarDateFields field) const
This is for ICU internal use only.
SimpleDateFormat(const UnicodeString &pattern, const DateFormatSymbols &formatData, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and locale-specific symbol data.
virtual UClassID getDynamicClassID(void) const override
Returns a unique class ID POLYMORPHICALLY.
virtual void setTimeZoneFormat(const TimeZoneFormat &newTimeZoneFormat)
Sets the TimeZoneFormat to be used by this date/time formatter.
SimpleDateFormat & operator=(const SimpleDateFormat &)
Assignment operator.
virtual void adoptCalendar(Calendar *calendarToAdopt) override
Set the calendar to be used by this date format.
SimpleDateFormat(UErrorCode &status)
Construct a SimpleDateFormat using the default pattern for the default locale.
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.
virtual void set2DigitYearStart(UDate d, UErrorCode &status)
Set the start UDate used to interpret two-digit year strings.
virtual ~SimpleDateFormat()
Destructor.
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...
SimpleDateFormat(const UnicodeString &pattern, const UnicodeString &override, const Locale &locale, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern, numbering system override, and locale.
const Locale & getSmpFmtLocale(void) const
This is for ICU internal use only.
SimpleDateFormat(const UnicodeString &pattern, const Locale &locale, UErrorCode &status)
Construct a SimpleDateFormat using the given pattern and locale.
static UBool isFieldUnitIgnored(const UnicodeString &pattern, UCalendarDateFields field)
This is for ICU internal use only.
virtual void adoptTimeZoneFormat(TimeZoneFormat *timeZoneFormatToAdopt)
Sets the TimeZoneFormat to be used by this date/time formatter.
virtual void applyLocalizedPattern(const UnicodeString &pattern, UErrorCode &status)
Apply the given localized pattern string to this date format.
virtual SimpleDateFormat * clone() const override
Clone this Format object polymorphically.
TimeZoneFormat supports time zone display name formatting and parsing.
Definition: tzfmt.h:274
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:133
UMemory is the common ICU base class.
Definition: uobject.h:115
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
C++ API: Abstract class for converting dates.
C++ API: TimeZoneFormat.
UTimeZoneFormatTimeType
Constants for time types used by TimeZoneFormat APIs for receiving time type (standard time,...
Definition: tzfmt.h:206
UCalendarDateFields
Possible fields in a UCalendar.
Definition: ucal.h:202
UDateFormatField
FieldPosition and UFieldPosition selectors for format fields defined by DateFormat and UDateFormat.
Definition: udat.h:515
C API: Display context types (enum values)
UDisplayContext
Display context settings.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
Basic definitions for ICU, for both C and C++ APIs.
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
Definition: utypes.h:188
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:301
double UDate
Date and Time data type.
Definition: utypes.h:203