ICU 69.1  69.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:
737  SimpleDateFormat(UErrorCode& status);
738 
750  SimpleDateFormat(const UnicodeString& pattern,
751  UErrorCode& status);
752 
774  SimpleDateFormat(const UnicodeString& pattern,
775  const UnicodeString& override,
776  UErrorCode& status);
777 
790  SimpleDateFormat(const UnicodeString& pattern,
791  const Locale& locale,
792  UErrorCode& status);
793 
815  SimpleDateFormat(const UnicodeString& pattern,
816  const UnicodeString& override,
817  const Locale& locale,
818  UErrorCode& status);
819 
829  SimpleDateFormat(const UnicodeString& pattern,
830  DateFormatSymbols* formatDataToAdopt,
831  UErrorCode& status);
832 
842  SimpleDateFormat(const UnicodeString& pattern,
843  const DateFormatSymbols& formatData,
844  UErrorCode& status);
845 
851 
857 
862  virtual ~SimpleDateFormat();
863 
870  virtual SimpleDateFormat* clone() const;
871 
879  virtual UBool operator==(const Format& other) const;
880 
881 
882  using DateFormat::format;
883 
900  virtual UnicodeString& format( Calendar& cal,
901  UnicodeString& appendTo,
902  FieldPosition& pos) const;
903 
922  virtual UnicodeString& format( Calendar& cal,
923  UnicodeString& appendTo,
924  FieldPositionIterator* posIter,
925  UErrorCode& status) const;
926 
927  using DateFormat::parse;
928 
955  virtual void parse( const UnicodeString& text,
956  Calendar& cal,
957  ParsePosition& pos) const;
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 
1025  virtual UnicodeString& toLocalizedPattern(UnicodeString& result,
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;
1101 
1111  virtual void adoptCalendar(Calendar* calendarToAdopt);
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);
1148 
1156  void adoptNumberFormat(NumberFormat *formatToAdopt);
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
1183 
1193  UBool isFieldUnitIgnored(UCalendarDateFields field) const;
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.
C++ API: TimeZoneFormat.
Base class for all formats.
Definition: format.h:98
TimeZoneFormat supports time zone display name formatting and parsing.
Definition: tzfmt.h:274
UCalendarDateFields
Possible fields in a UCalendar.
Definition: ucal.h:202
UTimeZoneFormatTimeType
Constants for time types used by TimeZoneFormat APIs for receiving time type (standard time...
Definition: tzfmt.h:206
virtual DateFormat * clone() const =0
Clones this object polymorphically.
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent man...
Definition: dtitvfmt.h:300
virtual UClassID getDynamicClassID() const
ICU4C "poor man's RTTI", returns a UClassID for the actual ICU class.
DateFormatSymbols is a public class for encapsulating localizable date-time formatting data – includi...
Definition: dtfmtsym.h:84
virtual void setContext(UDisplayContext value, UErrorCode &status)
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
Calendar is an abstract base class for converting between a UDate object and a set of integer fields ...
Definition: calendar.h:189
UDisplayContext
Display context settings.
double UDate
Date and Time data type.
Definition: utypes.h:203
virtual UDate parse(const UnicodeString &text, UErrorCode &status) const
Parse a date/time string.
DateFormat & operator=(const DateFormat &)
Default assignment operator.
void * UClassID
UClassID is used to identify classes without using the compiler's RTTI.
Definition: uobject.h:96
virtual void adoptNumberFormat(NumberFormat *formatToAdopt)
Allows you to set the number formatter.
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:133
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
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format an object to produce a string.
The BreakIterator class implements methods for finding the location of boundaries in text...
Definition: brkiter.h:106
#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:166
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:515
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:726
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
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:52
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:156
Basic definitions for ICU, for both C and C++ APIs.
virtual UBool operator==(const Format &) const
Equality operator.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:295
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
virtual void adoptCalendar(Calendar *calendarToAdopt)
Set the calendar to be used by this date format.
UMemory is the common ICU base class.
Definition: uobject.h:115
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195