ICU 65.1  65.1
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 U_SHOW_CPLUSPLUS_API
27 
28 #if !UCONFIG_NO_FORMATTING
29 
30 #include "unicode/udat.h"
31 #include "unicode/calendar.h"
32 #include "unicode/numfmt.h"
33 #include "unicode/format.h"
34 #include "unicode/locid.h"
35 #include "unicode/enumset.h"
37 
43 U_NAMESPACE_BEGIN
44 
45 class TimeZone;
46 class DateTimePatternGenerator;
47 
53 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN && !defined(U_IN_DOXYGEN)
54 template class U_I18N_API EnumSet<UDateFormatBooleanAttribute,
55  0,
57 #endif
58 
156 class U_I18N_API DateFormat : public Format {
157 public:
158 
166  enum EStyle
167  {
168  kNone = -1,
169 
170  kFull = 0,
171  kLong = 1,
172  kMedium = 2,
173  kShort = 3,
174 
175  kDateOffset = kShort + 1,
176  // kFull + kDateOffset = 4
177  // kLong + kDateOffset = 5
178  // kMedium + kDateOffset = 6
179  // kShort + kDateOffset = 7
180 
181  kDateTime = 8,
182  // Default DateTime
183 
184  kDateTimeOffset = kDateTime + 1,
185  // kFull + kDateTimeOffset = 9
186  // kLong + kDateTimeOffset = 10
187  // kMedium + kDateTimeOffset = 11
188  // kShort + kDateTimeOffset = 12
189 
190  // relative dates
191  kRelative = (1 << 7),
192 
193  kFullRelative = (kFull | kRelative),
194 
195  kLongRelative = kLong | kRelative,
196 
197  kMediumRelative = kMedium | kRelative,
198 
199  kShortRelative = kShort | kRelative,
200 
201 
202  kDefault = kMedium,
203 
204 
205 
210  FULL = kFull,
211  LONG = kLong,
212  MEDIUM = kMedium,
213  SHORT = kShort,
214  DEFAULT = kDefault,
215  DATE_OFFSET = kDateOffset,
216  NONE = kNone,
217  DATE_TIME = kDateTime
218  };
219 
224  virtual ~DateFormat();
225 
232  virtual DateFormat* clone() const = 0;
233 
238  virtual UBool operator==(const Format&) const;
239 
240 
241  using Format::format;
242 
257  virtual UnicodeString& format(const Formattable& obj,
258  UnicodeString& appendTo,
259  FieldPosition& pos,
260  UErrorCode& status) const;
261 
277  virtual UnicodeString& format(const Formattable& obj,
278  UnicodeString& appendTo,
279  FieldPositionIterator* posIter,
280  UErrorCode& status) const;
314  virtual UnicodeString& format( Calendar& cal,
315  UnicodeString& appendTo,
316  FieldPosition& fieldPosition) const = 0;
317 
336  virtual UnicodeString& format(Calendar& cal,
337  UnicodeString& appendTo,
338  FieldPositionIterator* posIter,
339  UErrorCode& status) const;
367  UnicodeString& format( UDate date,
368  UnicodeString& appendTo,
369  FieldPosition& fieldPosition) const;
370 
384  UnicodeString& format(UDate date,
385  UnicodeString& appendTo,
386  FieldPositionIterator* posIter,
387  UErrorCode& status) const;
399  UnicodeString& format(UDate date, UnicodeString& appendTo) const;
400 
435  virtual UDate parse( const UnicodeString& text,
436  UErrorCode& status) const;
437 
464  virtual void parse( const UnicodeString& text,
465  Calendar& cal,
466  ParsePosition& pos) const = 0;
467 
498  UDate parse( const UnicodeString& text,
499  ParsePosition& pos) const;
500 
524  virtual void parseObject(const UnicodeString& source,
525  Formattable& result,
526  ParsePosition& parse_pos) const;
527 
535  static DateFormat* U_EXPORT2 createInstance(void);
536 
548  static DateFormat* U_EXPORT2 createTimeInstance(EStyle style = kDefault,
549  const Locale& aLocale = Locale::getDefault());
550 
566  static DateFormat* U_EXPORT2 createDateInstance(EStyle style = kDefault,
567  const Locale& aLocale = Locale::getDefault());
568 
587  static DateFormat* U_EXPORT2 createDateTimeInstance(EStyle dateStyle = kDefault,
588  EStyle timeStyle = kDefault,
589  const Locale& aLocale = Locale::getDefault());
590 
591 #ifndef U_HIDE_INTERNAL_API
592 
600  static UnicodeString getBestPattern(
601  const Locale &locale,
602  const UnicodeString &skeleton,
603  UErrorCode &status);
604 #endif /* U_HIDE_INTERNAL_API */
605 
619  static DateFormat* U_EXPORT2 createInstanceForSkeleton(
620  const UnicodeString& skeleton,
621  UErrorCode &status);
622 
636  static DateFormat* U_EXPORT2 createInstanceForSkeleton(
637  const UnicodeString& skeleton,
638  const Locale &locale,
639  UErrorCode &status);
640 
655  static DateFormat* U_EXPORT2 createInstanceForSkeleton(
656  Calendar *calendarToAdopt,
657  const UnicodeString& skeleton,
658  const Locale &locale,
659  UErrorCode &status);
660 
661 
669  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
670 
676  virtual UBool isLenient(void) const;
677 
698  virtual void setLenient(UBool lenient);
699 
700 
705  virtual UBool isCalendarLenient(void) const;
706 
707 
717  virtual void setCalendarLenient(UBool lenient);
718 
719 
728  virtual const Calendar* getCalendar(void) const;
729 
739  virtual void adoptCalendar(Calendar* calendarToAdopt);
740 
748  virtual void setCalendar(const Calendar& newCalendar);
749 
750 
757  virtual const NumberFormat* getNumberFormat(void) const;
758 
765  virtual void adoptNumberFormat(NumberFormat* formatToAdopt);
766 
772  virtual void setNumberFormat(const NumberFormat& newNumberFormat);
773 
779  virtual const TimeZone& getTimeZone(void) const;
780 
787  virtual void adoptTimeZone(TimeZone* zoneToAdopt);
788 
794  virtual void setTimeZone(const TimeZone& zone);
795 
805  virtual void setContext(UDisplayContext value, UErrorCode& status);
806 
817  virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
818 
830  virtual DateFormat& U_EXPORT2 setBooleanAttribute(UDateFormatBooleanAttribute attr,
831  UBool newvalue,
832  UErrorCode &status);
833 
843  virtual UBool U_EXPORT2 getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &status) const;
844 
845 protected:
852  DateFormat();
853 
858  DateFormat(const DateFormat&);
859 
865 
873 
881 
882 
883 private:
884 
893  static DateFormat* U_EXPORT2 create(EStyle timeStyle, EStyle dateStyle, const Locale& inLocale);
894 
895 
899  EnumSet<UDateFormatBooleanAttribute, 0, UDAT_BOOLEAN_ATTRIBUTE_COUNT> fBoolFlags;
900 
901 
902  UDisplayContext fCapitalizationContext;
903  friend class DateFmtKeyByStyle;
904 
905 public:
906 #ifndef U_HIDE_OBSOLETE_API
907 
912  enum EField
913  {
914  // Obsolete; use UDateFormatField instead
915  kEraField = UDAT_ERA_FIELD,
916  kYearField = UDAT_YEAR_FIELD,
917  kMonthField = UDAT_MONTH_FIELD,
918  kDateField = UDAT_DATE_FIELD,
919  kHourOfDay1Field = UDAT_HOUR_OF_DAY1_FIELD,
920  kHourOfDay0Field = UDAT_HOUR_OF_DAY0_FIELD,
921  kMinuteField = UDAT_MINUTE_FIELD,
922  kSecondField = UDAT_SECOND_FIELD,
923  kMillisecondField = UDAT_FRACTIONAL_SECOND_FIELD,
924  kDayOfWeekField = UDAT_DAY_OF_WEEK_FIELD,
925  kDayOfYearField = UDAT_DAY_OF_YEAR_FIELD,
926  kDayOfWeekInMonthField = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
927  kWeekOfYearField = UDAT_WEEK_OF_YEAR_FIELD,
928  kWeekOfMonthField = UDAT_WEEK_OF_MONTH_FIELD,
929  kAmPmField = UDAT_AM_PM_FIELD,
930  kHour1Field = UDAT_HOUR1_FIELD,
931  kHour0Field = UDAT_HOUR0_FIELD,
932  kTimezoneField = UDAT_TIMEZONE_FIELD,
933  kYearWOYField = UDAT_YEAR_WOY_FIELD,
934  kDOWLocalField = UDAT_DOW_LOCAL_FIELD,
935  kExtendedYearField = UDAT_EXTENDED_YEAR_FIELD,
936  kJulianDayField = UDAT_JULIAN_DAY_FIELD,
937  kMillisecondsInDayField = UDAT_MILLISECONDS_IN_DAY_FIELD,
938 
939  // Obsolete; use UDateFormatField instead
940  ERA_FIELD = UDAT_ERA_FIELD,
941  YEAR_FIELD = UDAT_YEAR_FIELD,
942  MONTH_FIELD = UDAT_MONTH_FIELD,
943  DATE_FIELD = UDAT_DATE_FIELD,
944  HOUR_OF_DAY1_FIELD = UDAT_HOUR_OF_DAY1_FIELD,
945  HOUR_OF_DAY0_FIELD = UDAT_HOUR_OF_DAY0_FIELD,
946  MINUTE_FIELD = UDAT_MINUTE_FIELD,
947  SECOND_FIELD = UDAT_SECOND_FIELD,
948  MILLISECOND_FIELD = UDAT_FRACTIONAL_SECOND_FIELD,
949  DAY_OF_WEEK_FIELD = UDAT_DAY_OF_WEEK_FIELD,
950  DAY_OF_YEAR_FIELD = UDAT_DAY_OF_YEAR_FIELD,
951  DAY_OF_WEEK_IN_MONTH_FIELD = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
952  WEEK_OF_YEAR_FIELD = UDAT_WEEK_OF_YEAR_FIELD,
953  WEEK_OF_MONTH_FIELD = UDAT_WEEK_OF_MONTH_FIELD,
954  AM_PM_FIELD = UDAT_AM_PM_FIELD,
955  HOUR1_FIELD = UDAT_HOUR1_FIELD,
956  HOUR0_FIELD = UDAT_HOUR0_FIELD,
957  TIMEZONE_FIELD = UDAT_TIMEZONE_FIELD
958  };
959 #endif /* U_HIDE_OBSOLETE_API */
960 };
961 
962 U_NAMESPACE_END
963 
964 #endif /* #if !UCONFIG_NO_FORMATTING */
965 
966 #endif /* U_SHOW_CPLUSPLUS_API */
967 
968 #endif // _DATEFMT
969 //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:98
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:187
UDisplayContext
Display context settings.
double UDate
Date and Time data type.
Definition: utypes.h:203
virtual Format * clone() const =0
Clone this object polymorphically.
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:133
One more than the highest normal UDateFormatBooleanAttribute value.
Definition: udat.h:931
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:912
#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
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:58
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:872
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:166
NumberFormat * fNumberFormat
The number formatter that DateFormat uses to format numbers in dates and times.
Definition: datefmt.h:880
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
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
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:415
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:110
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
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:294
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:64
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:261
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195