|
ICU 57.1
57.1
|
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent manner. More...
#include <dtitvfmt.h>
Public Member Functions | |
| virtual | ~DateIntervalFormat () |
| Destructor. More... | |
| virtual Format * | clone (void) const |
| Clone this Format object polymorphically. More... | |
| virtual UBool | operator== (const Format &other) const |
| Return true if the given Format objects are semantically equal. More... | |
| UBool | operator!= (const Format &other) const |
| Return true if the given Format objects are not semantically equal. More... | |
| virtual UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPosition &fieldPosition, UErrorCode &status) const |
| Format an object to produce a string. More... | |
| UnicodeString & | format (const DateInterval *dtInterval, UnicodeString &appendTo, FieldPosition &fieldPosition, UErrorCode &status) const |
| Format a DateInterval to produce a string. More... | |
| UnicodeString & | format (Calendar &fromCalendar, Calendar &toCalendar, UnicodeString &appendTo, FieldPosition &fieldPosition, UErrorCode &status) const |
| Format 2 Calendars to produce a string. More... | |
| virtual void | parseObject (const UnicodeString &source, Formattable &result, ParsePosition &parse_pos) const |
| Date interval parsing is not supported. More... | |
| const DateIntervalInfo * | getDateIntervalInfo (void) const |
| Gets the date time interval patterns. More... | |
| void | setDateIntervalInfo (const DateIntervalInfo &newIntervalPatterns, UErrorCode &status) |
| Set the date time interval patterns. More... | |
| const DateFormat * | getDateFormat (void) const |
| Gets the date formatter. More... | |
| virtual const TimeZone & | getTimeZone (void) const |
| Returns a reference to the TimeZone used by this DateIntervalFormat's calendar. More... | |
| virtual void | adoptTimeZone (TimeZone *zoneToAdopt) |
| Sets the time zone for the calendar used by this DateIntervalFormat object. More... | |
| virtual void | setTimeZone (const TimeZone &zone) |
| Sets the time zone for the calendar used by this DateIntervalFormat object. More... | |
| virtual UClassID | getDynamicClassID (void) const |
| Returns a unique class ID POLYMORPHICALLY. More... | |
Public Member Functions inherited from icu::Format | |
| virtual | ~Format () |
| Destructor. More... | |
| UBool | operator!= (const Format &other) const |
| Return true if the given Format objects are not semantically equal. More... | |
| UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, UErrorCode &status) const |
| Formats an object to produce a string. More... | |
| virtual UnicodeString & | format (const Formattable &obj, UnicodeString &appendTo, FieldPositionIterator *posIter, UErrorCode &status) const |
| Format an object to produce a string. More... | |
| void | parseObject (const UnicodeString &source, Formattable &result, UErrorCode &status) const |
| Parses a string to produce an object. More... | |
| Locale | getLocale (ULocDataLocaleType type, UErrorCode &status) const |
| Get the locale for this format object. More... | |
| const char * | getLocaleID (ULocDataLocaleType type, UErrorCode &status) const |
| Get the locale for this format object. More... | |
Public Member Functions inherited from icu::UObject | |
| virtual | ~UObject () |
| Destructor. More... | |
Static Public Member Functions | |
| static DateIntervalFormat * | createInstance (const UnicodeString &skeleton, UErrorCode &status) |
| Construct a DateIntervalFormat from skeleton and the default locale. More... | |
| static DateIntervalFormat * | createInstance (const UnicodeString &skeleton, const Locale &locale, UErrorCode &status) |
| Construct a DateIntervalFormat from skeleton and a given locale. More... | |
| static DateIntervalFormat * | createInstance (const UnicodeString &skeleton, const DateIntervalInfo &dtitvinf, UErrorCode &status) |
| Construct a DateIntervalFormat from skeleton DateIntervalInfo, and default locale. More... | |
| static DateIntervalFormat * | createInstance (const UnicodeString &skeleton, const Locale &locale, const DateIntervalInfo &dtitvinf, UErrorCode &status) |
| Construct a DateIntervalFormat from skeleton a DateIntervalInfo, and the given locale. More... | |
| static UClassID | getStaticClassID (void) |
| Return the class ID for this class. More... | |
Protected Member Functions | |
| DateIntervalFormat (const DateIntervalFormat &) | |
| Copy constructor. More... | |
| DateIntervalFormat & | operator= (const DateIntervalFormat &) |
| Assignment operator. More... | |
Protected Member Functions inherited from icu::Format | |
| void | setLocaleIDs (const char *valid, const char *actual) |
| Format () | |
| Default constructor for subclass use only. More... | |
| Format (const Format &) | |
| Format & | operator= (const Format &) |
Additional Inherited Members | |
Static Protected Member Functions inherited from icu::Format | |
| static void | syntaxError (const UnicodeString &pattern, int32_t pos, UParseError &parseError) |
| Simple function for initializing a UParseError from a UnicodeString. More... | |
DateIntervalFormat is a class for formatting and parsing date intervals in a language-independent manner.
Only formatting is supported, parsing is not supported.
Date interval means from one date to another date, for example, from "Jan 11, 2008" to "Jan 18, 2008". We introduced class DateInterval to represent it. DateInterval is a pair of UDate, which is the standard milliseconds since 24:00 GMT, Jan 1, 1970.
DateIntervalFormat formats a DateInterval into text as compactly as possible. For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008" is "Jan 11-18, 2008" for English. And it parses text into DateInterval, although initially, parsing is not supported.
There is no structural information in date time patterns. For any punctuations and string literals inside a date time pattern, we do not know whether it is just a separator, or a prefix, or a suffix. Without such information, so, it is difficult to generate a sub-pattern (or super-pattern) by algorithm. So, formatting a DateInterval is pattern-driven. It is very similar to formatting in SimpleDateFormat. We introduce class DateIntervalInfo to save date interval patterns, similar to date time pattern in SimpleDateFormat.
Logically, the interval patterns are mappings from (skeleton, the_largest_different_calendar_field) to (date_interval_pattern).
A skeleton
For those non-digit calendar fields, the pattern letter length is important, such as MMM, MMMM, and MMMMM; EEE and EEEE, and the field's pattern letter length is honored.
For the digit calendar fields, such as M or MM, d or dd, yy or yyyy, the field pattern length is ignored and the best match, which is defined in date time patterns, will be returned without honor the field pattern letter length in skeleton.
The calendar fields we support for interval formatting are: year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, and second (though we do not currently have specific intervalFormat date for skeletons with seconds). Those calendar fields can be defined in the following order: year > month > date > hour (in day) > minute > second
The largest different calendar fields between 2 calendars is the first different calendar field in above order.
For example: the largest different calendar fields between "Jan 10, 2007" and "Feb 20, 2008" is year.
For other calendar fields, the compact interval formatting is not supported. And the interval format will be fall back to fall-back patterns, which is mostly "{date0} - {date1}".
There is a set of pre-defined static skeleton strings. There are pre-defined interval patterns for those pre-defined skeletons in locales' resource files. For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is "yMMMd", in en_US, if the largest different calendar field between date1 and date2 is "year", the date interval pattern is "MMM d, yyyy - MMM d, yyyy", such as "Jan 10, 2007 - Jan 10, 2008". If the largest different calendar field between date1 and date2 is "month", the date interval pattern is "MMM d - MMM d, yyyy", such as "Jan 10 - Feb 10, 2007". If the largest different calendar field between date1 and date2 is "day", the date interval pattern is "MMM d-d, yyyy", such as "Jan 10-20, 2007".
For date skeleton, the interval patterns when year, or month, or date is different are defined in resource files. For time skeleton, the interval patterns when am/pm, or hour, or minute is different are defined in resource files.
If a skeleton is not found in a locale's DateIntervalInfo, which means the interval patterns for the skeleton is not defined in resource file, the interval pattern will falls back to the interval "fallback" pattern defined in resource file. If the interval "fallback" pattern is not defined, the default fall-back is "{date0} - {data1}".
For the combination of date and time, The rule to generate interval patterns are:
If two dates are the same, the interval pattern is the single date pattern. For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is "Jan 10, 2007".
Or if the presenting fields between 2 dates have the exact same values, the interval pattern is the single date pattern. For example, if user only requests year and month, the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007".
DateIntervalFormat needs the following information for correct formatting: time zone, calendar type, pattern, date format symbols, and date interval patterns. It can be instantiated in 2 ways:
For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc. DateIntervalFormat uses the same syntax as that of DateTime format.
Code Sample: general usage
// the date interval object which the DateIntervalFormat formats on// and parses intoDateInterval* dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2);UErrorCode status = U_ZERO_ERROR;DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance(Locale("en", "GB", ""), status);UnicodeUnicodeString dateIntervalString;FieldPosition pos = 0;// formattingdtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status);delete dtIntervalFmt;
Definition at line 220 of file dtitvfmt.h.
|
virtual |
Destructor.
|
protected |
Copy constructor.