ICU 62.1  62.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 !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 
46 // explicit template instantiation. see digitlst.h
47 // (When building DLLs for Windows this is required.)
48 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
49 template class U_I18N_API EnumSet<UDateFormatBooleanAttribute,
50  0,
52 #endif
53 
150 class U_I18N_API DateFormat : public Format {
151 public:
152 
160  enum EStyle
161  {
162  kNone = -1,
163 
164  kFull = 0,
165  kLong = 1,
166  kMedium = 2,
167  kShort = 3,
168 
169  kDateOffset = kShort + 1,
170  // kFull + kDateOffset = 4
171  // kLong + kDateOffset = 5
172  // kMedium + kDateOffset = 6
173  // kShort + kDateOffset = 7
174 
175  kDateTime = 8,
176  // Default DateTime
177 
178  kDateTimeOffset = kDateTime + 1,
179  // kFull + kDateTimeOffset = 9
180  // kLong + kDateTimeOffset = 10
181  // kMedium + kDateTimeOffset = 11
182  // kShort + kDateTimeOffset = 12
183 
184  // relative dates
185  kRelative = (1 << 7),
186 
187  kFullRelative = (kFull | kRelative),
188 
189  kLongRelative = kLong | kRelative,
190 
191  kMediumRelative = kMedium | kRelative,
192 
193  kShortRelative = kShort | kRelative,
194 
195 
196  kDefault = kMedium,
197 
198 
199 
204  FULL = kFull,
205  LONG = kLong,
206  MEDIUM = kMedium,
207  SHORT = kShort,
208  DEFAULT = kDefault,
209  DATE_OFFSET = kDateOffset,
210  NONE = kNone,
211  DATE_TIME = kDateTime
212  };
213 
218  virtual ~DateFormat();
219 
224  virtual UBool operator==(const Format&) const;
225 
226 
227  using Format::format;
228 
243  virtual UnicodeString& format(const Formattable& obj,
244  UnicodeString& appendTo,
245  FieldPosition& pos,
246  UErrorCode& status) const;
247 
263  virtual UnicodeString& format(const Formattable& obj,
264  UnicodeString& appendTo,
265  FieldPositionIterator* posIter,
266  UErrorCode& status) const;
300  virtual UnicodeString& format( Calendar& cal,
301  UnicodeString& appendTo,
302  FieldPosition& fieldPosition) const = 0;
303 
322  virtual UnicodeString& format(Calendar& cal,
323  UnicodeString& appendTo,
324  FieldPositionIterator* posIter,
325  UErrorCode& status) const;
353  UnicodeString& format( UDate date,
354  UnicodeString& appendTo,
355  FieldPosition& fieldPosition) const;
356 
370  UnicodeString& format(UDate date,
371  UnicodeString& appendTo,
372  FieldPositionIterator* posIter,
373  UErrorCode& status) const;
385  UnicodeString& format(UDate date, UnicodeString& appendTo) const;
386 
421  virtual UDate parse( const UnicodeString& text,
422  UErrorCode& status) const;
423 
450  virtual void parse( const UnicodeString& text,
451  Calendar& cal,
452  ParsePosition& pos) const = 0;
453 
484  UDate parse( const UnicodeString& text,
485  ParsePosition& pos) const;
486 
510  virtual void parseObject(const UnicodeString& source,
511  Formattable& result,
512  ParsePosition& parse_pos) const;
513 
521  static DateFormat* U_EXPORT2 createInstance(void);
522 
534  static DateFormat* U_EXPORT2 createTimeInstance(EStyle style = kDefault,
535  const Locale& aLocale = Locale::getDefault());
536 
552  static DateFormat* U_EXPORT2 createDateInstance(EStyle style = kDefault,
553  const Locale& aLocale = Locale::getDefault());
554 
573  static DateFormat* U_EXPORT2 createDateTimeInstance(EStyle dateStyle = kDefault,
574  EStyle timeStyle = kDefault,
575  const Locale& aLocale = Locale::getDefault());
576 
577 #ifndef U_HIDE_INTERNAL_API
578 
586  static UnicodeString getBestPattern(
587  const Locale &locale,
588  const UnicodeString &skeleton,
589  UErrorCode &status);
590 #endif /* U_HIDE_INTERNAL_API */
591 
605  static DateFormat* U_EXPORT2 createInstanceForSkeleton(
606  const UnicodeString& skeleton,
607  UErrorCode &status);
608 
622  static DateFormat* U_EXPORT2 createInstanceForSkeleton(
623  const UnicodeString& skeleton,
624  const Locale &locale,
625  UErrorCode &status);
626 
641  static DateFormat* U_EXPORT2 createInstanceForSkeleton(
642  Calendar *calendarToAdopt,
643  const UnicodeString& skeleton,
644  const Locale &locale,
645  UErrorCode &status);
646 
647 
655  static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
656 
662  virtual UBool isLenient(void) const;
663 
684  virtual void setLenient(UBool lenient);
685 
686 
691  virtual UBool isCalendarLenient(void) const;
692 
693 
703  virtual void setCalendarLenient(UBool lenient);
704 
705 
714  virtual const Calendar* getCalendar(void) const;
715 
725  virtual void adoptCalendar(Calendar* calendarToAdopt);
726 
734  virtual void setCalendar(const Calendar& newCalendar);
735 
736 
743  virtual const NumberFormat* getNumberFormat(void) const;
744 
751  virtual void adoptNumberFormat(NumberFormat* formatToAdopt);
752 
758  virtual void setNumberFormat(const NumberFormat& newNumberFormat);
759 
765  virtual const TimeZone& getTimeZone(void) const;
766 
773  virtual void adoptTimeZone(TimeZone* zoneToAdopt);
774 
780  virtual void setTimeZone(const TimeZone& zone);
781 
791  virtual void setContext(UDisplayContext value, UErrorCode& status);
792 
803  virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
804 
816  virtual DateFormat& U_EXPORT2 setBooleanAttribute(UDateFormatBooleanAttribute attr,
817  UBool newvalue,
818  UErrorCode &status);
819 
829  virtual UBool U_EXPORT2 getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &status) const;
830 
831 protected:
838  DateFormat();
839 
844  DateFormat(const DateFormat&);
845 
851 
859 
867 
868 
869 private:
870 
879  static DateFormat* U_EXPORT2 create(EStyle timeStyle, EStyle dateStyle, const Locale& inLocale);
880 
881 
886 
887 
888  UDisplayContext fCapitalizationContext;
889  friend class DateFmtKeyByStyle;
890 
891 public:
892 #ifndef U_HIDE_OBSOLETE_API
893 
898  enum EField
899  {
900  // Obsolete; use UDateFormatField instead
901  kEraField = UDAT_ERA_FIELD,
902  kYearField = UDAT_YEAR_FIELD,
903  kMonthField = UDAT_MONTH_FIELD,
904  kDateField = UDAT_DATE_FIELD,
905  kHourOfDay1Field = UDAT_HOUR_OF_DAY1_FIELD,
906  kHourOfDay0Field = UDAT_HOUR_OF_DAY0_FIELD,
907  kMinuteField = UDAT_MINUTE_FIELD,
908  kSecondField = UDAT_SECOND_FIELD,
909  kMillisecondField = UDAT_FRACTIONAL_SECOND_FIELD,
910  kDayOfWeekField = UDAT_DAY_OF_WEEK_FIELD,
911  kDayOfYearField = UDAT_DAY_OF_YEAR_FIELD,
912  kDayOfWeekInMonthField = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
913  kWeekOfYearField = UDAT_WEEK_OF_YEAR_FIELD,
914  kWeekOfMonthField = UDAT_WEEK_OF_MONTH_FIELD,
915  kAmPmField = UDAT_AM_PM_FIELD,
916  kHour1Field = UDAT_HOUR1_FIELD,
917  kHour0Field = UDAT_HOUR0_FIELD,
918  kTimezoneField = UDAT_TIMEZONE_FIELD,
919  kYearWOYField = UDAT_YEAR_WOY_FIELD,
920  kDOWLocalField = UDAT_DOW_LOCAL_FIELD,
921  kExtendedYearField = UDAT_EXTENDED_YEAR_FIELD,
922  kJulianDayField = UDAT_JULIAN_DAY_FIELD,
923  kMillisecondsInDayField = UDAT_MILLISECONDS_IN_DAY_FIELD,
924 
925  // Obsolete; use UDateFormatField instead
926  ERA_FIELD = UDAT_ERA_FIELD,
927  YEAR_FIELD = UDAT_YEAR_FIELD,
928  MONTH_FIELD = UDAT_MONTH_FIELD,
929  DATE_FIELD = UDAT_DATE_FIELD,
930  HOUR_OF_DAY1_FIELD = UDAT_HOUR_OF_DAY1_FIELD,
931  HOUR_OF_DAY0_FIELD = UDAT_HOUR_OF_DAY0_FIELD,
932  MINUTE_FIELD = UDAT_MINUTE_FIELD,
933  SECOND_FIELD = UDAT_SECOND_FIELD,
934  MILLISECOND_FIELD = UDAT_FRACTIONAL_SECOND_FIELD,
935  DAY_OF_WEEK_FIELD = UDAT_DAY_OF_WEEK_FIELD,
936  DAY_OF_YEAR_FIELD = UDAT_DAY_OF_YEAR_FIELD,
937  DAY_OF_WEEK_IN_MONTH_FIELD = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
938  WEEK_OF_YEAR_FIELD = UDAT_WEEK_OF_YEAR_FIELD,
939  WEEK_OF_MONTH_FIELD = UDAT_WEEK_OF_MONTH_FIELD,
940  AM_PM_FIELD = UDAT_AM_PM_FIELD,
941  HOUR1_FIELD = UDAT_HOUR1_FIELD,
942  HOUR0_FIELD = UDAT_HOUR0_FIELD,
943  TIMEZONE_FIELD = UDAT_TIMEZONE_FIELD
944  };
945 #endif /* U_HIDE_OBSOLETE_API */
946 };
947 
949 
950 #endif /* #if !UCONFIG_NO_FORMATTING */
951 
952 #endif // _DATEFMT
953 //eof
FieldPosition and UFieldPosition selector for &#39;w&#39; field alignment, corresponding to the UCAL_WEEK_OF_...
Definition: udat.h:592
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:646
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:510
FieldPosition and UFieldPosition selector for &#39;k&#39; field alignment, corresponding to the UCAL_HOUR_OF_...
Definition: udat.h:526
Calendar is an abstract base class for converting between a UDate object and a set of integer fields ...
Definition: calendar.h:202
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:909
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:660
FieldPosition and UFieldPosition selector for &#39;D&#39; field alignment, corresponding to the UCAL_DAY_OF_Y...
Definition: udat.h:578
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:653
EField
Field selector for FieldPosition for DateFormat fields.
Definition: datefmt.h:898
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:360
FieldPosition and UFieldPosition selector for &#39;d&#39; field alignment, corresponding to the UCAL_DATE fie...
Definition: udat.h:517
FieldPosition and UFieldPosition selector for &#39;Y&#39; field alignment, corresponding to the UCAL_YEAR_WOY...
Definition: udat.h:639
#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:585
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:667
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:858
FieldPosition and UFieldPosition selector for &#39;W&#39; field alignment, corresponding to the UCAL_WEEK_OF_...
Definition: udat.h:599
EStyle
Constants for various style patterns.
Definition: datefmt.h:160
NumberFormat * fNumberFormat
The number formatter that DateFormat uses to format numbers in dates and times.
Definition: datefmt.h:866
FieldPosition and UFieldPosition selector for &#39;E&#39; field alignment, corresponding to the UCAL_DAY_OF_W...
Definition: udat.h:571
FieldPosition and UFieldPosition selector for &#39;a&#39; field alignment, corresponding to the UCAL_AM_PM fi...
Definition: udat.h:606
FieldPosition and UFieldPosition selector for &#39;K&#39; field alignment, corresponding to the UCAL_HOUR fie...
Definition: udat.h:624
#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:496
UErrorCode
Error code to replace exception handling, so that the code is compatible with all C++ compilers...
Definition: utypes.h:396
FieldPosition and UFieldPosition selector for &#39;m&#39; field alignment, corresponding to the UCAL_MINUTE f...
Definition: udat.h:542
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:150
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:549
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:632
Format & operator=(const Format &)
FieldPosition and UFieldPosition selector for &#39;y&#39; field alignment, corresponding to the UCAL_YEAR fie...
Definition: udat.h:503
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:286
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:535
UDateFormatBooleanAttribute
DateFormat boolean attributes.
Definition: udat.h:878
FieldPosition and UFieldPosition selector for &#39;S&#39; field alignment, corresponding to the UCAL_MILLISEC...
Definition: udat.h:564
FieldPosition and UFieldPosition selector for &#39;h&#39; field alignment, corresponding to the UCAL_HOUR fie...
Definition: udat.h:615
int8_t UBool
The ICU boolean type.
Definition: umachine.h:236
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:188