ICU 64.2  64.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 
36 #if !UCONFIG_NO_FORMATTING
37 
38 #include "unicode/datefmt.h"
40 #include "unicode/tzfmt.h" /* for UTimeZoneFormatTimeType */
41 #include "unicode/brkiter.h"
42 
44 
45 class DateFormatSymbols;
46 class DateFormat;
47 class MessageFormat;
48 class FieldPositionHandler;
49 class TimeZoneFormat;
50 class SharedNumberFormat;
51 class SimpleDateFormatMutableNFs;
52 class DateIntervalFormat;
53 
54 namespace number {
55 class LocalizedNumberFormatter;
56 }
57 
725 public:
735  SimpleDateFormat(UErrorCode& status);
736 
748  SimpleDateFormat(const UnicodeString& pattern,
749  UErrorCode& status);
750 
772  SimpleDateFormat(const UnicodeString& pattern,
773  const UnicodeString& override,
774  UErrorCode& status);
775 
788  SimpleDateFormat(const UnicodeString& pattern,
789  const Locale& locale,
790  UErrorCode& status);
791 
813  SimpleDateFormat(const UnicodeString& pattern,
814  const UnicodeString& override,
815  const Locale& locale,
816  UErrorCode& status);
817 
827  SimpleDateFormat(const UnicodeString& pattern,
828  DateFormatSymbols* formatDataToAdopt,
829  UErrorCode& status);
830 
840  SimpleDateFormat(const UnicodeString& pattern,
841  const DateFormatSymbols& formatData,
842  UErrorCode& status);
843 
849 
854  SimpleDateFormat& operator=(const SimpleDateFormat&);
855 
860  virtual ~SimpleDateFormat();
861 
868  virtual Format* clone(void) const;
869 
877  virtual UBool operator==(const Format& other) const;
878 
879 
880  using DateFormat::format;
881 
898  virtual UnicodeString& format( Calendar& cal,
899  UnicodeString& appendTo,
900  FieldPosition& pos) const;
901 
920  virtual UnicodeString& format( Calendar& cal,
921  UnicodeString& appendTo,
922  FieldPositionIterator* posIter,
923  UErrorCode& status) const;
924 
925  using DateFormat::parse;
926 
953  virtual void parse( const UnicodeString& text,
954  Calendar& cal,
955  ParsePosition& pos) const;
956 
957 
974  virtual void set2DigitYearStart(UDate d, UErrorCode& status);
975 
991  UDate get2DigitYearStart(UErrorCode& status) const;
992 
999  virtual UnicodeString& toPattern(UnicodeString& result) const;
1000 
1023  virtual UnicodeString& toLocalizedPattern(UnicodeString& result,
1024  UErrorCode& status) const;
1025 
1034  virtual void applyPattern(const UnicodeString& pattern);
1035 
1046  virtual void applyLocalizedPattern(const UnicodeString& pattern,
1047  UErrorCode& status);
1048 
1057  virtual const DateFormatSymbols* getDateFormatSymbols(void) const;
1058 
1065  virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols);
1066 
1072  virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols);
1073 
1085  static UClassID U_EXPORT2 getStaticClassID(void);
1086 
1098  virtual UClassID getDynamicClassID(void) const;
1099 
1109  virtual void adoptCalendar(Calendar* calendarToAdopt);
1110 
1111  /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following methods since they are virtual */
1119  virtual void adoptTimeZoneFormat(TimeZoneFormat* timeZoneFormatToAdopt);
1120 
1126  virtual void setTimeZoneFormat(const TimeZoneFormat& newTimeZoneFormat);
1127 
1133  virtual const TimeZoneFormat* getTimeZoneFormat(void) const;
1134 
1145  virtual void setContext(UDisplayContext value, UErrorCode& status);
1146 
1154  void adoptNumberFormat(NumberFormat *formatToAdopt);
1155 
1171  void adoptNumberFormat(const UnicodeString& fields, NumberFormat *formatToAdopt, UErrorCode &status);
1172 
1178  const NumberFormat * getNumberFormatForField(char16_t field) const;
1179 
1180 #ifndef U_HIDE_INTERNAL_API
1181 
1191  UBool isFieldUnitIgnored(UCalendarDateFields field) const;
1192 
1193 
1205  static UBool isFieldUnitIgnored(const UnicodeString& pattern,
1206  UCalendarDateFields field);
1207 
1216  const Locale& getSmpFmtLocale(void) const;
1217 #endif /* U_HIDE_INTERNAL_API */
1218 
1219 private:
1220  friend class DateFormat;
1221  friend class DateIntervalFormat;
1222 
1223  void initializeDefaultCentury(void);
1224 
1225  void initializeBooleanAttributes(void);
1226 
1227  SimpleDateFormat(); // default constructor not implemented
1228 
1237  SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1238 
1247  SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern
1248 
1252  UnicodeString& _format(Calendar& cal, UnicodeString& appendTo, FieldPositionHandler& handler, UErrorCode& status) const;
1253 
1270  void subFormat(UnicodeString &appendTo,
1271  char16_t ch,
1272  int32_t count,
1273  UDisplayContext capitalizationContext,
1274  int32_t fieldNum,
1275  FieldPositionHandler& handler,
1276  Calendar& cal,
1277  UErrorCode& status) const; // in case of illegal argument
1278 
1292  void zeroPaddingNumber(const NumberFormat *currentNumberFormat,
1293  UnicodeString &appendTo,
1294  int32_t value,
1295  int32_t minDigits,
1296  int32_t maxDigits) const;
1297 
1302  static UBool isNumeric(char16_t formatChar, int32_t count);
1303 
1307  static UBool isAtNumericField(const UnicodeString &pattern, int32_t patternOffset);
1308 
1312  static UBool isAfterNonNumericField(const UnicodeString &pattern, int32_t patternOffset);
1313 
1321  Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
1322 
1332  void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1333 
1340  void initialize(const Locale& locale, UErrorCode& status);
1341 
1355  int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1356  const UnicodeString* stringArray, int32_t stringArrayCount,
1357  const UnicodeString* monthPattern, Calendar& cal) const;
1358 
1371  int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1372  const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
1373 
1377  int32_t matchDayPeriodStrings(const UnicodeString& text, int32_t start,
1378  const UnicodeString* stringArray, int32_t stringArrayCount,
1379  int32_t &dayPeriod) const;
1380 
1396  static UBool matchLiterals(const UnicodeString &pattern, int32_t &patternOffset,
1397  const UnicodeString &text, int32_t &textOffset,
1398  UBool whitespaceLenient, UBool partialMatchLenient, UBool oldLeniency);
1399 
1420  int32_t subParse(const UnicodeString& text, int32_t& start, char16_t ch, int32_t count,
1421  UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
1422  int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
1423  int32_t *dayPeriod=NULL) const;
1424 
1425  void parseInt(const UnicodeString& text,
1426  Formattable& number,
1427  ParsePosition& pos,
1428  UBool allowNegative,
1429  const NumberFormat *fmt) const;
1430 
1431  void parseInt(const UnicodeString& text,
1432  Formattable& number,
1433  int32_t maxDigits,
1434  ParsePosition& pos,
1435  UBool allowNegative,
1436  const NumberFormat *fmt) const;
1437 
1438  int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
1439  int32_t patLoc, UBool isNegative) const;
1440 
1449  int32_t countDigits(const UnicodeString& text, int32_t start, int32_t end) const;
1450 
1463  static void translatePattern(const UnicodeString& originalPattern,
1464  UnicodeString& translatedPattern,
1465  const UnicodeString& from,
1466  const UnicodeString& to,
1467  UErrorCode& status);
1468 
1476  void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
1477 
1487  int32_t compareSimpleAffix(const UnicodeString& affix,
1488  const UnicodeString& input,
1489  int32_t pos) const;
1490 
1495  int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const;
1496 
1501  int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
1502 
1506  void initFastNumberFormatters(UErrorCode& status);
1507 
1511  void freeFastNumberFormatters();
1512 
1516  void initNumberFormatters(const Locale &locale,UErrorCode &status);
1517 
1521  void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
1522 
1526  static const UCalendarDateFields fgPatternIndexToCalendarField[];
1527 
1531  static const UDateFormatField fgPatternIndexToDateFormatField[];
1532 
1536  TimeZoneFormat *tzFormat(UErrorCode &status) const;
1537 
1538  const NumberFormat* getNumberFormatByIndex(UDateFormatField index) const;
1539 
1546  static const int32_t fgCalendarFieldToLevel[];
1547 
1551  static int32_t getLevelFromChar(char16_t ch);
1552 
1556  static UBool isSyntaxChar(char16_t ch);
1557 
1561  UnicodeString fPattern;
1562 
1566  UnicodeString fDateOverride;
1567 
1571  UnicodeString fTimeOverride;
1572 
1573 
1577  Locale fLocale;
1578 
1583  DateFormatSymbols* fSymbols; // Owned
1584 
1588  TimeZoneFormat* fTimeZoneFormat;
1589 
1598  UDate fDefaultCenturyStart;
1599 
1600  UBool fHasMinute;
1601  UBool fHasSecond;
1602  UBool fHasHanYearChar; // pattern contains the Han year character \u5E74
1603 
1607  void parsePattern();
1608 
1612  /*transient*/ int32_t fDefaultCenturyStartYear;
1613 
1614  struct NSOverride : public UMemory {
1615  const SharedNumberFormat *snf;
1616  int32_t hash;
1617  NSOverride *next;
1618  void free();
1619  NSOverride() : snf(NULL), hash(0), next(NULL) {
1620  }
1621  ~NSOverride();
1622  };
1623 
1628  const SharedNumberFormat **fSharedNumberFormatters;
1629 
1630  enum NumberFormatterKey {
1631  SMPDTFMT_NF_1x10,
1632  SMPDTFMT_NF_2x10,
1633  SMPDTFMT_NF_3x10,
1634  SMPDTFMT_NF_4x10,
1635  SMPDTFMT_NF_2x2,
1636  SMPDTFMT_NF_COUNT
1637  };
1638 
1642  const number::LocalizedNumberFormatter* fFastNumberFormatters[SMPDTFMT_NF_COUNT] = {};
1643 
1644  UBool fHaveDefaultCentury;
1645 
1646  BreakIterator* fCapitalizationBrkIter;
1647 };
1648 
1649 inline UDate
1650 SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const
1651 {
1652  return fDefaultCenturyStart;
1653 }
1654 
1656 
1657 #endif /* #if !UCONFIG_NO_FORMATTING */
1658 
1659 #endif // _SMPDTFMT
1660 //eof
C++ API: Break Iterator.
C++ API: TimeZoneFormat.
Base class for all formats.
Definition: format.h:96
TimeZoneFormat supports time zone display name formatting and parsing.
Definition: tzfmt.h:272
UCalendarDateFields
Possible fields in a UCalendar.
Definition: ucal.h:199
UTimeZoneFormatTimeType
Constants for time types used by TimeZoneFormat APIs for receiving time type (standard time...
Definition: tzfmt.h:204
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent man...
Definition: dtitvfmt.h:298
DateFormatSymbols is a public class for encapsulating localizable date-time formatting data – includ...
Definition: dtfmtsym.h:82
Calendar is an abstract base class for converting between a UDate object and a set of integer fields ...
Definition: calendar.h:185
UDisplayContext
Display context settings.
double UDate
Date and Time data type.
Definition: utypes.h:203
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:93
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:131
C API: Display context types (enum values)
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
The BreakIterator class implements methods for finding the location of boundaries in text...
Definition: brkiter.h:102
#define NULL
Define NULL if necessary, to nullptr for C++ and to ((void *)0) for C.
Definition: utypes.h:188
EStyle
Constants for various style patterns.
Definition: datefmt.h:164
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
A NumberFormatter that has a locale associated with it; this means .format() methods are available...
UDateFormatField
FieldPosition and UFieldPosition selectors for format fields defined by DateFormat and UDateFormat...
Definition: udat.h:511
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:724
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
C++ API: Abstract class for converting dates.
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:49
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:154
Basic definitions for ICU, for both C and C++ APIs.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:289
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:62
UMemory is the common ICU base class.
Definition: uobject.h:112
int8_t UBool
The ICU boolean type.
Definition: umachine.h:225
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:192