+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************************
-* Copyright (C) 2008-2010, International Business Machines Corporation and
+* Copyright (C) 2008-2016, International Business Machines Corporation and
* others. All Rights Reserved.
*******************************************************************************
*
#include "unicode/dtintrv.h"
#include "unicode/dtitvinf.h"
#include "unicode/dtptngen.h"
+#include "unicode/udateintervalformat.h"
+#if U_SHOW_CPLUSPLUS_API
U_NAMESPACE_BEGIN
*
* <P>
* The calendar fields we support for interval formatting are:
- * year, month, date, day-of-week, am-pm, hour, hour-of-day, and minute.
+ * 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
+ * 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.
* "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs,
* "EEE, d MMM - EEE, d MMM, yyyy" for month differs,
* "EEE, d - EEE, d MMM, yyyy" for day differs,
- * @param skeleton the skeleton on which interval format based.
+ * @param skeleton the skeleton on which the interval format is based.
* @param locale the given locale
* @param status output param set to success/failure code on exit
* @return a date time interval formatter which the caller owns.
* @stable ICU 4.0
+ * <p>
+ * <h4>Sample code</h4>
+ * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
+ * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined
+ * <p>
*/
static DateIntervalFormat* U_EXPORT2 createInstance(
* @param status output param set to success/failure code on exit
* @return a date time interval formatter which the caller owns.
* @stable ICU 4.0
+ * <p>
+ * <h4>Sample code</h4>
+ * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
+ * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized
+ * <p>
*/
static DateIntervalFormat* U_EXPORT2 createInstance(
const UnicodeString& skeleton,
* Result is appended to existing contents.
* @param fieldPosition On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
+ * There may be multiple instances of a given field type
+ * in an interval format; in this case the fieldPosition
+ * offsets refer to the first instance.
* @param status Output param filled with success/failure status.
* @return Reference to 'appendTo' parameter.
* @stable ICU 4.0
* Result is appended to existing contents.
* @param fieldPosition On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
+ * There may be multiple instances of a given field type
+ * in an interval format; in this case the fieldPosition
+ * offsets refer to the first instance.
* @param status Output param filled with success/failure status.
* @return Reference to 'appendTo' parameter.
* @stable ICU 4.0
* Result is appended to existing contents.
* @param fieldPosition On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
+ * There may be multiple instances of a given field type
+ * in an interval format; in this case the fieldPosition
+ * offsets refer to the first instance.
* @param status Output param filled with success/failure status.
* Caller needs to make sure it is SUCCESS
* at the function entrance
/**
- * Gets the date formatter
+ * Gets the date formatter. The DateIntervalFormat instance continues to own
+ * the returned DateFormatter object, and will use and possibly modify it
+ * during format operations. In a multi-threaded environment, the returned
+ * DateFormat can only be used if it is certain that no other threads are
+ * concurrently using this DateIntervalFormatter, even for nominally const
+ * functions.
+ *
* @return the date formatter associated with this date interval formatter.
* @stable ICU 4.0
*/
const DateFormat* getDateFormat(void) const;
+ /**
+ * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
+ * @return the time zone associated with the calendar of DateIntervalFormat.
+ * @stable ICU 4.8
+ */
+ virtual const TimeZone& getTimeZone(void) const;
+
+ /**
+ * Sets the time zone for the calendar used by this DateIntervalFormat object. The
+ * caller no longer owns the TimeZone object and should not delete it after this call.
+ * @param zoneToAdopt the TimeZone to be adopted.
+ * @stable ICU 4.8
+ */
+ virtual void adoptTimeZone(TimeZone* zoneToAdopt);
+
+ /**
+ * Sets the time zone for the calendar used by this DateIntervalFormat object.
+ * @param zone the new time zone.
+ * @stable ICU 4.8
+ */
+ virtual void setTimeZone(const TimeZone& zone);
+
+ /**
+ * Change attributes for the DateIntervalFormat object.
+ * @param attr
+ * The attribute to change.
+ * @param value
+ * The new value for the attribute.
+ * @param status
+ * A UErrorCode to receive any errors.
+ * @internal
+ */
+ virtual void setAttribute(UDateIntervalFormatAttribute attr,
+ UDateIntervalFormatAttributeValue value,
+ UErrorCode &status);
+
/**
* Return the class ID for this class. This is useful only for comparing to
* a return value from getDynamicClassID(). For example:
* Also, the first date appears in an interval pattern could be
* the earlier date or the later date.
* And such information is saved in the interval pattern as well.
- * @internal ICU 4.0
*/
struct PatternInfo {
UnicodeString firstPart;
/**
* default constructor
- * @internal ICU 4.0
+ * @internal (private)
*/
DateIntervalFormat();
* Caller should not delete them.
*
* @param locale the locale of this date interval formatter.
- * @param dtitvinf the DateIntervalInfo object to be adopted.
+ * @param dtItvInfo the DateIntervalInfo object to be adopted.
* @param skeleton the skeleton of the date formatter
* @param status output param set to success/failure code on exit
- * @internal ICU 4.0
*/
DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo,
const UnicodeString* skeleton, UErrorCode& status);
* @param skeleton the skeleton of this formatter.
* @param status Output param set to success/failure code.
* @return a date time interval formatter which the caller owns.
- * @internal ICU 4.0
*/
static DateIntervalFormat* U_EXPORT2 create(const Locale& locale,
DateIntervalInfo* dtitvinf,
UErrorCode& status);
/**
- * Create a simple date/time formatter from skeleton, given locale,
- * and date time pattern generator.
- *
- * @param skeleton the skeleton on which date format based.
- * @param locale the given locale.
- * @param dtpng the date time pattern generator.
- * @param status Output param to be set to success/failure code.
- * If it is failure, the returned date formatter will
- * be NULL.
- * @return a simple date formatter which the caller owns.
- * @internal ICU 4.0
+ * Below are for generating interval patterns local to the formatter
*/
- static SimpleDateFormat* U_EXPORT2 createSDFPatternInstance(
- const UnicodeString& skeleton,
- const Locale& locale,
- DateTimePatternGenerator* dtpng,
- UErrorCode& status);
-
/**
- * Below are for generating interval patterns local to the formatter
+ * Provide an updated FieldPosition posResult based on two formats,
+ * the FieldPosition values for each of them, and the pattern used
+ * to combine them. The idea is for posResult to indicate the first
+ * instance (if any) of the specified field in the combined result,
+ * with correct offsets.
+ *
+ * @param combiningPattern Pattern used to combine pat0 and pat1
+ * @param pat0 Formatted date/time value to replace {0}
+ * @param pos0 FieldPosition within pat0
+ * @param pat1 Formatted date/time value to replace {1}
+ * @param pos1 FieldPosition within pat1
+ * @param posResult FieldPosition to be set to the correct
+ * position of the first field instance when
+ * pat0 and pat1 are combined using combiningPattern
*/
+ static void
+ adjustPosition(UnicodeString& combiningPattern, // has {0} and {1} in it
+ UnicodeString& pat0, FieldPosition& pos0, // pattern and pos corresponding to {0}
+ UnicodeString& pat1, FieldPosition& pos1, // pattern and pos corresponding to {1}
+ FieldPosition& posResult);
/**
* The full pattern used in this fall-back format is the
* full pattern of the date formatter.
*
+ * gFormatterMutex must already be locked when calling this function.
+ *
* @param fromCalendar calendar set to the from date in date interval
* to be formatted into date interval string
* @param toCalendar calendar set to the to date in date interval
* to be formatted into date interval string
+ * @param fromToOnSameDay TRUE iff from and to dates are on the same day
+ * (any difference is in ampm/hours or below)
* @param appendTo Output parameter to receive result.
* Result is appended to existing contents.
* @param pos On input: an alignment field, if desired.
* On output: the offsets of the alignment field.
* @param status output param set to success/failure code on exit
* @return Reference to 'appendTo' parameter.
- * @internal ICU 4.0
+ * @internal (private)
*/
UnicodeString& fallbackFormat(Calendar& fromCalendar,
Calendar& toCalendar,
+ UBool fromToOnSameDay,
UnicodeString& appendTo,
FieldPosition& pos,
UErrorCode& status) const;
*
*
* @param status output param set to success/failure code on exit
- * @internal ICU 4.0
*/
void initializePattern(UErrorCode& status);
* @param field the largest different calendar field
* @param skeleton a skeleton
* @param status output param set to success/failure code on exit
- * @internal ICU 4.0
*/
void setFallbackPattern(UCalendarDateFields field,
const UnicodeString& skeleton,
* @param normalizedTime Output parameter for normalized time only
* skeleton.
*
- * @internal ICU 4.0
*/
static void U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton,
UnicodeString& date,
* @return whether the resource is found for the skeleton.
* TRUE if interval pattern found for the skeleton,
* FALSE otherwise.
- * @internal ICU 4.0
*/
UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton,
const UnicodeString& timeSkeleton);
* through extending skeleton or not.
* TRUE if interval pattern is found by
* extending skeleton, FALSE otherwise.
- * @internal ICU 4.0
*/
UBool setIntervalPattern(UCalendarDateFields field,
const UnicodeString* skeleton,
*
* @param inputSkeleton the input skeleton
* @param bestMatchSkeleton the best match skeleton
- * @param bestMatchIntervalpattern the best match interval pattern
+ * @param bestMatchIntervalPattern the best match interval pattern
* @param differenceInfo the difference between 2 skeletons
* 1 means only field width differs
* 2 means v/z exchange
* @param adjustedIntervalPattern adjusted interval pattern
- * @internal ICU 4.0
*/
static void U_EXPORT2 adjustFieldWidth(
const UnicodeString& inputSkeleton,
* both time and date. Present the date followed by
* the range expression for the time.
* @param format date and time format
- * @param formatLen format string length
* @param datePattern date pattern
* @param field time calendar field: AM_PM, HOUR, MINUTE
* @param status output param set to success/failure code on exit
- * @internal ICU 4.0
*/
- void concatSingleDate2TimeInterval(const UChar* format,
- int32_t formatLen,
+ void concatSingleDate2TimeInterval(UnicodeString& format,
const UnicodeString& datePattern,
UCalendarDateFields field,
UErrorCode& status);
* @param field calendar field need to check
* @param skeleton given skeleton on which to check the calendar field
* @return true if field present in a skeleton.
- * @internal ICU 4.0
*/
static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field,
const UnicodeString& skeleton);
* Split interval patterns into 2 part.
* @param intervalPattern interval pattern
* @return the index in interval pattern which split the pattern into 2 part
- * @internal ICU 4.0
*/
static int32_t U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern);
* Break interval patterns as 2 part and save them into pattern info.
* @param field calendar field
* @param intervalPattern interval pattern
- * @internal ICU 4.0
*/
void setIntervalPattern(UCalendarDateFields field,
const UnicodeString& intervalPattern);
* @param field calendar field
* @param intervalPattern interval pattern
* @param laterDateFirst whether later date appear first in interval pattern
- * @internal ICU 4.0
*/
void setIntervalPattern(UCalendarDateFields field,
const UnicodeString& intervalPattern,
* @param secondPart the second part in interval pattern
* @param laterDateFirst whether the first date in intervalPattern
* is earlier date or later date
- * @internal ICU 4.0
*/
void setPatternInfo(UCalendarDateFields field,
const UnicodeString* firstPart,
- const UnicodeString* secondpart,
+ const UnicodeString* secondPart,
UBool laterDateFirst);
+ /**
+ * Format 2 Calendars to produce a string.
+ * Implementation of the similar public format function.
+ * Must be called with gFormatterMutex already locked.
+ *
+ * Note: "fromCalendar" and "toCalendar" are not const,
+ * since calendar is not const in SimpleDateFormat::format(Calendar&),
+ *
+ * @param fromCalendar calendar set to the from date in date interval
+ * to be formatted into date interval string
+ * @param toCalendar calendar set to the to date in date interval
+ * to be formatted into date interval string
+ * @param appendTo Output parameter to receive result.
+ * Result is appended to existing contents.
+ * @param fieldPosition On input: an alignment field, if desired.
+ * On output: the offsets of the alignment field.
+ * There may be multiple instances of a given field type
+ * in an interval format; in this case the fieldPosition
+ * offsets refer to the first instance.
+ * @param status Output param filled with success/failure status.
+ * Caller needs to make sure it is SUCCESS
+ * at the function entrance
+ * @return Reference to 'appendTo' parameter.
+ * @internal (private)
+ */
+ UnicodeString& formatImpl(Calendar& fromCalendar,
+ Calendar& toCalendar,
+ UnicodeString& appendTo,
+ FieldPosition& fieldPosition,
+ UErrorCode& status) const ;
+
// from calendar field to pattern letter
- static const UChar fgCalendarFieldToPatternLetter[];
+ static const char16_t fgCalendarFieldToPatternLetter[];
/**
Calendar* fFromCalendar;
Calendar* fToCalendar;
- /**
- * Date time pattern generator
- */
- DateTimePatternGenerator* fDtpng;
+ Locale fLocale;
/**
- * Following are interval information relavent (locale) to this formatter.
+ * Following are interval information relevant (locale) to this formatter.
*/
UnicodeString fSkeleton;
PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX];
+
+ /**
+ * Patterns for fallback formatting.
+ */
+ UnicodeString* fDatePattern;
+ UnicodeString* fTimePattern;
+ UnicodeString* fDateTimeFormat;
+
+ /**
+ * Atttributes
+ */
+ int32_t fMinimizeType;
};
inline UBool
}
U_NAMESPACE_END
+#endif // U_SHOW_CPLUSPLUS_API
#endif /* #if !UCONFIG_NO_FORMATTING */