ICU 78.1  78.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 
145 public:
146 
154  enum EStyle
155  {
156  kNone = -1,
157 
158  kFull = 0,
159  kLong = 1,
160  kMedium = 2,
161  kShort = 3,
162 
163  kDateOffset = kShort + 1,
164  // kFull + kDateOffset = 4
165  // kLong + kDateOffset = 5
166  // kMedium + kDateOffset = 6
167  // kShort + kDateOffset = 7
168 
169  kDateTime = 8,
170  // Default DateTime
171 
172  kDateTimeOffset = kDateTime + 1,
173  // kFull + kDateTimeOffset = 9
174  // kLong + kDateTimeOffset = 10
175  // kMedium + kDateTimeOffset = 11
176  // kShort + kDateTimeOffset = 12
177 
178  // relative dates
179  kRelative = (1 << 7),
180 
181  kFullRelative = (kFull | kRelative),
182 
183  kLongRelative = kLong | kRelative,
184 
185  kMediumRelative = kMedium | kRelative,
186 
187  kShortRelative = kShort | kRelative,
188 
189 
190  kDefault = kMedium,
191 
192 
193 
198  FULL = kFull,
199  LONG = kLong,
200  MEDIUM = kMedium,
201  SHORT = kShort,
202  DEFAULT = kDefault,
203  DATE_OFFSET = kDateOffset,
204  NONE = kNone,
205  DATE_TIME = kDateTime
206  };
207 
213 
220  U_I18N_API virtual DateFormat* clone() const override = 0;
221 
226  U_I18N_API virtual bool operator==(const Format&) const override;
227 
228 
229  using Format::format;
230 
246  UnicodeString& appendTo,
247  FieldPosition& pos,
248  UErrorCode& status) const override;
249 
266  UnicodeString& appendTo,
267  FieldPositionIterator* posIter,
268  UErrorCode& status) const override;
303  UnicodeString& appendTo,
304  FieldPosition& fieldPosition) const = 0;
305 
325  UnicodeString& appendTo,
326  FieldPositionIterator* posIter,
327  UErrorCode& status) const;
356  UnicodeString& appendTo,
357  FieldPosition& fieldPosition) const;
358 
373  UnicodeString& appendTo,
374  FieldPositionIterator* posIter,
375  UErrorCode& status) const;
388 
423  U_I18N_API virtual UDate parse(const UnicodeString& text, UErrorCode& status) const;
424 
451  U_I18N_API virtual void parse(const UnicodeString& text,
452  Calendar& cal,
453  ParsePosition& pos) const = 0;
454 
486 
510  U_I18N_API virtual void parseObject(const UnicodeString& source,
511  Formattable& result,
512  ParsePosition& parse_pos) const override;
513 
521  U_I18N_API static DateFormat* U_EXPORT2 createInstance();
522 
534  U_I18N_API static DateFormat* U_EXPORT2
535  createTimeInstance(EStyle style = kDefault, const Locale& aLocale = Locale::getDefault());
536 
552  U_I18N_API static DateFormat* U_EXPORT2
553  createDateInstance(EStyle style = kDefault, const Locale& aLocale = Locale::getDefault());
554 
573  U_I18N_API static DateFormat* U_EXPORT2
574  createDateTimeInstance(EStyle dateStyle = kDefault,
575  EStyle timeStyle = kDefault,
576  const Locale& aLocale = Locale::getDefault());
577 
578 #ifndef U_HIDE_INTERNAL_API
588  const UnicodeString& skeleton,
589  UErrorCode& status);
590 #endif /* U_HIDE_INTERNAL_API */
591 
606  UErrorCode& status);
607 
622  const Locale& locale,
623  UErrorCode& status);
624 
639  U_I18N_API static DateFormat* U_EXPORT2 createInstanceForSkeleton(Calendar* calendarToAdopt,
640  const UnicodeString& skeleton,
641  const Locale& locale,
642  UErrorCode& status);
643 
651  U_I18N_API static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
652 
658  U_I18N_API virtual UBool isLenient() const;
659 
680  U_I18N_API virtual void setLenient(UBool lenient);
681 
682 
688 
698  U_I18N_API virtual void setCalendarLenient(UBool lenient);
699 
700 
709  U_I18N_API virtual const Calendar* getCalendar() const;
710 
720  U_I18N_API virtual void adoptCalendar(Calendar* calendarToAdopt);
721 
729  U_I18N_API virtual void setCalendar(const Calendar& newCalendar);
730 
737  U_I18N_API virtual const NumberFormat* getNumberFormat() const;
738 
745  U_I18N_API virtual void adoptNumberFormat(NumberFormat* formatToAdopt);
746 
752  U_I18N_API virtual void setNumberFormat(const NumberFormat& newNumberFormat);
753 
759  U_I18N_API virtual const TimeZone& getTimeZone() const;
760 
767  U_I18N_API virtual void adoptTimeZone(TimeZone* zoneToAdopt);
768 
774  U_I18N_API virtual void setTimeZone(const TimeZone& zone);
775 
785  U_I18N_API virtual void setContext(UDisplayContext value, UErrorCode& status);
786 
798 
810  UBool newvalue, UErrorCode& status);
811 
822  UErrorCode& status) const;
823 
824 protected:
832 
838 
844 
852 
860 
861 
862 private:
863 
872  static DateFormat* U_EXPORT2 create(EStyle timeStyle, EStyle dateStyle, const Locale& inLocale);
873 
874 
878  EnumSet<UDateFormatBooleanAttribute, 0, UDAT_BOOLEAN_ATTRIBUTE_COUNT> fBoolFlags;
879 
880 
881  UDisplayContext fCapitalizationContext;
882  friend class DateFmtKeyByStyle;
883 
884 public:
885 #ifndef U_HIDE_OBSOLETE_API
891  enum EField
892  {
893  // Obsolete; use UDateFormatField instead
894  kEraField = UDAT_ERA_FIELD,
895  kYearField = UDAT_YEAR_FIELD,
896  kMonthField = UDAT_MONTH_FIELD,
897  kDateField = UDAT_DATE_FIELD,
898  kHourOfDay1Field = UDAT_HOUR_OF_DAY1_FIELD,
899  kHourOfDay0Field = UDAT_HOUR_OF_DAY0_FIELD,
900  kMinuteField = UDAT_MINUTE_FIELD,
901  kSecondField = UDAT_SECOND_FIELD,
902  kMillisecondField = UDAT_FRACTIONAL_SECOND_FIELD,
903  kDayOfWeekField = UDAT_DAY_OF_WEEK_FIELD,
904  kDayOfYearField = UDAT_DAY_OF_YEAR_FIELD,
905  kDayOfWeekInMonthField = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
906  kWeekOfYearField = UDAT_WEEK_OF_YEAR_FIELD,
907  kWeekOfMonthField = UDAT_WEEK_OF_MONTH_FIELD,
908  kAmPmField = UDAT_AM_PM_FIELD,
909  kHour1Field = UDAT_HOUR1_FIELD,
910  kHour0Field = UDAT_HOUR0_FIELD,
911  kTimezoneField = UDAT_TIMEZONE_FIELD,
912  kYearWOYField = UDAT_YEAR_WOY_FIELD,
913  kDOWLocalField = UDAT_DOW_LOCAL_FIELD,
914  kExtendedYearField = UDAT_EXTENDED_YEAR_FIELD,
915  kJulianDayField = UDAT_JULIAN_DAY_FIELD,
916  kMillisecondsInDayField = UDAT_MILLISECONDS_IN_DAY_FIELD,
917 
918  // Obsolete; use UDateFormatField instead
919  ERA_FIELD = UDAT_ERA_FIELD,
920  YEAR_FIELD = UDAT_YEAR_FIELD,
921  MONTH_FIELD = UDAT_MONTH_FIELD,
922  DATE_FIELD = UDAT_DATE_FIELD,
923  HOUR_OF_DAY1_FIELD = UDAT_HOUR_OF_DAY1_FIELD,
924  HOUR_OF_DAY0_FIELD = UDAT_HOUR_OF_DAY0_FIELD,
925  MINUTE_FIELD = UDAT_MINUTE_FIELD,
926  SECOND_FIELD = UDAT_SECOND_FIELD,
927  MILLISECOND_FIELD = UDAT_FRACTIONAL_SECOND_FIELD,
928  DAY_OF_WEEK_FIELD = UDAT_DAY_OF_WEEK_FIELD,
929  DAY_OF_YEAR_FIELD = UDAT_DAY_OF_YEAR_FIELD,
930  DAY_OF_WEEK_IN_MONTH_FIELD = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
931  WEEK_OF_YEAR_FIELD = UDAT_WEEK_OF_YEAR_FIELD,
932  WEEK_OF_MONTH_FIELD = UDAT_WEEK_OF_MONTH_FIELD,
933  AM_PM_FIELD = UDAT_AM_PM_FIELD,
934  HOUR1_FIELD = UDAT_HOUR1_FIELD,
935  HOUR0_FIELD = UDAT_HOUR0_FIELD,
936  TIMEZONE_FIELD = UDAT_TIMEZONE_FIELD
937  };
938 #endif /* U_HIDE_OBSOLETE_API */
939 };
940 
941 U_NAMESPACE_END
942 
943 #endif /* #if !UCONFIG_NO_FORMATTING */
944 
945 #endif /* U_SHOW_CPLUSPLUS_API */
946 
947 #endif // _DATEFMT
948 //eof
C++ API: Calendar object.
Calendar is an abstract base class for converting between a UDate object and a set of integer fields ...
Definition: calendar.h:189
DateFormat is an abstract class for a family of classes that convert dates and times from their inter...
Definition: datefmt.h:144
static U_I18N_API DateFormat * createInstanceForSkeleton(Calendar *calendarToAdopt, const UnicodeString &skeleton, const Locale &locale, UErrorCode &status)
Creates a date/time formatter for the given skeleton and locale.
virtual U_I18N_API void setCalendar(const Calendar &newCalendar)
Set the calendar to be used by this date format.
U_I18N_API DateFormat()
Default constructor.
U_I18N_API UnicodeString & format(UDate date, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Formats a UDate into a date/time string.
virtual U_I18N_API UDate parse(const UnicodeString &text, UErrorCode &status) const
Parse a date/time string.
virtual U_I18N_API UBool getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &status) const
Returns a boolean from this DateFormat May return U_UNSUPPORTED_ERROR if this instance does not suppo...
virtual U_I18N_API UnicodeString & format(Calendar &cal, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const
Formats a date into a date/time string.
static U_I18N_API DateFormat * createDateInstance(EStyle style=kDefault, const Locale &aLocale=Locale::getDefault())
Creates a date formatter with the given formatting style for the given const locale.
U_I18N_API UnicodeString & format(UDate date, UnicodeString &appendTo) const
Formats a UDate into a date/time string.
virtual U_I18N_API void setTimeZone(const TimeZone &zone)
Sets the time zone for the calendar of this DateFormat object.
Calendar * fCalendar
The calendar that DateFormat uses to produce the time field values needed to implement date/time form...
Definition: datefmt.h:851
NumberFormat * fNumberFormat
The number formatter that DateFormat uses to format numbers in dates and times.
Definition: datefmt.h:859
virtual U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const override
Format an object to produce a string.
virtual U_I18N_API DateFormat * clone() const override=0
Clones this object polymorphically.
virtual U_I18N_API const TimeZone & getTimeZone() const
Returns a reference to the TimeZone used by this DateFormat's calendar.
virtual U_I18N_API void adoptTimeZone(TimeZone *zoneToAdopt)
Sets the time zone for the calendar of this DateFormat object.
static U_I18N_API const Locale * getAvailableLocales(int32_t &count)
Gets the set of locales for which DateFormats are installed.
virtual U_I18N_API void setCalendarLenient(UBool lenient)
Specifies whether encapsulated Calendar date/time parsing is to be lenient.
virtual U_I18N_API ~DateFormat()
Destructor.
static U_I18N_API DateFormat * createInstanceForSkeleton(const UnicodeString &skeleton, UErrorCode &status)
Creates a date/time formatter for the given skeleton and default locale.
static U_I18N_API DateFormat * createDateTimeInstance(EStyle dateStyle=kDefault, EStyle timeStyle=kDefault, const Locale &aLocale=Locale::getDefault())
Creates a date/time formatter with the given formatting styles for the given locale.
virtual U_I18N_API const NumberFormat * getNumberFormat() const
Gets the number formatter which this date/time formatter uses to format and parse the numeric portion...
virtual U_I18N_API void adoptCalendar(Calendar *calendarToAdopt)
Set the calendar to be used by this date format.
virtual U_I18N_API void parse(const UnicodeString &text, Calendar &cal, ParsePosition &pos) const =0
Parse a date/time string beginning at the given parse position.
virtual U_I18N_API DateFormat & setBooleanAttribute(UDateFormatBooleanAttribute attr, UBool newvalue, UErrorCode &status)
Sets an boolean attribute on this DateFormat.
static U_I18N_API DateFormat * createInstance()
Create a default date/time formatter that uses the SHORT style for both the date and the time.
virtual U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, FieldPosition &pos, UErrorCode &status) const override
Format an object to produce a string.
virtual U_I18N_API UDisplayContext getContext(UDisplayContextType type, UErrorCode &status) const
Get the formatter's UDisplayContext value for the specified UDisplayContextType, such as UDISPCTX_TYP...
virtual U_I18N_API UBool isLenient() const
Returns whether both date/time parsing in the encapsulated Calendar object and DateFormat whitespace ...
virtual U_I18N_API bool operator==(const Format &) const override
Equality operator.
U_I18N_API DateFormat(const DateFormat &)
Copy constructor.
static U_I18N_API DateFormat * createInstanceForSkeleton(const UnicodeString &skeleton, const Locale &locale, UErrorCode &status)
Creates a date/time formatter for the given skeleton and locale.
virtual U_I18N_API void adoptNumberFormat(NumberFormat *formatToAdopt)
Allows you to set the number formatter.
EStyle
Constants for various style patterns.
Definition: datefmt.h:155
U_I18N_API UDate parse(const UnicodeString &text, ParsePosition &pos) const
Parse a date/time string beginning at the given parse position.
virtual U_I18N_API UBool isCalendarLenient() const
Returns whether date/time parsing in the encapsulated Calendar object processing is lenient.
U_I18N_API DateFormat & operator=(const DateFormat &)
Default assignment operator.
EField
Field selector for FieldPosition for DateFormat fields.
Definition: datefmt.h:892
virtual U_I18N_API void setLenient(UBool lenient)
Specifies whether date/time parsing is to be lenient.
virtual U_I18N_API UnicodeString & format(Calendar &cal, UnicodeString &appendTo, FieldPosition &fieldPosition) const =0
Formats a date into a date/time string.
virtual U_I18N_API const Calendar * getCalendar() const
Gets the calendar associated with this date/time formatter.
U_I18N_API UnicodeString & format(UDate date, UnicodeString &appendTo, FieldPosition &fieldPosition) const
Formats a UDate into a date/time string.
static U_I18N_API DateFormat * createTimeInstance(EStyle style=kDefault, const Locale &aLocale=Locale::getDefault())
Creates a time formatter with the given formatting style for the given locale.
virtual U_I18N_API void setNumberFormat(const NumberFormat &newNumberFormat)
Allows you to set the number formatter.
static U_I18N_API UnicodeString getBestPattern(const Locale &locale, const UnicodeString &skeleton, UErrorCode &status)
Returns the best pattern given a skeleton and locale.
virtual U_I18N_API void parseObject(const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const override
Parse a string to produce an object.
virtual U_I18N_API void setContext(UDisplayContext value, UErrorCode &status)
Set a particular UDisplayContext value in the formatter, such as UDISPCTX_CAPITALIZATION_FOR_STANDALO...
FieldPositionIterator returns the field ids and their start/limit positions generated by a call to Fo...
Definition: fpositer.h:58
FieldPosition is a simple class used by Format and its subclasses to identify fields in formatted out...
Definition: fieldpos.h:110
Base class for all formats.
Definition: format.h:98
U_I18N_API UnicodeString & format(const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const
Formats an object to produce a string.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition: fmtable.h:63
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:198
ParsePosition is a simple class used by Format and its subclasses to keep track of the current positi...
Definition: parsepos.h:52
TimeZone represents a time zone offset, and also figures out daylight savings.
Definition: timezone.h:133
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:303
C++: internal template EnumSet<>
C++ API: Base class for all formats.
C++ API: Locale ID object.
C++ API: Compatibility APIs for number formatting.
C API: DateFormat.
@ UDAT_AM_PM_FIELD
FieldPosition and UFieldPosition selector for 'a' field alignment, corresponding to the UCAL_AM_PM fi...
Definition: udat.h:631
@ UDAT_TIMEZONE_FIELD
FieldPosition and UFieldPosition selector for 'z' field alignment, corresponding to the UCAL_ZONE_OFF...
Definition: udat.h:657
@ UDAT_DOW_LOCAL_FIELD
FieldPosition and UFieldPosition selector for 'e' field alignment, corresponding to the UCAL_DOW_LOCA...
Definition: udat.h:671
@ UDAT_DATE_FIELD
FieldPosition and UFieldPosition selector for 'd' field alignment, corresponding to the UCAL_DATE fie...
Definition: udat.h:542
@ UDAT_MONTH_FIELD
FieldPosition and UFieldPosition selector for 'M' field alignment, corresponding to the UCAL_MONTH fi...
Definition: udat.h:535
@ UDAT_JULIAN_DAY_FIELD
FieldPosition and UFieldPosition selector for 'g' field alignment, corresponding to the UCAL_JULIAN_D...
Definition: udat.h:685
@ UDAT_EXTENDED_YEAR_FIELD
FieldPosition and UFieldPosition selector for 'u' field alignment, corresponding to the UCAL_EXTENDED...
Definition: udat.h:678
@ UDAT_DAY_OF_WEEK_FIELD
FieldPosition and UFieldPosition selector for 'E' field alignment, corresponding to the UCAL_DAY_OF_W...
Definition: udat.h:596
@ UDAT_MILLISECONDS_IN_DAY_FIELD
FieldPosition and UFieldPosition selector for 'A' field alignment, corresponding to the UCAL_MILLISEC...
Definition: udat.h:692
@ UDAT_YEAR_FIELD
FieldPosition and UFieldPosition selector for 'y' field alignment, corresponding to the UCAL_YEAR fie...
Definition: udat.h:528
@ UDAT_ERA_FIELD
FieldPosition and UFieldPosition selector for 'G' field alignment, corresponding to the UCAL_ERA fiel...
Definition: udat.h:521
@ UDAT_DAY_OF_YEAR_FIELD
FieldPosition and UFieldPosition selector for 'D' field alignment, corresponding to the UCAL_DAY_OF_Y...
Definition: udat.h:603
@ UDAT_WEEK_OF_MONTH_FIELD
FieldPosition and UFieldPosition selector for 'W' field alignment, corresponding to the UCAL_WEEK_OF_...
Definition: udat.h:624
@ UDAT_FRACTIONAL_SECOND_FIELD
FieldPosition and UFieldPosition selector for 'S' field alignment, corresponding to the UCAL_MILLISEC...
Definition: udat.h:589
@ UDAT_DAY_OF_WEEK_IN_MONTH_FIELD
FieldPosition and UFieldPosition selector for 'F' field alignment, corresponding to the UCAL_DAY_OF_W...
Definition: udat.h:610
@ UDAT_SECOND_FIELD
FieldPosition and UFieldPosition selector for 's' field alignment, corresponding to the UCAL_SECOND f...
Definition: udat.h:574
@ UDAT_HOUR1_FIELD
FieldPosition and UFieldPosition selector for 'h' field alignment, corresponding to the UCAL_HOUR fie...
Definition: udat.h:640
@ UDAT_MINUTE_FIELD
FieldPosition and UFieldPosition selector for 'm' field alignment, corresponding to the UCAL_MINUTE f...
Definition: udat.h:567
@ UDAT_HOUR0_FIELD
FieldPosition and UFieldPosition selector for 'K' field alignment, corresponding to the UCAL_HOUR fie...
Definition: udat.h:649
@ UDAT_YEAR_WOY_FIELD
FieldPosition and UFieldPosition selector for 'Y' field alignment, corresponding to the UCAL_YEAR_WOY...
Definition: udat.h:664
@ UDAT_WEEK_OF_YEAR_FIELD
FieldPosition and UFieldPosition selector for 'w' field alignment, corresponding to the UCAL_WEEK_OF_...
Definition: udat.h:617
@ UDAT_HOUR_OF_DAY0_FIELD
FieldPosition and UFieldPosition selector for 'H' field alignment, corresponding to the UCAL_HOUR_OF_...
Definition: udat.h:560
@ UDAT_HOUR_OF_DAY1_FIELD
FieldPosition and UFieldPosition selector for 'k' field alignment, corresponding to the UCAL_HOUR_OF_...
Definition: udat.h:551
UDateFormatBooleanAttribute
DateFormat boolean attributes.
Definition: udat.h:917
C API: Display context types (enum values)
UDisplayContextType
Display context types, for getting values of a particular setting.
UDisplayContext
Display context settings.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:509
#define U_I18N_API_CLASS
Set to export library symbols from inside the i18n library, and to import them from outside,...
Definition: utypes.h:457
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:316
double UDate
Date and Time data type.
Definition: utypes.h:218