ICU 63.1  63.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 
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 
53 namespace number {
54 class LocalizedNumberFormatter;
55 }
56 
724 public:
734  SimpleDateFormat(UErrorCode& status);
735 
747  SimpleDateFormat(const UnicodeString& pattern,
748  UErrorCode& status);
749 
771  SimpleDateFormat(const UnicodeString& pattern,
772  const UnicodeString& override,
773  UErrorCode& status);
774 
787  SimpleDateFormat(const UnicodeString& pattern,
788  const Locale& locale,
789  UErrorCode& status);
790 
812  SimpleDateFormat(const UnicodeString& pattern,
813  const UnicodeString& override,
814  const Locale& locale,
815  UErrorCode& status);
816 
826  SimpleDateFormat(const UnicodeString& pattern,
827  DateFormatSymbols* formatDataToAdopt,
828  UErrorCode& status);
829 
839  SimpleDateFormat(const UnicodeString& pattern,
840  const DateFormatSymbols& formatData,
841  UErrorCode& status);
842 
848 
853  SimpleDateFormat& operator=(const SimpleDateFormat&);
854 
859  virtual ~SimpleDateFormat();
860 
867  virtual Format* clone(void) const;
868 
876  virtual UBool operator==(const Format& other) const;
877 
878 
879  using DateFormat::format;
880 
897  virtual UnicodeString& format( Calendar& cal,
898  UnicodeString& appendTo,
899  FieldPosition& pos) const;
900 
919  virtual UnicodeString& format( Calendar& cal,
920  UnicodeString& appendTo,
921  FieldPositionIterator* posIter,
922  UErrorCode& status) const;
923 
924  using DateFormat::parse;
925 
952  virtual void parse( const UnicodeString& text,
953  Calendar& cal,
954  ParsePosition& pos) const;
955 
956 
973  virtual void set2DigitYearStart(UDate d, UErrorCode& status);
974 
990  UDate get2DigitYearStart(UErrorCode& status) const;
991 
998  virtual UnicodeString& toPattern(UnicodeString& result) const;
999 
1022  virtual UnicodeString& toLocalizedPattern(UnicodeString& result,
1023  UErrorCode& status) const;
1024 
1033  virtual void applyPattern(const UnicodeString& pattern);
1034 
1045  virtual void applyLocalizedPattern(const UnicodeString& pattern,
1046  UErrorCode& status);
1047 
1056  virtual const DateFormatSymbols* getDateFormatSymbols(void) const;
1057 
1064  virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols);
1065 
1071  virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols);
1072 
1084  static UClassID U_EXPORT2 getStaticClassID(void);
1085 
1097  virtual UClassID getDynamicClassID(void) const;
1098 
1108  virtual void adoptCalendar(Calendar* calendarToAdopt);
1109 
1110  /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following methods since they are virtual */
1118  virtual void adoptTimeZoneFormat(TimeZoneFormat* timeZoneFormatToAdopt);
1119 
1125  virtual void setTimeZoneFormat(const TimeZoneFormat& newTimeZoneFormat);
1126 
1132  virtual const TimeZoneFormat* getTimeZoneFormat(void) const;
1133 
1144  virtual void setContext(UDisplayContext value, UErrorCode& status);
1145 
1153  void adoptNumberFormat(NumberFormat *formatToAdopt);
1154 
1170  void adoptNumberFormat(const UnicodeString& fields, NumberFormat *formatToAdopt, UErrorCode &status);
1171 
1177  const NumberFormat * getNumberFormatForField(char16_t field) const;
1178 
1179 #ifndef U_HIDE_INTERNAL_API
1180 
1190  UBool isFieldUnitIgnored(UCalendarDateFields field) const;
1191 
1192 
1204  static UBool isFieldUnitIgnored(const UnicodeString& pattern,
1205  UCalendarDateFields field);
1206 
1215  const Locale& getSmpFmtLocale(void) const;
1216 #endif /* U_HIDE_INTERNAL_API */
1217 
1218 private:
1219  friend class DateFormat;
1220 
1221  void initializeDefaultCentury(void);
1222 
1223  void initializeBooleanAttributes(void);
1224 
1225  SimpleDateFormat(); // default constructor not implemented
1226 
1235  SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1236 
1245  SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern
1246 
1250  UnicodeString& _format(Calendar& cal, UnicodeString& appendTo, FieldPositionHandler& handler, UErrorCode& status) const;
1251 
1268  void subFormat(UnicodeString &appendTo,
1269  char16_t ch,
1270  int32_t count,
1271  UDisplayContext capitalizationContext,
1272  int32_t fieldNum,
1273  FieldPositionHandler& handler,
1274  Calendar& cal,
1275  UErrorCode& status) const; // in case of illegal argument
1276 
1290  void zeroPaddingNumber(const NumberFormat *currentNumberFormat,
1291  UnicodeString &appendTo,
1292  int32_t value,
1293  int32_t minDigits,
1294  int32_t maxDigits) const;
1295 
1300  static UBool isNumeric(char16_t formatChar, int32_t count);
1301 
1305  static UBool isAtNumericField(const UnicodeString &pattern, int32_t patternOffset);
1306 
1310  static UBool isAfterNonNumericField(const UnicodeString &pattern, int32_t patternOffset);
1311 
1319  Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
1320 
1330  void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
1331 
1338  void initialize(const Locale& locale, UErrorCode& status);
1339 
1353  int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1354  const UnicodeString* stringArray, int32_t stringArrayCount,
1355  const UnicodeString* monthPattern, Calendar& cal) const;
1356 
1369  int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
1370  const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
1371 
1375  int32_t matchDayPeriodStrings(const UnicodeString& text, int32_t start,
1376  const UnicodeString* stringArray, int32_t stringArrayCount,
1377  int32_t &dayPeriod) const;
1378 
1394  static UBool matchLiterals(const UnicodeString &pattern, int32_t &patternOffset,
1395  const UnicodeString &text, int32_t &textOffset,
1396  UBool whitespaceLenient, UBool partialMatchLenient, UBool oldLeniency);
1397 
1418  int32_t subParse(const UnicodeString& text, int32_t& start, char16_t ch, int32_t count,
1419  UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
1420  int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
1421  int32_t *dayPeriod=NULL) const;
1422 
1423  void parseInt(const UnicodeString& text,
1424  Formattable& number,
1425  ParsePosition& pos,
1426  UBool allowNegative,
1427  const NumberFormat *fmt) const;
1428 
1429  void parseInt(const UnicodeString& text,
1430  Formattable& number,
1431  int32_t maxDigits,
1432  ParsePosition& pos,
1433  UBool allowNegative,
1434  const NumberFormat *fmt) const;
1435 
1436  int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
1437  int32_t patLoc, UBool isNegative) const;
1438 
1447  int32_t countDigits(const UnicodeString& text, int32_t start, int32_t end) const;
1448 
1461  static void translatePattern(const UnicodeString& originalPattern,
1462  UnicodeString& translatedPattern,
1463  const UnicodeString& from,
1464  const UnicodeString& to,
1465  UErrorCode& status);
1466 
1474  void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
1475 
1485  int32_t compareSimpleAffix(const UnicodeString& affix,
1486  const UnicodeString& input,
1487  int32_t pos) const;
1488 
1493  int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const;
1494 
1499  int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
1500 
1504  void initFastNumberFormatters(UErrorCode& status);
1505 
1509  void freeFastNumberFormatters();
1510 
1514  void initNumberFormatters(const Locale &locale,UErrorCode &status);
1515 
1519  void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
1520 
1524  static const UCalendarDateFields fgPatternIndexToCalendarField[];
1525 
1529  static const UDateFormatField fgPatternIndexToDateFormatField[];
1530 
1534  TimeZoneFormat *tzFormat(UErrorCode &status) const;
1535 
1536  const NumberFormat* getNumberFormatByIndex(UDateFormatField index) const;
1537 
1544  static const int32_t fgCalendarFieldToLevel[];
1545 
1549  static int32_t getLevelFromChar(char16_t ch);
1550 
1554  static UBool isSyntaxChar(char16_t ch);
1555 
1559  UnicodeString fPattern;
1560 
1564  UnicodeString fDateOverride;
1565 
1569  UnicodeString fTimeOverride;
1570 
1571 
1575  Locale fLocale;
1576 
1581  DateFormatSymbols* fSymbols; // Owned
1582 
1586  TimeZoneFormat* fTimeZoneFormat;
1587 
1596  UDate fDefaultCenturyStart;
1597 
1598  UBool fHasMinute;
1599  UBool fHasSecond;
1600 
1604  void parsePattern();
1605 
1609  /*transient*/ int32_t fDefaultCenturyStartYear;
1610 
1611  struct NSOverride : public UMemory {
1612  const SharedNumberFormat *snf;
1613  int32_t hash;
1614  NSOverride *next;
1615  void free();
1616  NSOverride() : snf(NULL), hash(0), next(NULL) {
1617  }
1618  ~NSOverride();
1619  };
1620 
1625  const SharedNumberFormat **fSharedNumberFormatters;
1626 
1627  enum NumberFormatterKey {
1628  SMPDTFMT_NF_1x10,
1629  SMPDTFMT_NF_2x10,
1630  SMPDTFMT_NF_3x10,
1631  SMPDTFMT_NF_4x10,
1632  SMPDTFMT_NF_2x2,
1633  SMPDTFMT_NF_COUNT
1634  };
1635 
1639  const number::LocalizedNumberFormatter* fFastNumberFormatters[SMPDTFMT_NF_COUNT] = {};
1640 
1641  UBool fHaveDefaultCentury;
1642 
1643  BreakIterator* fCapitalizationBrkIter;
1644 };
1645 
1646 inline UDate
1647 SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const
1648 {
1649  return fDefaultCenturyStart;
1650 }
1651 
1653 
1654 #endif /* #if !UCONFIG_NO_FORMATTING */
1655 
1656 #endif // _SMPDTFMT
1657 //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:198
UTimeZoneFormatTimeType
Constants for time types used by TimeZoneFormat APIs for receiving time type (standard time...
Definition: tzfmt.h:204
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:90
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:490
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:723
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:109
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