Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/unicode/ureldatefmt.h was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 // © 2016 and later: Unicode, Inc. and others.
0002 // License & terms of use: http://www.unicode.org/copyright.html
0003 /*
0004 *****************************************************************************************
0005 * Copyright (C) 2016, International Business Machines
0006 * Corporation and others. All Rights Reserved.
0007 *****************************************************************************************
0008 */
0009 
0010 #ifndef URELDATEFMT_H
0011 #define URELDATEFMT_H
0012 
0013 #include "unicode/utypes.h"
0014 
0015 #if !UCONFIG_NO_FORMATTING
0016 
0017 #include "unicode/unum.h"
0018 #include "unicode/udisplaycontext.h"
0019 #include "unicode/uformattedvalue.h"
0020 
0021 #if U_SHOW_CPLUSPLUS_API
0022 #include "unicode/localpointer.h"
0023 #endif   // U_SHOW_CPLUSPLUS_API
0024 
0025 /**
0026  * \file
0027  * \brief C API: URelativeDateTimeFormatter, relative date formatting of unit + numeric offset.
0028  *
0029  * Provides simple formatting of relative dates, in two ways
0030  * <ul>
0031  *   <li>relative dates with a quantity e.g "in 5 days"</li>
0032  *   <li>relative dates without a quantity e.g "next Tuesday"</li>
0033  * </ul>
0034  * <p>
0035  * This does not provide compound formatting for multiple units,
0036  * other than the ability to combine a time string with a relative date,
0037  * as in "next Tuesday at 3:45 PM". It also does not provide support
0038  * for determining which unit to use, such as deciding between "in 7 days"
0039  * and "in 1 week".
0040  *
0041  * @stable ICU 57
0042  */
0043 
0044 /**
0045  * The formatting style
0046  * @stable ICU 54
0047  */
0048 typedef enum UDateRelativeDateTimeFormatterStyle {
0049   /**
0050    * Everything spelled out.
0051    * @stable ICU 54
0052    */
0053   UDAT_STYLE_LONG,
0054 
0055   /**
0056    * Abbreviations used when possible.
0057    * @stable ICU 54
0058    */
0059   UDAT_STYLE_SHORT,
0060 
0061   /**
0062    * Use the shortest possible form.
0063    * @stable ICU 54
0064    */
0065   UDAT_STYLE_NARROW,
0066 
0067 #ifndef U_HIDE_DEPRECATED_API
0068     /**
0069      * One more than the highest normal UDateRelativeDateTimeFormatterStyle value.
0070      * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
0071      */
0072     UDAT_STYLE_COUNT
0073 #endif  /* U_HIDE_DEPRECATED_API */
0074 } UDateRelativeDateTimeFormatterStyle;
0075 
0076 /**
0077  * Represents the unit for formatting a relative date. e.g "in 5 days"
0078  * or "next year"
0079  * @stable ICU 57
0080  */
0081 typedef enum URelativeDateTimeUnit {
0082     /**
0083      * Specifies that relative unit is year, e.g. "last year",
0084      * "in 5 years". 
0085      * @stable ICU 57
0086      */
0087     UDAT_REL_UNIT_YEAR,
0088     /**
0089      * Specifies that relative unit is quarter, e.g. "last quarter",
0090      * "in 5 quarters".
0091      * @stable ICU 57
0092      */
0093     UDAT_REL_UNIT_QUARTER,
0094     /**
0095      * Specifies that relative unit is month, e.g. "last month",
0096      * "in 5 months".
0097      * @stable ICU 57
0098      */
0099     UDAT_REL_UNIT_MONTH,
0100     /**
0101      * Specifies that relative unit is week, e.g. "last week",
0102      * "in 5 weeks".
0103      * @stable ICU 57
0104      */
0105     UDAT_REL_UNIT_WEEK,
0106     /**
0107      * Specifies that relative unit is day, e.g. "yesterday",
0108      * "in 5 days".
0109      * @stable ICU 57
0110      */
0111     UDAT_REL_UNIT_DAY,
0112     /**
0113      * Specifies that relative unit is hour, e.g. "1 hour ago",
0114      * "in 5 hours".
0115      * @stable ICU 57
0116      */
0117     UDAT_REL_UNIT_HOUR,
0118     /**
0119      * Specifies that relative unit is minute, e.g. "1 minute ago",
0120      * "in 5 minutes".
0121      * @stable ICU 57
0122      */
0123     UDAT_REL_UNIT_MINUTE,
0124     /**
0125      * Specifies that relative unit is second, e.g. "1 second ago",
0126      * "in 5 seconds".
0127      * @stable ICU 57
0128      */
0129     UDAT_REL_UNIT_SECOND,
0130     /**
0131      * Specifies that relative unit is Sunday, e.g. "last Sunday",
0132      * "this Sunday", "next Sunday", "in 5 Sundays".
0133      * @stable ICU 57
0134      */
0135     UDAT_REL_UNIT_SUNDAY,
0136     /**
0137      * Specifies that relative unit is Monday, e.g. "last Monday",
0138      * "this Monday", "next Monday", "in 5 Mondays".
0139      * @stable ICU 57
0140      */
0141     UDAT_REL_UNIT_MONDAY,
0142     /**
0143      * Specifies that relative unit is Tuesday, e.g. "last Tuesday",
0144      * "this Tuesday", "next Tuesday", "in 5 Tuesdays".
0145      * @stable ICU 57
0146      */
0147     UDAT_REL_UNIT_TUESDAY,
0148     /**
0149      * Specifies that relative unit is Wednesday, e.g. "last Wednesday",
0150      * "this Wednesday", "next Wednesday", "in 5 Wednesdays".
0151      * @stable ICU 57
0152      */
0153     UDAT_REL_UNIT_WEDNESDAY,
0154     /**
0155      * Specifies that relative unit is Thursday, e.g. "last Thursday",
0156      * "this Thursday", "next Thursday", "in 5 Thursdays". 
0157      * @stable ICU 57
0158      */
0159     UDAT_REL_UNIT_THURSDAY,
0160     /**
0161      * Specifies that relative unit is Friday, e.g. "last Friday",
0162      * "this Friday", "next Friday", "in 5 Fridays".
0163      * @stable ICU 57
0164      */
0165     UDAT_REL_UNIT_FRIDAY,
0166     /**
0167      * Specifies that relative unit is Saturday, e.g. "last Saturday",
0168      * "this Saturday", "next Saturday", "in 5 Saturdays".
0169      * @stable ICU 57
0170      */
0171     UDAT_REL_UNIT_SATURDAY,
0172 #ifndef U_HIDE_DEPRECATED_API
0173     /**
0174      * One more than the highest normal URelativeDateTimeUnit value.
0175      * @deprecated ICU 58 The numeric value may change over time, see ICU ticket #12420.
0176      */
0177     UDAT_REL_UNIT_COUNT
0178 #endif  /* U_HIDE_DEPRECATED_API */
0179 } URelativeDateTimeUnit;
0180 
0181 /**
0182  * FieldPosition and UFieldPosition selectors for format fields
0183  * defined by RelativeDateTimeFormatter.
0184  * @stable ICU 64
0185  */
0186 typedef enum URelativeDateTimeFormatterField {
0187     /**
0188      * Represents a literal text string, like "tomorrow" or "days ago".
0189      * @stable ICU 64
0190      */
0191     UDAT_REL_LITERAL_FIELD,
0192     /**
0193      * Represents a number quantity, like "3" in "3 days ago".
0194      * @stable ICU 64
0195      */
0196     UDAT_REL_NUMERIC_FIELD,
0197 } URelativeDateTimeFormatterField;
0198 
0199 
0200 /**
0201  * Opaque URelativeDateTimeFormatter object for use in C programs.
0202  * @stable ICU 57
0203  */
0204 struct URelativeDateTimeFormatter;
0205 typedef struct URelativeDateTimeFormatter URelativeDateTimeFormatter;  /**< C typedef for struct URelativeDateTimeFormatter. @stable ICU 57 */
0206 
0207 
0208 /**
0209  * Open a new URelativeDateTimeFormatter object for a given locale using the
0210  * specified width and capitalizationContext, along with a number formatter
0211  * (if desired) to override the default formatter that would be used for
0212  * display of numeric field offsets. The default formatter typically rounds
0213  * toward 0 and has a minimum of 0 fraction digits and a maximum of 3
0214  * fraction digits (i.e. it will show as many decimal places as necessary
0215  * up to 3, without showing trailing 0s).
0216  *
0217  * @param locale
0218  *          The locale
0219  * @param nfToAdopt
0220  *          A number formatter to set for this URelativeDateTimeFormatter
0221  *          object (instead of the default decimal formatter). Ownership of
0222  *          this UNumberFormat object will pass to the URelativeDateTimeFormatter
0223  *          object (the URelativeDateTimeFormatter adopts the UNumberFormat),
0224  *          which becomes responsible for closing it. If the caller wishes to
0225  *          retain ownership of the UNumberFormat object, the caller must clone
0226  *          it (with unum_clone) and pass the clone to ureldatefmt_open. May be
0227  *          NULL to use the default decimal formatter.
0228  * @param width
0229  *          The width - wide, short, narrow, etc.
0230  * @param capitalizationContext
0231  *          A value from UDisplayContext that pertains to capitalization, e.g.
0232  *          UDISPCTX_CAPITALIZATION_FOR_BEGINNING_OF_SENTENCE.
0233  * @param status
0234  *          A pointer to a UErrorCode to receive any errors.
0235  * @return
0236  *          A pointer to a URelativeDateTimeFormatter object for the specified locale,
0237  *          or NULL if an error occurred.
0238  * @stable ICU 57
0239  */
0240 U_CAPI URelativeDateTimeFormatter* U_EXPORT2
0241 ureldatefmt_open( const char*          locale,
0242                   UNumberFormat*       nfToAdopt,
0243                   UDateRelativeDateTimeFormatterStyle width,
0244                   UDisplayContext      capitalizationContext,
0245                   UErrorCode*          status );
0246 
0247 /**
0248  * Close a URelativeDateTimeFormatter object. Once closed it may no longer be used.
0249  * @param reldatefmt
0250  *            The URelativeDateTimeFormatter object to close.
0251  * @stable ICU 57
0252  */
0253 U_CAPI void U_EXPORT2
0254 ureldatefmt_close(URelativeDateTimeFormatter *reldatefmt);
0255 
0256 struct UFormattedRelativeDateTime;
0257 /**
0258  * Opaque struct to contain the results of a URelativeDateTimeFormatter operation.
0259  * @stable ICU 64
0260  */
0261 typedef struct UFormattedRelativeDateTime UFormattedRelativeDateTime;
0262 
0263 /**
0264  * Creates an object to hold the result of a URelativeDateTimeFormatter
0265  * operation. The object can be used repeatedly; it is cleared whenever
0266  * passed to a format function.
0267  *
0268  * @param ec Set if an error occurs.
0269  * @return A pointer needing ownership.
0270  * @stable ICU 64
0271  */
0272 U_CAPI UFormattedRelativeDateTime* U_EXPORT2
0273 ureldatefmt_openResult(UErrorCode* ec);
0274 
0275 /**
0276  * Returns a representation of a UFormattedRelativeDateTime as a UFormattedValue,
0277  * which can be subsequently passed to any API requiring that type.
0278  *
0279  * The returned object is owned by the UFormattedRelativeDateTime and is valid
0280  * only as long as the UFormattedRelativeDateTime is present and unchanged in memory.
0281  *
0282  * You can think of this method as a cast between types.
0283  *
0284  * @param ufrdt The object containing the formatted string.
0285  * @param ec Set if an error occurs.
0286  * @return A UFormattedValue owned by the input object.
0287  * @stable ICU 64
0288  */
0289 U_CAPI const UFormattedValue* U_EXPORT2
0290 ureldatefmt_resultAsValue(const UFormattedRelativeDateTime* ufrdt, UErrorCode* ec);
0291 
0292 /**
0293  * Releases the UFormattedRelativeDateTime created by ureldatefmt_openResult.
0294  *
0295  * @param ufrdt The object to release.
0296  * @stable ICU 64
0297  */
0298 U_CAPI void U_EXPORT2
0299 ureldatefmt_closeResult(UFormattedRelativeDateTime* ufrdt);
0300 
0301 
0302 #if U_SHOW_CPLUSPLUS_API
0303 
0304 U_NAMESPACE_BEGIN
0305 
0306 /**
0307  * \class LocalURelativeDateTimeFormatterPointer
0308  * "Smart pointer" class, closes a URelativeDateTimeFormatter via ureldatefmt_close().
0309  * For most methods see the LocalPointerBase base class.
0310  *
0311  * @see LocalPointerBase
0312  * @see LocalPointer
0313  * @stable ICU 57
0314  */
0315 U_DEFINE_LOCAL_OPEN_POINTER(LocalURelativeDateTimeFormatterPointer, URelativeDateTimeFormatter, ureldatefmt_close);
0316 
0317 /**
0318  * \class LocalUFormattedRelativeDateTimePointer
0319  * "Smart pointer" class, closes a UFormattedRelativeDateTime via ureldatefmt_closeResult().
0320  * For most methods see the LocalPointerBase base class.
0321  *
0322  * @see LocalPointerBase
0323  * @see LocalPointer
0324  * @stable ICU 64
0325  */
0326 U_DEFINE_LOCAL_OPEN_POINTER(LocalUFormattedRelativeDateTimePointer, UFormattedRelativeDateTime, ureldatefmt_closeResult);
0327 
0328 U_NAMESPACE_END
0329 
0330 #endif
0331 
0332 /**
0333  * Format a combination of URelativeDateTimeUnit and numeric
0334  * offset using a numeric style, e.g. "1 week ago", "in 1 week",
0335  * "5 weeks ago", "in 5 weeks".
0336  *
0337  * @param reldatefmt
0338  *          The URelativeDateTimeFormatter object specifying the
0339  *          format conventions.
0340  * @param offset
0341  *          The signed offset for the specified unit. This will
0342  *          be formatted according to this object's UNumberFormat
0343  *          object.
0344  * @param unit
0345  *          The unit to use when formatting the relative
0346  *          date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
0347  * @param result
0348  *          A pointer to a buffer to receive the formatted result.
0349  * @param resultCapacity
0350  *          The maximum size of result.
0351  * @param status
0352  *          A pointer to a UErrorCode to receive any errors. In
0353  *          case of error status, the contents of result are
0354  *          undefined.
0355  * @return
0356  *          The length of the formatted result; may be greater
0357  *          than resultCapacity, in which case an error is returned.
0358  * @stable ICU 57
0359  */
0360 U_CAPI int32_t U_EXPORT2
0361 ureldatefmt_formatNumeric( const URelativeDateTimeFormatter* reldatefmt,
0362                     double                offset,
0363                     URelativeDateTimeUnit unit,
0364                     UChar*                result,
0365                     int32_t               resultCapacity,
0366                     UErrorCode*           status);
0367 
0368 /**
0369  * Format a combination of URelativeDateTimeUnit and numeric
0370  * offset using a numeric style, e.g. "1 week ago", "in 1 week",
0371  * "5 weeks ago", "in 5 weeks".
0372  *
0373  * @param reldatefmt
0374  *          The URelativeDateTimeFormatter object specifying the
0375  *          format conventions.
0376  * @param offset
0377  *          The signed offset for the specified unit. This will
0378  *          be formatted according to this object's UNumberFormat
0379  *          object.
0380  * @param unit
0381  *          The unit to use when formatting the relative
0382  *          date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
0383  * @param result
0384  *          A pointer to a UFormattedRelativeDateTime to populate.
0385  * @param status
0386  *          A pointer to a UErrorCode to receive any errors. In
0387  *          case of error status, the contents of result are
0388  *          undefined.
0389  * @stable ICU 64
0390  */
0391 U_CAPI void U_EXPORT2
0392 ureldatefmt_formatNumericToResult(
0393     const URelativeDateTimeFormatter* reldatefmt,
0394     double                            offset,
0395     URelativeDateTimeUnit             unit,
0396     UFormattedRelativeDateTime*       result,
0397     UErrorCode*                       status);
0398 
0399 /**
0400  * Format a combination of URelativeDateTimeUnit and numeric offset
0401  * using a text style if possible, e.g. "last week", "this week",
0402  * "next week", "yesterday", "tomorrow". Falls back to numeric
0403  * style if no appropriate text term is available for the specified
0404  * offset in the object's locale.
0405  *
0406  * @param reldatefmt
0407  *          The URelativeDateTimeFormatter object specifying the
0408  *          format conventions.
0409  * @param offset
0410  *          The signed offset for the specified unit.
0411  * @param unit
0412  *          The unit to use when formatting the relative
0413  *          date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
0414  * @param result
0415  *          A pointer to a buffer to receive the formatted result.
0416  * @param resultCapacity
0417  *          The maximum size of result.
0418  * @param status
0419  *          A pointer to a UErrorCode to receive any errors. In
0420  *          case of error status, the contents of result are
0421  *          undefined.
0422  * @return
0423  *          The length of the formatted result; may be greater
0424  *          than resultCapacity, in which case an error is returned.
0425  * @stable ICU 57
0426  */
0427 U_CAPI int32_t U_EXPORT2
0428 ureldatefmt_format( const URelativeDateTimeFormatter* reldatefmt,
0429                     double                offset,
0430                     URelativeDateTimeUnit unit,
0431                     UChar*                result,
0432                     int32_t               resultCapacity,
0433                     UErrorCode*           status);
0434 
0435 /**
0436  * Format a combination of URelativeDateTimeUnit and numeric offset
0437  * using a text style if possible, e.g. "last week", "this week",
0438  * "next week", "yesterday", "tomorrow". Falls back to numeric
0439  * style if no appropriate text term is available for the specified
0440  * offset in the object's locale.
0441  *
0442  * This method populates a UFormattedRelativeDateTime, which exposes more
0443  * information than the string populated by format().
0444  *
0445  * @param reldatefmt
0446  *          The URelativeDateTimeFormatter object specifying the
0447  *          format conventions.
0448  * @param offset
0449  *          The signed offset for the specified unit.
0450  * @param unit
0451  *          The unit to use when formatting the relative
0452  *          date, e.g. UDAT_REL_UNIT_WEEK, UDAT_REL_UNIT_FRIDAY.
0453  * @param result
0454  *          A pointer to a UFormattedRelativeDateTime to populate.
0455  * @param status
0456  *          A pointer to a UErrorCode to receive any errors. In
0457  *          case of error status, the contents of result are
0458  *          undefined.
0459  * @stable ICU 64
0460  */
0461 U_CAPI void U_EXPORT2
0462 ureldatefmt_formatToResult(
0463     const URelativeDateTimeFormatter* reldatefmt,
0464     double                            offset,
0465     URelativeDateTimeUnit             unit,
0466     UFormattedRelativeDateTime*       result,
0467     UErrorCode*                       status);
0468 
0469 /**
0470  * Combines a relative date string and a time string in this object's
0471  * locale. This is done with the same date-time separator used for the
0472  * default calendar in this locale to produce a result such as
0473  * "yesterday at 3:45 PM".
0474  *
0475  * @param reldatefmt
0476  *          The URelativeDateTimeFormatter object specifying the format conventions.
0477  * @param relativeDateString
0478  *          The relative date string.
0479  * @param relativeDateStringLen
0480  *          The length of relativeDateString; may be -1 if relativeDateString
0481  *          is zero-terminated.
0482  * @param timeString
0483  *          The time string.
0484  * @param timeStringLen
0485  *          The length of timeString; may be -1 if timeString is zero-terminated.
0486  * @param result
0487  *          A pointer to a buffer to receive the formatted result.
0488  * @param resultCapacity
0489  *          The maximum size of result.
0490  * @param status
0491  *          A pointer to a UErrorCode to receive any errors. In case of error status,
0492  *          the contents of result are undefined.
0493  * @return
0494  *          The length of the formatted result; may be greater than resultCapacity,
0495  *          in which case an error is returned.
0496  * @stable ICU 57
0497  */
0498 U_CAPI int32_t U_EXPORT2
0499 ureldatefmt_combineDateAndTime( const URelativeDateTimeFormatter* reldatefmt,
0500                     const UChar *     relativeDateString,
0501                     int32_t           relativeDateStringLen,
0502                     const UChar *     timeString,
0503                     int32_t           timeStringLen,
0504                     UChar*            result,
0505                     int32_t           resultCapacity,
0506                     UErrorCode*       status );
0507 
0508 #endif /* !UCONFIG_NO_FORMATTING */
0509 
0510 #endif