ICU 4.2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
smpdtfmt.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 1997-2009, International Business Machines Corporation and others. All Rights Reserved.
3 *******************************************************************************
4 *
5 * File SMPDTFMT.H
6 *
7 * Modification History:
8 *
9 * Date Name Description
10 * 02/19/97 aliu Converted from java.
11 * 07/09/97 helena Make ParsePosition into a class.
12 * 07/21/98 stephen Added GMT_PLUS, GMT_MINUS
13 * Changed setTwoDigitStartDate to set2DigitYearStart
14 * Changed getTwoDigitStartDate to get2DigitYearStart
15 * Removed subParseLong
16 * Removed getZoneIndex (added in DateFormatSymbols)
17 * 06/14/99 stephen Removed fgTimeZoneDataSuffix
18 * 10/14/99 aliu Updated class doc to describe 2-digit year parsing
19 * {j28 4182066}.
20 *******************************************************************************
21 */
22 
23 #ifndef SMPDTFMT_H
24 #define SMPDTFMT_H
25 
26 #include "unicode/utypes.h"
27 
33 #if !UCONFIG_NO_FORMATTING
34 
35 #include "unicode/datefmt.h"
36 
38 
39 class DateFormatSymbols;
40 class DateFormat;
41 class MessageFormat;
42 
214 public:
224  SimpleDateFormat(UErrorCode& status);
225 
237  SimpleDateFormat(const UnicodeString& pattern,
238  UErrorCode& status);
239 
261  SimpleDateFormat(const UnicodeString& pattern,
262  const UnicodeString& override,
263  UErrorCode& status);
264 
277  SimpleDateFormat(const UnicodeString& pattern,
278  const Locale& locale,
279  UErrorCode& status);
280 
302  SimpleDateFormat(const UnicodeString& pattern,
303  const UnicodeString& override,
304  const Locale& locale,
305  UErrorCode& status);
306 
316  SimpleDateFormat(const UnicodeString& pattern,
317  DateFormatSymbols* formatDataToAdopt,
318  UErrorCode& status);
319 
329  SimpleDateFormat(const UnicodeString& pattern,
330  const DateFormatSymbols& formatData,
331  UErrorCode& status);
332 
338 
344 
349  virtual ~SimpleDateFormat();
350 
357  virtual Format* clone(void) const;
358 
366  virtual UBool operator==(const Format& other) const;
367 
384  virtual UnicodeString& format( Calendar& cal,
385  UnicodeString& appendTo,
386  FieldPosition& pos) const;
387 
407  virtual UnicodeString& format( const Formattable& obj,
408  UnicodeString& appendTo,
409  FieldPosition& pos,
410  UErrorCode& status) const;
411 
422  UnicodeString& format(UDate date,
423  UnicodeString& appendTo,
424  FieldPosition& fieldPosition) const;
425 
435  UnicodeString& format(const Formattable& obj,
436  UnicodeString& appendTo,
437  UErrorCode& status) const;
438 
447  UnicodeString& format(UDate date, UnicodeString& appendTo) const;
448 
468  virtual void parse( const UnicodeString& text,
469  Calendar& cal,
470  ParsePosition& pos) const;
471 
491  UDate parse( const UnicodeString& text,
492  ParsePosition& pos) const;
493 
494 
510  virtual UDate parse( const UnicodeString& text,
511  UErrorCode& status) const;
512 
529  virtual void set2DigitYearStart(UDate d, UErrorCode& status);
530 
546  UDate get2DigitYearStart(UErrorCode& status) const;
547 
554  virtual UnicodeString& toPattern(UnicodeString& result) const;
555 
572  virtual UnicodeString& toLocalizedPattern(UnicodeString& result,
573  UErrorCode& status) const;
574 
583  virtual void applyPattern(const UnicodeString& pattern);
584 
595  virtual void applyLocalizedPattern(const UnicodeString& pattern,
596  UErrorCode& status);
597 
606  virtual const DateFormatSymbols* getDateFormatSymbols(void) const;
607 
614  virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols);
615 
621  virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols);
622 
634  static UClassID U_EXPORT2 getStaticClassID(void);
635 
647  virtual UClassID getDynamicClassID(void) const;
648 
658  virtual void adoptCalendar(Calendar* calendarToAdopt);
659 
670  UBool isFieldUnitIgnored(UCalendarDateFields field) const;
671 
672 
684  static UBool isFieldUnitIgnored(const UnicodeString& pattern,
685  UCalendarDateFields field);
686 
687 
688 
697  const Locale& getSmpFmtLocale(void) const;
698 
699 
700 private:
701  friend class DateFormat;
702 
703  void initializeDefaultCentury(void);
704 
705  SimpleDateFormat(); // default constructor not implemented
706 
715  SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
716 
725  SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern
726 
743  void subFormat( UnicodeString &appendTo,
744  UChar ch,
745  int32_t count,
746  FieldPosition& pos,
747  Calendar& cal,
748  UErrorCode& status) const; // in case of illegal argument
749 
762  void zeroPaddingNumber( NumberFormat *currentNumberFormat,
763  UnicodeString &appendTo,
764  int32_t value,
765  int32_t minDigits,
766  int32_t maxDigits) const;
767 
772  static UBool isNumeric(UChar formatChar, int32_t count);
773 
781  Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
782 
789  void initializeSymbols(const Locale& locale, Calendar* calendar, UErrorCode& status);
790 
800  void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
801 
808  void initialize(const Locale& locale, UErrorCode& status);
809 
822  int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
823  const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
824 
837  int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
838  const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
839 
854  int32_t subParse(const UnicodeString& text, int32_t& start, UChar ch, int32_t count,
855  UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], Calendar& cal,
856  int32_t patLoc) const;
857 
858  void parseInt(const UnicodeString& text,
859  Formattable& number,
860  ParsePosition& pos,
861  UBool allowNegative,
862  NumberFormat *fmt) const;
863 
864  void parseInt(const UnicodeString& text,
865  Formattable& number,
866  int32_t maxDigits,
867  ParsePosition& pos,
868  UBool allowNegative,
869  NumberFormat *fmt) const;
870 
871  int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
872  int32_t patLoc, UBool isNegative) const;
873 
886  static void translatePattern(const UnicodeString& originalPattern,
887  UnicodeString& translatedPattern,
888  const UnicodeString& from,
889  const UnicodeString& to,
890  UErrorCode& status);
891 
899  void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
900 
910  int32_t compareSimpleAffix(const UnicodeString& affix,
911  const UnicodeString& input,
912  int32_t pos) const;
913 
918  int32_t skipRuleWhiteSpace(const UnicodeString& text, int32_t pos) const;
919 
924  int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
925 
929  void appendGMT(NumberFormat *currentNumberFormat,UnicodeString &appendTo, Calendar& cal, UErrorCode& status) const;
930  void formatGMTDefault(NumberFormat *currentNumberFormat,UnicodeString &appendTo, int32_t offset) const;
931  int32_t parseGMT(const UnicodeString &text, ParsePosition &pos) const;
932  int32_t parseGMTDefault(const UnicodeString &text, ParsePosition &pos) const;
933  UBool isDefaultGMTFormat() const;
934 
935  void formatRFC822TZ(UnicodeString &appendTo, int32_t offset) const;
936 
940  void initGMTFormatters(UErrorCode &status);
941 
945  void initNumberFormatters(const Locale &locale,UErrorCode &status);
946 
950  using DateFormat::getNumberFormat; // Do not hide visibility of base class function
952 
956  void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
957 
961  static const UCalendarDateFields fgPatternIndexToCalendarField[];
962 
966  static const UDateFormatField fgPatternIndexToDateFormatField[];
967 
974  static const int32_t fgCalendarFieldToLevel[];
975  static const int32_t fgPatternCharToLevel[];
976 
980  UnicodeString fPattern;
981 
985  UnicodeString fDateOverride;
986 
990  UnicodeString fTimeOverride;
991 
992 
996  Locale fLocale;
997 
1002  DateFormatSymbols* fSymbols; // Owned
1003 
1012  UDate fDefaultCenturyStart;
1013 
1017  /*transient*/ int32_t fDefaultCenturyStartYear;
1018 
1019  enum ParsedTZType {
1020  TZTYPE_UNK,
1021  TZTYPE_STD,
1022  TZTYPE_DST
1023  };
1024 
1025  ParsedTZType tztype; // here to avoid api change
1026 
1027  typedef struct NSOverride {
1028  NumberFormat *nf;
1029  int32_t hash;
1030  NSOverride *next;
1031  } NSOverride;
1032 
1033  /*
1034  * MessageFormat instances used for localized GMT format
1035  */
1036  enum {
1037  kGMTNegativeHMS = 0,
1038  kGMTNegativeHM,
1039  kGMTPositiveHMS,
1040  kGMTPositiveHM,
1041 
1042  kNumGMTFormatters
1043  };
1044  enum {
1045  kGMTNegativeHMSMinLenIdx = 0,
1046  kGMTPositiveHMSMinLenIdx,
1047 
1048  kNumGMTFormatMinLengths
1049  };
1050 
1051  MessageFormat **fGMTFormatters;
1052  // If a GMT hour format has a second field, we need to make sure
1053  // the length of input localized GMT string must match the expected
1054  // length. Otherwise, sub DateForamt handling offset format may
1055  // unexpectedly success parsing input GMT string without second field.
1056  // See #6880 about this issue.
1057  // TODO: SimpleDateFormat should provide an option to invalidate
1058  //
1059  int32_t fGMTFormatHmsMinLen[kNumGMTFormatMinLengths];
1060 
1061  NumberFormat **fNumberFormatters;
1062 
1063  NSOverride *fOverrideList;
1064 
1065  UBool fHaveDefaultCentury;
1066 };
1067 
1068 inline UDate
1070 {
1071  return fDefaultCenturyStart;
1072 }
1073 
1074 inline UnicodeString&
1076  UnicodeString& appendTo,
1077  UErrorCode& status) const {
1078  // Don't use Format:: - use immediate base class only,
1079  // in case immediate base modifies behavior later.
1080  return DateFormat::format(obj, appendTo, status);
1081 }
1082 
1083 inline UnicodeString&
1085  UnicodeString& appendTo,
1086  FieldPosition& fieldPosition) const {
1087  // Don't use Format:: - use immediate base class only,
1088  // in case immediate base modifies behavior later.
1089  return DateFormat::format(date, appendTo, fieldPosition);
1090 }
1091 
1092 inline UnicodeString&
1094  return DateFormat::format(date, appendTo);
1095 }
1096 
1098 
1099 #endif /* #if !UCONFIG_NO_FORMATTING */
1100 
1101 #endif // _SMPDTFMT
1102 //eof
virtual UClassID getDynamicClassID() const =0
Returns a unique class ID POLYMORPHICALLY.
UCalendarDateFields
Possible fields in a UCalendar.
Definition: ucal.h:176
Calendar is an abstract base class for converting between a UDate object and a set of integer fields ...
Definition: calendar.h:165
virtual void adoptCalendar(Calendar *calendarToAdopt)
Set the calendar to be used by this date format.
SimpleDateFormat is a concrete class for formatting and parsing dates in a language-independent manne...
Definition: smpdtfmt.h:213
double UDate
Date and Time data type.
Definition: utypes.h:281
virtual UBool operator==(const Format &) const
Equality operator.
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:138
virtual UnicodeString & format(Calendar &cal, UnicodeString &appendTo, FieldPosition &pos) const
Format a date or time, which is the standard millis since 24:00 GMT, Jan 1, 1970. ...
EStyle
Constants for various style patterns.
Definition: datefmt.h:148
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:475
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:183
Abstract base class for all number formats.
Definition: numfmt.h:162
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:47
DateFormat & operator=(const DateFormat &)
Default assignment operator.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:187
#define U_EXPORT2
Definition: platform.h:338
virtual UDate parse(const UnicodeString &text, UErrorCode &status) const
Parse a date/time string.
virtual Format * clone() const =0
Clone this object polymorphically.
virtual const NumberFormat * getNumberFormat(void) const
Gets the number formatter which this date/time formatter uses to format and parse the numeric portion...
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:106
uint16_t UChar
Define UChar to be wchar_t if that is 16 bits wide; always assumed to be unsigned.
Definition: umachine.h:299
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API If the compiler doesn't support namespaces...
Definition: uversion.h:184
signed char int8_t
Define 64 bit limits.
Definition: pwin32.h:127
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:47
UDateFormatField
FieldPosition and UFieldPosition selectors for format fields defined by DateFormat and UDateFormat...
Definition: udat.h:259
void * UClassID
UClassID is used to identify classes without using RTTI, since RTTI is not yet supported by all C++ c...
Definition: utypes.h:339
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:593
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:122
C++ API: Abstract class for converting dates.
Base class for all formats.
Definition: format.h:93
UDate get2DigitYearStart(UErrorCode &status) const
Get the start UDate used to interpret two-digit year strings.
Definition: smpdtfmt.h:1069
Basic definitions for ICU, for both C and C++ APIs.
virtual UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const
Format an object to produce a string.
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:181
DateFormatSymbols is a public class for encapsulating localizable date-time formatting data – includi...
Definition: dtfmtsym.h:80
signed int int32_t
Define 64 bit limits.
Definition: pwin32.h:143
A MessageFormat produces concatenated messages in a language-neutral way.
Definition: msgfmt.h:306
int8_t UBool
The ICU boolean type.
Definition: umachine.h:208