ICU 64.2  64.2
datefmt.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  ********************************************************************************
5  * Copyright (C) 1997-2016, International Business Machines
6  * Corporation and others. All Rights Reserved.
7  ********************************************************************************
8  *
9  * File DATEFMT.H
10  *
11  * Modification History:
12  *
13  * Date Name Description
14  * 02/19/97 aliu Converted from java.
15  * 04/01/97 aliu Added support for centuries.
16  * 07/23/98 stephen JDK 1.2 sync
17  * 11/15/99 weiv Added support for week of year/day of week formatting
18  ********************************************************************************
19  */
20 
21 #ifndef DATEFMT_H
22 #define DATEFMT_H
23 
24 #include "unicode/utypes.h"
25 
26 #if !UCONFIG_NO_FORMATTING
27 
28 #include "unicode/udat.h"
29 #include "unicode/calendar.h"
30 #include "unicode/numfmt.h"
31 #include "unicode/format.h"
32 #include "unicode/locid.h"
33 #include "unicode/enumset.h"
35 
42 
43 class TimeZone;
44 class DateTimePatternGenerator;
45 
51 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN && !defined(U_IN_DOXYGEN)
52 template class U_I18N_API EnumSet<UDateFormatBooleanAttribute,
53  0,
55 #endif
56 
154 class U_I18N_API DateFormat : public Format {
155 public:
156 
164  enum EStyle
165  {
166  kNone = -1,
167 
168  kFull = 0,
169  kLong = 1,
170  kMedium = 2,
171  kShort = 3,
172 
173  kDateOffset = kShort + 1,
174  // kFull + kDateOffset = 4
175  // kLong + kDateOffset = 5
176  // kMedium + kDateOffset = 6
177  // kShort + kDateOffset = 7
178 
179  kDateTime = 8,
180  // Default DateTime
181 
182  kDateTimeOffset = kDateTime + 1,
183  // kFull + kDateTimeOffset = 9
184  // kLong + kDateTimeOffset = 10
185  // kMedium + kDateTimeOffset = 11
186  // kShort + kDateTimeOffset = 12
187 
188  // relative dates
189  kRelative = (1 << 7),
190 
191  kFullRelative = (kFull | kRelative),
192 
193  kLongRelative = kLong | kRelative,
194 
195  kMediumRelative = kMedium | kRelative,
196 
197  kShortRelative = kShort | kRelative,
198 
199 
200  kDefault = kMedium,
201 
202 
203 
208  FULL = kFull,
209  LONG = kLong,
210  MEDIUM = kMedium,
211  SHORT = kShort,
212  DEFAULT = kDefault,
213  DATE_OFFSET = kDateOffset,
214  NONE = kNone,
215  DATE_TIME = kDateTime
216  };
217 
222  virtual ~DateFormat();
223 
228  virtual UBool operator==(const Format&) const;
229 
230 
231  using Format::format;
232 
247  virtual UnicodeString& format(const Formattable& obj,
248  UnicodeString& appendTo,
249  FieldPosition& pos,
250  UErrorCode& status) const;
251 
267  virtual UnicodeString& format(const Formattable& obj,
268  UnicodeString& appendTo,
269  FieldPositionIterator* posIter,
270  UErrorCode& status) const;
304  virtual UnicodeString& format( Calendar& cal,
305  UnicodeString& appendTo,
306  FieldPosition& fieldPosition) const = 0;
307 
326  virtual UnicodeString& format(Calendar& cal,
327  UnicodeString& appendTo,
328  FieldPositionIterator* posIter,
329  UErrorCode& status) const;
357  UnicodeString& format( UDate date,
358  UnicodeString& appendTo,
359  FieldPosition& fieldPosition) const;
360 
374  UnicodeString& format(UDate date,
375  UnicodeString& appendTo,
376  FieldPositionIterator* posIter,
377  UErrorCode& status) const;
389  UnicodeString& format(UDate date, UnicodeString& appendTo) const;
390 
425  virtual UDate parse( const UnicodeString& text,
426  UErrorCode& status) const;
427 
454  virtual void parse( const UnicodeString& text,
455  Calendar& cal,
456  ParsePosition& pos) const = 0;
457 
488  UDate parse( const UnicodeString& text,
489  ParsePosition& pos) const;
490 
514  virtual void parseObject(const UnicodeString& source,
515  Formattable& result,
516  ParsePosition& parse_pos) const;
517 
525  static DateFormat* U_EXPORT2 createInstance(void);
526 
538  static DateFormat* U_EXPORT2 createTimeInstance(EStyle style = kDefault,
539  const Locale& aLocale = Locale::getDefault());
540 
556  static DateFormat* U_EXPORT2 createDateInstance(EStyle style = kDefault,
557  const Locale& aLocale = Locale::getDefault());
558 
577  static DateFormat* U_EXPORT2 createDateTimeInstance(EStyle dateStyle = kDefault,
578  EStyle timeStyle = kDefault,
579  const Locale& aLocale = Locale::getDefault());
580 
581 #ifndef U_HIDE_INTERNAL_API
582 
590  static UnicodeString getBestPattern(
591  const Locale &locale,
592  const UnicodeString &skeleton,
593  UErrorCode &status);
594 #endif /* U_HIDE_INTERNAL_API */
595 
609  static DateFormat* U_EXPORT2 createInstanceForSkeleton(
610  const UnicodeString& skeleton,
611  UErrorCode &status);
612 
626  static DateFormat* U_EXPORT2 createInstanceForSkeleton(
627  const UnicodeString& skeleton,
628  const Locale &locale,
629  UErrorCode &status);
630 
645  static DateFormat* U_EXPORT2 createInstanceForSkeleton(
646  Calendar *calendarToAdopt,
647  const UnicodeString& skeleton,
648  const Locale &locale,
649  UErrorCode &status);
650 
651 
659  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
660 
666  virtual UBool isLenient(void) const;
667 
688  virtual void setLenient(UBool lenient);
689 
690 
695  virtual UBool isCalendarLenient(void) const;
696 
697 
707  virtual void setCalendarLenient(UBool lenient);
708 
709 
718  virtual const Calendar* getCalendar(void) const;
719 
729  virtual void adoptCalendar(Calendar* calendarToAdopt);
730 
738  virtual void setCalendar(const Calendar& newCalendar);
739 
740 
747  virtual const NumberFormat* getNumberFormat(void) const;
748 
755  virtual void adoptNumberFormat(NumberFormat* formatToAdopt);
756 
762  virtual void setNumberFormat(const NumberFormat& newNumberFormat);
763 
769  virtual const TimeZone& getTimeZone(void) const;
770 
777  virtual void adoptTimeZone(TimeZone* zoneToAdopt);
778 
784  virtual void setTimeZone(const TimeZone& zone);
785 
795  virtual void setContext(UDisplayContext value, UErrorCode& status);
796 
807  virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
808 
820  virtual DateFormat& U_EXPORT2 setBooleanAttribute(UDateFormatBooleanAttribute attr,
821  UBool newvalue,
822  UErrorCode &status);
823 
833  virtual UBool U_EXPORT2 getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &status) const;
834 
835 protected:
842  DateFormat();
843 
848  DateFormat(const DateFormat&);
849 
855 
863 
871 
872 
873 private:
874 
883  static DateFormat* U_EXPORT2 create(EStyle timeStyle, EStyle dateStyle, const Locale& inLocale);
884 
885 
889  EnumSet<UDateFormatBooleanAttribute, 0, UDAT_BOOLEAN_ATTRIBUTE_COUNT> fBoolFlags;
890 
891 
892  UDisplayContext fCapitalizationContext;
893  friend class DateFmtKeyByStyle;
894 
895 public:
896 #ifndef U_HIDE_OBSOLETE_API
897 
902  enum EField
903  {
904  // Obsolete; use UDateFormatField instead
905  kEraField = UDAT_ERA_FIELD,
906  kYearField = UDAT_YEAR_FIELD,
907  kMonthField = UDAT_MONTH_FIELD,
908  kDateField = UDAT_DATE_FIELD,
909  kHourOfDay1Field = UDAT_HOUR_OF_DAY1_FIELD,
910  kHourOfDay0Field = UDAT_HOUR_OF_DAY0_FIELD,
911  kMinuteField = UDAT_MINUTE_FIELD,
912  kSecondField = UDAT_SECOND_FIELD,
913  kMillisecondField = UDAT_FRACTIONAL_SECOND_FIELD,
914  kDayOfWeekField = UDAT_DAY_OF_WEEK_FIELD,
915  kDayOfYearField = UDAT_DAY_OF_YEAR_FIELD,
916  kDayOfWeekInMonthField = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
917  kWeekOfYearField = UDAT_WEEK_OF_YEAR_FIELD,
918  kWeekOfMonthField = UDAT_WEEK_OF_MONTH_FIELD,
919  kAmPmField = UDAT_AM_PM_FIELD,
920  kHour1Field = UDAT_HOUR1_FIELD,
921  kHour0Field = UDAT_HOUR0_FIELD,
922  kTimezoneField = UDAT_TIMEZONE_FIELD,
923  kYearWOYField = UDAT_YEAR_WOY_FIELD,
924  kDOWLocalField = UDAT_DOW_LOCAL_FIELD,
925  kExtendedYearField = UDAT_EXTENDED_YEAR_FIELD,
926  kJulianDayField = UDAT_JULIAN_DAY_FIELD,
927  kMillisecondsInDayField = UDAT_MILLISECONDS_IN_DAY_FIELD,
928 
929  // Obsolete; use UDateFormatField instead
930  ERA_FIELD = UDAT_ERA_FIELD,
931  YEAR_FIELD = UDAT_YEAR_FIELD,
932  MONTH_FIELD = UDAT_MONTH_FIELD,
933  DATE_FIELD = UDAT_DATE_FIELD,
934  HOUR_OF_DAY1_FIELD = UDAT_HOUR_OF_DAY1_FIELD,
935  HOUR_OF_DAY0_FIELD = UDAT_HOUR_OF_DAY0_FIELD,
936  MINUTE_FIELD = UDAT_MINUTE_FIELD,
937  SECOND_FIELD = UDAT_SECOND_FIELD,
938  MILLISECOND_FIELD = UDAT_FRACTIONAL_SECOND_FIELD,
939  DAY_OF_WEEK_FIELD = UDAT_DAY_OF_WEEK_FIELD,
940  DAY_OF_YEAR_FIELD = UDAT_DAY_OF_YEAR_FIELD,
941  DAY_OF_WEEK_IN_MONTH_FIELD = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
942  WEEK_OF_YEAR_FIELD = UDAT_WEEK_OF_YEAR_FIELD,
943  WEEK_OF_MONTH_FIELD = UDAT_WEEK_OF_MONTH_FIELD,
944  AM_PM_FIELD = UDAT_AM_PM_FIELD,
945  HOUR1_FIELD = UDAT_HOUR1_FIELD,
946  HOUR0_FIELD = UDAT_HOUR0_FIELD,
947  TIMEZONE_FIELD = UDAT_TIMEZONE_FIELD
948  };
949 #endif /* U_HIDE_OBSOLETE_API */
950 };
951 
953 
954 #endif /* #if !UCONFIG_NO_FORMATTING */
955 
956 #endif // _DATEFMT
957 //eof
FieldPosition and UFieldPosition selector for &#39;w&#39; field alignment, corresponding to the UCAL_WEEK_OF_...
Definition: udat.h:613
Base class for all formats.
Definition: format.h:96
FieldPosition and UFieldPosition selector for &#39;e&#39; field alignment, corresponding to the UCAL_DOW_LOCA...
Definition: udat.h:667
UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
FieldPosition and UFieldPosition selector for &#39;M&#39; field alignment, corresponding to the UCAL_MONTH fi...
Definition: udat.h:531
FieldPosition and UFieldPosition selector for &#39;k&#39; field alignment, corresponding to the UCAL_HOUR_OF_...
Definition: udat.h:547
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
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:131
One more than the highest normal UDateFormatBooleanAttribute value.
Definition: udat.h:930
C API: Display context types (enum values)
FieldPosition and UFieldPosition selector for &#39;g&#39; field alignment, corresponding to the UCAL_JULIAN_D...
Definition: udat.h:681
FieldPosition and UFieldPosition selector for &#39;D&#39; field alignment, corresponding to the UCAL_DAY_OF_Y...
Definition: udat.h:599
C++ API: Base class for all formats.
FieldPosition and UFieldPosition selector for &#39;u&#39; field alignment, corresponding to the UCAL_EXTENDED...
Definition: udat.h:674
EField
Field selector for FieldPosition for DateFormat fields.
Definition: datefmt.h:902
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:301
FieldPosition and UFieldPosition selector for &#39;d&#39; field alignment, corresponding to the UCAL_DATE fie...
Definition: udat.h:538
FieldPosition and UFieldPosition selector for &#39;Y&#39; field alignment, corresponding to the UCAL_YEAR_WOY...
Definition: udat.h:660
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:137
FieldPosition and UFieldPosition selector for &#39;F&#39; field alignment, corresponding to the UCAL_DAY_OF_W...
Definition: udat.h:606
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:55
FieldPosition and UFieldPosition selector for &#39;A&#39; field alignment, corresponding to the UCAL_MILLISEC...
Definition: udat.h:688
C++ API: Calendar object.
C API: DateFormat.
Calendar * fCalendar
The calendar that DateFormat uses to produce the time field values needed to implement date/time form...
Definition: datefmt.h:862
FieldPosition and UFieldPosition selector for &#39;W&#39; field alignment, corresponding to the UCAL_WEEK_OF_...
Definition: udat.h:620
EStyle
Constants for various style patterns.
Definition: datefmt.h:164
NumberFormat * fNumberFormat
The number formatter that DateFormat uses to format numbers in dates and times.
Definition: datefmt.h:870
FieldPosition and UFieldPosition selector for &#39;E&#39; field alignment, corresponding to the UCAL_DAY_OF_W...
Definition: udat.h:592
FieldPosition and UFieldPosition selector for &#39;a&#39; field alignment, corresponding to the UCAL_AM_PM fi...
Definition: udat.h:627
FieldPosition and UFieldPosition selector for &#39;K&#39; field alignment, corresponding to the UCAL_HOUR fie...
Definition: udat.h:645
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:138
UDisplayContextType
Display context types, for getting values of a particular setting.
FieldPosition and UFieldPosition selector for &#39;G&#39; field alignment, corresponding to the UCAL_ERA fiel...
Definition: udat.h:517
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:401
FieldPosition and UFieldPosition selector for &#39;m&#39; field alignment, corresponding to the UCAL_MINUTE f...
Definition: udat.h:563
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:108
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
C++ API: Locale ID object.
virtual UBool operator==(const Format &other) const =0
Return true if the given Format objects are semantically equal.
virtual void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const =0
Parse a string to produce an object.
FieldPosition and UFieldPosition selector for &#39;s&#39; field alignment, corresponding to the UCAL_SECOND f...
Definition: udat.h:570
Basic definitions for ICU, for both C and C++ APIs.
FieldPosition and UFieldPosition selector for &#39;z&#39; field alignment, corresponding to the UCAL_ZONE_OFF...
Definition: udat.h:653
Format & operator=(const Format &)
FieldPosition and UFieldPosition selector for &#39;y&#39; field alignment, corresponding to the UCAL_YEAR fie...
Definition: udat.h:524
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
C++: internal template EnumSet<>
C++ API: Compatibility APIs for number formatting.
FieldPosition and UFieldPosition selector for &#39;H&#39; field alignment, corresponding to the UCAL_HOUR_OF_...
Definition: udat.h:556
UDateFormatBooleanAttribute
DateFormat boolean attributes.
Definition: udat.h:899
FieldPosition and UFieldPosition selector for &#39;S&#39; field alignment, corresponding to the UCAL_MILLISEC...
Definition: udat.h:585
FieldPosition and UFieldPosition selector for &#39;h&#39; field alignment, corresponding to the UCAL_HOUR fie...
Definition: udat.h:636
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