intl/icu/source/i18n/reldtfmt.h

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

     1 /*
     2 *******************************************************************************
     3 * Copyright (C) 2007-2012, International Business Machines Corporation and    *
     4 * others. All Rights Reserved.                                                *
     5 *******************************************************************************
     6 */
     8 #ifndef RELDTFMT_H
     9 #define RELDTFMT_H
    11 #include "unicode/utypes.h"
    13 /**
    14  * \file
    15  * \brief C++ API: Format and parse relative dates and times.
    16  */
    18 #if !UCONFIG_NO_FORMATTING
    20 #include "unicode/datefmt.h"
    21 #include "unicode/smpdtfmt.h"
    23 U_NAMESPACE_BEGIN
    25 // forward declarations
    26 class DateFormatSymbols;
    27 class MessageFormat;
    29 // internal structure used for caching strings
    30 struct URelativeString;
    32 /**
    33  * This class is normally accessed using the kRelative or k...Relative values of EStyle as
    34  * parameters to DateFormat::createDateInstance.
    35  *
    36  * Example:
    37  *     DateFormat *fullrelative = DateFormat::createDateInstance(DateFormat::kFullRelative, loc);
    38  *
    39  * @internal ICU 3.8
    40  */
    42 class RelativeDateFormat : public DateFormat {
    43 public:
    44     RelativeDateFormat( UDateFormatStyle timeStyle, UDateFormatStyle dateStyle, const Locale& locale, UErrorCode& status);
    46     // overrides
    47     /**
    48      * Copy constructor.
    49      * @internal ICU 3.8
    50      */
    51     RelativeDateFormat(const RelativeDateFormat&);
    53     /**
    54      * Assignment operator.
    55      * @internal ICU 3.8
    56      */
    57     RelativeDateFormat& operator=(const RelativeDateFormat&);
    59     /**
    60      * Destructor.
    61      * @internal ICU 3.8
    62      */
    63     virtual ~RelativeDateFormat();
    65     /**
    66      * Clone this Format object polymorphically. The caller owns the result and
    67      * should delete it when done.
    68      * @return    A copy of the object.
    69      * @internal ICU 3.8
    70      */
    71     virtual Format* clone(void) const;
    73     /**
    74      * Return true if the given Format objects are semantically equal. Objects
    75      * of different subclasses are considered unequal.
    76      * @param other    the object to be compared with.
    77      * @return         true if the given Format objects are semantically equal.
    78      * @internal ICU 3.8
    79      */
    80     virtual UBool operator==(const Format& other) const;
    83     using DateFormat::format;
    85     /**
    86      * Format a date or time, which is the standard millis since 24:00 GMT, Jan
    87      * 1, 1970. Overrides DateFormat pure virtual method.
    88      * <P>
    89      * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
    90      * 1996.07.10 AD at 15:08:56 PDT
    91      *
    92      * @param cal       Calendar set to the date and time to be formatted
    93      *                  into a date/time string.
    94      * @param appendTo  Output parameter to receive result.
    95      *                  Result is appended to existing contents.
    96      * @param pos       The formatting position. On input: an alignment field,
    97      *                  if desired. On output: the offsets of the alignment field.
    98      * @return          Reference to 'appendTo' parameter.
    99      * @internal ICU 3.8
   100      */
   101     virtual UnicodeString& format(  Calendar& cal,
   102                                     UnicodeString& appendTo,
   103                                     FieldPosition& pos) const;
   105     /**
   106      * Format an object to produce a string. This method handles Formattable
   107      * objects with a UDate type. If a the Formattable object type is not a Date,
   108      * then it returns a failing UErrorCode.
   109      *
   110      * @param obj       The object to format. Must be a Date.
   111      * @param appendTo  Output parameter to receive result.
   112      *                  Result is appended to existing contents.
   113      * @param pos       On input: an alignment field, if desired.
   114      *                  On output: the offsets of the alignment field.
   115      * @param status    Output param filled with success/failure status.
   116      * @return          Reference to 'appendTo' parameter.
   117      * @internal ICU 3.8
   118      */
   119     virtual UnicodeString& format(const Formattable& obj,
   120                                   UnicodeString& appendTo,
   121                                   FieldPosition& pos,
   122                                   UErrorCode& status) const;
   125     /**
   126      * Parse a date/time string beginning at the given parse position. For
   127      * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
   128      * that is equivalent to Date(837039928046).
   129      * <P>
   130      * By default, parsing is lenient: If the input is not in the form used by
   131      * this object's format method but can still be parsed as a date, then the
   132      * parse succeeds. Clients may insist on strict adherence to the format by
   133      * calling setLenient(false).
   134      *
   135      * @param text  The date/time string to be parsed
   136      * @param cal   a Calendar set to the date and time to be formatted
   137      *              into a date/time string.
   138      * @param pos   On input, the position at which to start parsing; on
   139      *              output, the position at which parsing terminated, or the
   140      *              start position if the parse failed.
   141      * @return      A valid UDate if the input could be parsed.
   142      * @internal ICU 3.8
   143      */
   144     virtual void parse( const UnicodeString& text,
   145                         Calendar& cal,
   146                         ParsePosition& pos) const;
   148     /**
   149      * Parse a date/time string starting at the given parse position. For
   150      * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
   151      * that is equivalent to Date(837039928046).
   152      * <P>
   153      * By default, parsing is lenient: If the input is not in the form used by
   154      * this object's format method but can still be parsed as a date, then the
   155      * parse succeeds. Clients may insist on strict adherence to the format by
   156      * calling setLenient(false).
   157      *
   158      * @see DateFormat::setLenient(boolean)
   159      *
   160      * @param text  The date/time string to be parsed
   161      * @param pos   On input, the position at which to start parsing; on
   162      *              output, the position at which parsing terminated, or the
   163      *              start position if the parse failed.
   164      * @return      A valid UDate if the input could be parsed.
   165      * @internal ICU 3.8
   166      */
   167     UDate parse( const UnicodeString& text,
   168                  ParsePosition& pos) const;
   171     /**
   172      * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT"
   173      * will be parsed into a UDate that is equivalent to Date(837039928046).
   174      * Parsing begins at the beginning of the string and proceeds as far as
   175      * possible.  Assuming no parse errors were encountered, this function
   176      * doesn't return any information about how much of the string was consumed
   177      * by the parsing.  If you need that information, use the version of
   178      * parse() that takes a ParsePosition.
   179      *
   180      * @param text  The date/time string to be parsed
   181      * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
   182      *              an error value if there was a parse error.
   183      * @return      A valid UDate if the input could be parsed.
   184      * @internal ICU 3.8
   185      */
   186     virtual UDate parse( const UnicodeString& text,
   187                         UErrorCode& status) const;
   189     /**
   190      * Return a single pattern string generated by combining the patterns for the
   191      * date and time formatters associated with this object.
   192      * @param result Output param to receive the pattern.
   193      * @return       A reference to 'result'.
   194      * @internal ICU 4.2 technology preview
   195      */
   196     virtual UnicodeString& toPattern(UnicodeString& result, UErrorCode& status) const;
   198     /**
   199      * Get the date pattern for the the date formatter associated with this object.
   200      * @param result Output param to receive the date pattern.
   201      * @return       A reference to 'result'.
   202      * @internal ICU 4.2 technology preview
   203      */
   204     virtual UnicodeString& toPatternDate(UnicodeString& result, UErrorCode& status) const;
   206     /**
   207      * Get the time pattern for the the time formatter associated with this object.
   208      * @param result Output param to receive the time pattern.
   209      * @return       A reference to 'result'.
   210      * @internal ICU 4.2 technology preview
   211      */
   212     virtual UnicodeString& toPatternTime(UnicodeString& result, UErrorCode& status) const;
   214     /**
   215      * Apply the given unlocalized date & time pattern strings to this relative date format.
   216      * (i.e., after this call, this formatter will format dates and times according to
   217      * the new patterns)
   218      *
   219      * @param datePattern   The date pattern to be applied.
   220      * @param timePattern   The time pattern to be applied.
   221      * @internal ICU 4.2 technology preview
   222      */
   223     virtual void applyPatterns(const UnicodeString& datePattern, const UnicodeString& timePattern, UErrorCode &status);
   225     /**
   226      * Gets the date/time formatting symbols (this is an object carrying
   227      * the various strings and other symbols used in formatting: e.g., month
   228      * names and abbreviations, time zone names, AM/PM strings, etc.)
   229      * @return a copy of the date-time formatting data associated
   230      * with this date-time formatter.
   231      * @internal ICU 4.8
   232      */
   233     virtual const DateFormatSymbols* getDateFormatSymbols(void) const;
   236 private:
   237     SimpleDateFormat *fDateTimeFormatter;
   238     UnicodeString fDatePattern;
   239     UnicodeString fTimePattern;
   240     MessageFormat *fCombinedFormat; //  the {0} {1} format.
   242     UDateFormatStyle fDateStyle;
   243     Locale  fLocale;
   245     int32_t fDayMin;    // day id of lowest #
   246     int32_t fDayMax;    // day id of highest #
   247     int32_t fDatesLen;    // Length of array
   248     URelativeString *fDates; // array of strings
   251     /**
   252      * Get the string at a specific offset.
   253      * @param day day offset ( -1, 0, 1, etc.. )
   254      * @param len on output, length of string.
   255      * @return the string, or NULL if none at that location.
   256      */
   257     const UChar *getStringForDay(int32_t day, int32_t &len, UErrorCode &status) const;
   259     /**
   260      * Load the Date string array
   261      */
   262     void loadDates(UErrorCode &status);
   264     /**
   265      * @return the number of days in "until-now"
   266      */
   267     static int32_t dayDifference(Calendar &until, UErrorCode &status);
   269     /**
   270      * initializes fCalendar from parameters.  Returns fCalendar as a convenience.
   271      * @param adoptZone  Zone to be adopted, or NULL for TimeZone::createDefault().
   272      * @param locale Locale of the calendar
   273      * @param status Error code
   274      * @return the newly constructed fCalendar
   275      * @internal ICU 3.8
   276      */
   277     Calendar* initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
   279 public:
   280     /**
   281      * Return the class ID for this class. This is useful only for comparing to
   282      * a return value from getDynamicClassID(). For example:
   283      * <pre>
   284      * .   Base* polymorphic_pointer = createPolymorphicObject();
   285      * .   if (polymorphic_pointer->getDynamicClassID() ==
   286      * .       erived::getStaticClassID()) ...
   287      * </pre>
   288      * @return          The class ID for all objects of this class.
   289      * @internal ICU 3.8
   290      */
   291     U_I18N_API static UClassID U_EXPORT2 getStaticClassID(void);
   293     /**
   294      * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
   295      * method is to implement a simple version of RTTI, since not all C++
   296      * compilers support genuine RTTI. Polymorphic operator==() and clone()
   297      * methods call this method.
   298      *
   299      * @return          The class ID for this object. All objects of a
   300      *                  given class have the same class ID.  Objects of
   301      *                  other classes have different class IDs.
   302      * @internal ICU 3.8
   303      */
   304     virtual UClassID getDynamicClassID(void) const;
   305 };
   308 U_NAMESPACE_END
   310 #endif /* #if !UCONFIG_NO_FORMATTING */
   312 #endif // RELDTFMT_H
   313 //eof

mercurial