ICU 70.1  70.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(); // 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 matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1375  const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
1376 
1380  int32_t matchDayPeriodStrings(const UnicodeString& text, int32_t start,
1381  const UnicodeString* stringArray, int32_t stringArrayCount,
1382  int32_t &dayPeriod) const;
1383 
1399  static UBool matchLiterals(const UnicodeString &pattern, int32_t &patternOffset,
1400  const UnicodeString &text, int32_t &textOffset,
1401  UBool whitespaceLenient, UBool partialMatchLenient, UBool oldLeniency);
1402 
1423  int32_t subParse(const UnicodeString& text, int32_t& start, char16_t ch, int32_t count,
1424  UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
1425  int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
1426  int32_t *dayPeriod=NULL) const;
1427 
1428  void parseInt(const UnicodeString& text,
1429  Formattable& number,
1430  ParsePosition& pos,
1431  UBool allowNegative,
1432  const NumberFormat *fmt) const;
1433 
1434  void parseInt(const UnicodeString& text,
1435  Formattable& number,
1436  int32_t maxDigits,
1437  ParsePosition& pos,
1438  UBool allowNegative,
1439  const NumberFormat *fmt) const;
1440 
1441  int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
1442  int32_t patLoc, UBool isNegative) const;
1443 
1452  int32_t countDigits(const UnicodeString& text, int32_t start, int32_t end) const;
1453 
1466  static void translatePattern(const UnicodeString& originalPattern,
1467  UnicodeString& translatedPattern,
1468  const UnicodeString& from,
1469  const UnicodeString& to,
1470  UErrorCode& status);
1471 
1479  void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
1480 
1490  int32_t compareSimpleAffix(const UnicodeString& affix,
1491  const UnicodeString& input,
1492  int32_t pos) const;
1493 
1498  int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const;
1499 
1504  int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
1505 
1509  void initFastNumberFormatters(UErrorCode& status);
1510 
1514  void freeFastNumberFormatters();
1515 
1519  void initNumberFormatters(const Locale &locale,UErrorCode &status);
1520 
1524  void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
1525 
1529  static const UCalendarDateFields fgPatternIndexToCalendarField[];
1530 
1534  static const UDateFormatField fgPatternIndexToDateFormatField[];
1535 
1539  TimeZoneFormat *tzFormat(UErrorCode &status) const;
1540 
1541  const NumberFormat* getNumberFormatByIndex(UDateFormatField index) const;
1542 
1549  static const int32_t fgCalendarFieldToLevel[];
1550 
1554  static int32_t getLevelFromChar(char16_t ch);
1555 
1559  static UBool isSyntaxChar(char16_t ch);
1560 
1564  UnicodeString fPattern;
1565 
1569  UnicodeString fDateOverride;
1570 
1574  UnicodeString fTimeOverride;
1575 
1576 
1580  Locale fLocale;
1581 
1586  DateFormatSymbols* fSymbols; // Owned
1587 
1591  TimeZoneFormat* fTimeZoneFormat;
1592 
1601  UDate fDefaultCenturyStart;
1602 
1603  UBool fHasMinute;
1604  UBool fHasSecond;
1605  UBool fHasHanYearChar; // pattern contains the Han year character \u5E74
1606 
1610  void parsePattern();
1611 
1615  /*transient*/ int32_t fDefaultCenturyStartYear;
1616 
1617  struct NSOverride : public UMemory {
1618  const SharedNumberFormat *snf;
1619  int32_t hash;
1620  NSOverride *next;
1621  void free();
1622  NSOverride() : snf(NULL), hash(0), next(NULL) {
1623  }
1624  ~NSOverride();
1625  };
1626 
1631  const SharedNumberFormat **fSharedNumberFormatters;
1632 
1633  enum NumberFormatterKey {
1634  SMPDTFMT_NF_1x10,
1635  SMPDTFMT_NF_2x10,
1636  SMPDTFMT_NF_3x10,
1637  SMPDTFMT_NF_4x10,
1638  SMPDTFMT_NF_2x2,
1639  SMPDTFMT_NF_COUNT
1640  };
1641 
1645  const number::LocalizedNumberFormatter* fFastNumberFormatters[SMPDTFMT_NF_COUNT] = {};
1646 
1647  UBool fHaveDefaultCentury;
1648 
1649  const BreakIterator* fCapitalizationBrkIter;
1650 };
1651 
1652 inline UDate
1653 SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const
1654 {
1655  return fDefaultCenturyStart;
1656 }
1657 
1658 U_NAMESPACE_END
1659 
1660 #endif /* #if !UCONFIG_NO_FORMATTING */
1661 
1662 #endif /* U_SHOW_CPLUSPLUS_API */
1663 
1664 #endif // _SMPDTFMT
1665 //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