Home | History | Annotate | Download | only in unicode
      1 /*
      2 *****************************************************************************************
      3 * Copyright (C) 2010-2011, International Business Machines
      4 * Corporation and others. All Rights Reserved.
      5 *****************************************************************************************
      6 */
      7 
      8 #ifndef UDATEINTERVALFORMAT_H
      9 #define UDATEINTERVALFORMAT_H
     10 
     11 #include "unicode/utypes.h"
     12 
     13 #if !UCONFIG_NO_FORMATTING
     14 
     15 #include "unicode/umisc.h"
     16 #include "unicode/localpointer.h"
     17 
     18 /**
     19  * A UDateIntervalFormat is used to format the range between two UDate values
     20  * in a locale-sensitive way, using a skeleton that specifies the precision and
     21  * completeness of the information to show. If the range smaller than the resolution
     22  * specified by the skeleton, a single date format will be produced. If the range
     23  * is larger than the format specified by the skeleton, a locale-specific fallback
     24  * will be used to format the items missing from the skeleton.
     25  *
     26  * For example, if the range is 2010-03-04 07:56 - 2010-03-04 19:56 (12 hours)
     27  * - The skeleton jm will produce
     28  *   for en_US, "7:56 AM - 7:56 PM"
     29  *   for en_GB, "7:56 - 19:56"
     30  * - The skeleton MMMd will produce
     31  *   for en_US, "Mar 4"
     32  *   for en_GB, "4 Mar"
     33  * If the range is 2010-03-04 07:56 - 2010-03-08 16:11 (4 days, 8 hours, 15 minutes)
     34  * - The skeleton jm will produce
     35  *   for en_US, "3/4/2010 7:56 AM - 3/8/2010 4:11 PM"
     36  *   for en_GB, "4/3/2010 7:56 - 8/3/2010 16:11"
     37  * - The skeleton MMMd will produce
     38  *   for en_US, "Mar 4-8"
     39  *   for en_GB, "4-8 Mar"
     40  *
     41  * Note:  the "-" characters in the above sample output will actually be
     42  * Unicode 2013, EN_DASH, in all but the last example.
     43  *
     44  * Note, in ICU 4.4 the standard skeletons for which date interval format data
     45  * is usually available are as follows; best results will be obtained by using
     46  * skeletons from this set, or those formed by combining these standard skeletons
     47  * (note that for these skeletons, the length of digit field such as d, y, or
     48  * M vs MM is irrelevant (but for non-digit fields such as MMM vs MMMM it is
     49  * relevant). Note that a skeleton involving h or H generally explicitly requests
     50  * that time style (12- or 24-hour time respectively). For a skeleton that
     51  * requests the locale's default time style (h or H), use 'j' instead of h or H.
     52  *   h, H, hm, Hm,
     53  *   hv, Hv, hmv, Hmv,
     54  *   d,
     55  *   M, MMM, MMMM,
     56  *   Md, MMMd,
     57  *   MEd, MMMEd,
     58  *   y,
     59  *   yM, yMMM, yMMMM,
     60  *   yMd, yMMMd,
     61  *   yMEd, yMMMEd
     62  *
     63  * Locales for which ICU 4.4 seems to have a reasonable amount of this data
     64  * include:
     65  *   af, am, ar, be, bg, bn, ca, cs, da, de (_AT), el, en (_AU,_CA,_GB,_IE,_IN...),
     66  *   eo, es (_AR,_CL,_CO,...,_US) et, fa, fi, fo, fr (_BE,_CH,_CA), fur, gsw, he,
     67  *   hr, hu, hy, is, it (_CH), ja, kk, km, ko, lt, lv, mk, ml, mt, nb, nl )_BE),
     68  *   nn, pl, pt (_PT), rm, ro, ru (_UA), sk, sl, so, sq, sr, sr_Latn, sv, th, to,
     69  *   tr, uk, ur, vi, zh (_SG), zh_Hant (_HK,_MO)
     70  */
     71 
     72 /**
     73  * Opaque UDateIntervalFormat object for use in C programs.
     74  * @draft ICU 4.8
     75  */
     76 struct UDateIntervalFormat;
     77 typedef struct UDateIntervalFormat UDateIntervalFormat;  /**< C typedef for struct UDateIntervalFormat. @draft ICU 4.8 */
     78 
     79 /**
     80  * Open a new UDateIntervalFormat object using the predefined rules for a
     81  * given locale plus a specified skeleton.
     82  * @param locale
     83  *            The locale for whose rules should be used; may be NULL for
     84  *            default locale.
     85  * @param skeleton
     86  *            A pattern containing only the fields desired for the interval
     87  *            format, for example "Hm", "yMMMd", or "yMMMEdHm".
     88  * @param skeletonLength
     89  *            The length of skeleton; may be -1 if the skeleton is zero-terminated.
     90  * @param tzID
     91  *            A timezone ID specifying the timezone to use. If 0, use the default
     92  *            timezone.
     93  * @param tzIDLength
     94  *            The length of tzID, or -1 if null-terminated. If 0, use the default
     95  *            timezone.
     96  * @param status
     97  *            A pointer to a UErrorCode to receive any errors.
     98  * @return
     99  *            A pointer to a UDateIntervalFormat object for the specified locale,
    100  *            or NULL if an error occurred.
    101  * @draft ICU 4.8
    102  */
    103 U_DRAFT UDateIntervalFormat* U_EXPORT2
    104 udtitvfmt_open(const char*  locale,
    105               const UChar* skeleton,
    106               int32_t      skeletonLength,
    107               const UChar* tzID,
    108               int32_t      tzIDLength,
    109               UErrorCode*  status);
    110 
    111 /**
    112  * Close a UDateIntervalFormat object. Once closed it may no longer be used.
    113  * @param formatter
    114  *            The UDateIntervalFormat object to close.
    115  * @draft ICU 4.8
    116  */
    117 U_DRAFT void U_EXPORT2
    118 udtitvfmt_close(UDateIntervalFormat *formatter);
    119 
    120 
    121 #if U_SHOW_CPLUSPLUS_API
    122 
    123 U_NAMESPACE_BEGIN
    124 
    125 /**
    126  * \class LocalUDateIntervalFormatPointer
    127  * "Smart pointer" class, closes a UDateIntervalFormat via udtitvfmt_close().
    128  * For most methods see the LocalPointerBase base class.
    129  *
    130  * @see LocalPointerBase
    131  * @see LocalPointer
    132  * @draft ICU 4.8
    133  */
    134 U_DEFINE_LOCAL_OPEN_POINTER(LocalUDateIntervalFormatPointer, UDateIntervalFormat, udtitvfmt_close);
    135 
    136 U_NAMESPACE_END
    137 
    138 #endif
    139 
    140 
    141 /**
    142  * Formats a date/time range using the conventions established for the
    143  * UDateIntervalFormat object.
    144  * @param formatter
    145  *            The UDateIntervalFormat object specifying the format conventions.
    146  * @param fromDate
    147  *            The starting point of the range.
    148  * @param toDate
    149  *            The ending point of the range.
    150  * @param result
    151  *            A pointer to a buffer to receive the formatted range.
    152  * @param resultCapacity
    153  *            The maximum size of result.
    154  * @param position
    155  *            A pointer to a UFieldPosition. On input, position->field is read.
    156  *            On output, position->beginIndex and position->endIndex indicate
    157  *            the beginning and ending indices of field number position->field,
    158  *            if such a field exists. This parameter may be NULL, in which case
    159  *            no field position data is returned.
    160  * @param status
    161  *            A pointer to a UErrorCode to receive any errors.
    162  * @return
    163  *            The total buffer size needed; if greater than resultLength, the
    164  *            output was truncated.
    165  * @draft ICU 4.8
    166  */
    167 U_DRAFT int32_t U_EXPORT2
    168 udtitvfmt_format(const UDateIntervalFormat* formatter,
    169                 UDate           fromDate,
    170                 UDate           toDate,
    171                 UChar*          result,
    172                 int32_t         resultCapacity,
    173                 UFieldPosition* position,
    174                 UErrorCode*     status);
    175 
    176 #endif /* #if !UCONFIG_NO_FORMATTING */
    177 
    178 #endif
    179