]> git.saurik.com Git - apple/icu.git/blobdiff - icuSources/i18n/unicode/dtitvfmt.h
ICU-59180.0.1.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / dtitvfmt.h
index 750454f703f2eaa76e63f78dfb391fec9b88726a..fe1db7a4a5efe6145e3720c62783755a51c86da7 100644 (file)
@@ -1,5 +1,7 @@
+// © 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.
 *******************************************************************************
 *
@@ -26,7 +28,9 @@
 #include "unicode/dtintrv.h"
 #include "unicode/dtitvinf.h"
 #include "unicode/dtptngen.h"
+#include "unicode/udateintervalformat.h"
 
+#if U_SHOW_CPLUSPLUS_API
 U_NAMESPACE_BEGIN
 
 
@@ -93,9 +97,11 @@ 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.
@@ -257,11 +263,16 @@ public:
      * "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(
@@ -323,6 +334,11 @@ public:
      * @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,
@@ -377,6 +393,9 @@ public:
      *                          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
@@ -396,6 +415,9 @@ public:
      *                          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
@@ -420,6 +442,9 @@ public:
      *                          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
@@ -483,12 +508,54 @@ public:
 
 
     /**
-     * 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:
@@ -543,7 +610,6 @@ private:
      * 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;
@@ -567,7 +633,7 @@ private:
 
     /**
      * default constructor
-     * @internal ICU 4.0
+     * @internal (private)
      */
     DateIntervalFormat();
 
@@ -585,10 +651,9 @@ private:
      * 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);
@@ -605,7 +670,6 @@ private:
      * @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,
@@ -613,28 +677,30 @@ private:
                                                 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);
 
 
     /**
@@ -643,20 +709,25 @@ private:
      * 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;
@@ -696,7 +767,6 @@ private:
      *
      *
      * @param status    output param set to success/failure code on exit
-     * @internal ICU 4.0
      */
     void initializePattern(UErrorCode& status);
 
@@ -708,7 +778,6 @@ private:
      * @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,
@@ -739,7 +808,6 @@ private:
      *  @param normalizedTime         Output parameter for normalized time only
      *                                skeleton.
      *
-     * @internal ICU 4.0
      */
     static void  U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton,
                                     UnicodeString& date,
@@ -771,7 +839,6 @@ private:
      * @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);
@@ -801,7 +868,6 @@ private:
      *                              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,
@@ -832,12 +898,11 @@ private:
      *
      * @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,
@@ -853,14 +918,11 @@ private:
      * 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);
@@ -870,7 +932,6 @@ private:
      * @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);
@@ -880,7 +941,6 @@ private:
      * 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);
 
@@ -889,7 +949,6 @@ private:
      * 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);
@@ -900,7 +959,6 @@ private:
      * @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,
@@ -915,16 +973,46 @@ private:
      * @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[];
 
 
     /**
@@ -945,16 +1033,25 @@ private:
     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
@@ -963,6 +1060,7 @@ DateIntervalFormat::operator!=(const Format& other) const  {
 }
 
 U_NAMESPACE_END
+#endif // U_SHOW_CPLUSPLUS_API
 
 #endif /* #if !UCONFIG_NO_FORMATTING */