2017-01-20 00:20:31 +00:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
2016-06-15 18:58:17 +00:00
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
2008-09-19 19:05:37 +00:00
|
|
|
/*
|
|
|
|
*******************************************************************************
|
2016-05-31 21:45:07 +00:00
|
|
|
* Copyright (C) 2008-2014, Google, International Business Machines Corporation
|
|
|
|
* and others. All Rights Reserved.
|
2008-09-19 19:05:37 +00:00
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __TMUTFMT_H__
|
|
|
|
#define __TMUTFMT_H__
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* \brief C++ API: Format and parse duration in single time unit
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_FORMATTING
|
2014-09-16 08:09:06 +00:00
|
|
|
#ifndef U_HIDE_DEPRECATED_API
|
2008-09-19 19:05:37 +00:00
|
|
|
|
|
|
|
#include "unicode/unistr.h"
|
|
|
|
#include "unicode/tmunit.h"
|
|
|
|
#include "unicode/tmutamt.h"
|
|
|
|
#include "unicode/measfmt.h"
|
|
|
|
#include "unicode/numfmt.h"
|
|
|
|
#include "unicode/plurrule.h"
|
|
|
|
|
2014-03-11 23:14:05 +00:00
|
|
|
|
2011-04-28 16:42:58 +00:00
|
|
|
/**
|
|
|
|
* Constants for various styles.
|
|
|
|
* There are 2 styles: full name and abbreviated name.
|
|
|
|
* For example, for English, the full name for hour duration is "3 hours",
|
|
|
|
* and the abbreviated name is "3 hrs".
|
2014-03-11 23:14:05 +00:00
|
|
|
* @deprecated ICU 53 Use MeasureFormat and UMeasureFormatWidth instead.
|
2011-04-28 16:42:58 +00:00
|
|
|
*/
|
|
|
|
enum UTimeUnitFormatStyle {
|
2014-03-11 23:14:05 +00:00
|
|
|
/** @deprecated ICU 53 */
|
2011-04-28 16:42:58 +00:00
|
|
|
UTMUTFMT_FULL_STYLE,
|
2014-03-11 23:14:05 +00:00
|
|
|
/** @deprecated ICU 53 */
|
2011-04-28 16:42:58 +00:00
|
|
|
UTMUTFMT_ABBREVIATED_STYLE,
|
2014-03-11 23:14:05 +00:00
|
|
|
/** @deprecated ICU 53 */
|
2011-04-28 16:42:58 +00:00
|
|
|
UTMUTFMT_FORMAT_STYLE_COUNT
|
|
|
|
};
|
2014-03-11 23:14:05 +00:00
|
|
|
typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @deprecated ICU 53 */
|
|
|
|
|
2011-04-28 16:42:58 +00:00
|
|
|
|
2008-09-19 19:05:37 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
class Hashtable;
|
2012-06-25 16:34:19 +00:00
|
|
|
class UVector;
|
2008-09-19 19:05:37 +00:00
|
|
|
|
2016-06-24 00:51:49 +00:00
|
|
|
struct TimeUnitFormatReadSink;
|
|
|
|
|
2008-09-19 19:05:37 +00:00
|
|
|
/**
|
|
|
|
* Format or parse a TimeUnitAmount, using plural rules for the units where available.
|
|
|
|
*
|
|
|
|
* <P>
|
2010-02-24 23:52:27 +00:00
|
|
|
* Code Sample:
|
2008-09-19 19:05:37 +00:00
|
|
|
* <pre>
|
|
|
|
* // create time unit amount instance - a combination of Number and time unit
|
|
|
|
* UErrorCode status = U_ZERO_ERROR;
|
|
|
|
* TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status);
|
|
|
|
* // create time unit format instance
|
|
|
|
* TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status);
|
|
|
|
* // format a time unit amount
|
|
|
|
* UnicodeString formatted;
|
|
|
|
* Formattable formattable;
|
|
|
|
* if (U_SUCCESS(status)) {
|
|
|
|
* formattable.adoptObject(source);
|
|
|
|
* formatted = ((Format*)format)->format(formattable, formatted, status);
|
|
|
|
* Formattable result;
|
|
|
|
* ((Format*)format)->parseObject(formatted, result, status);
|
|
|
|
* if (U_SUCCESS(status)) {
|
2010-02-24 23:52:27 +00:00
|
|
|
* assert (result == formattable);
|
2008-09-19 19:05:37 +00:00
|
|
|
* }
|
|
|
|
* }
|
|
|
|
* </pre>
|
|
|
|
*
|
|
|
|
* <P>
|
|
|
|
* @see TimeUnitAmount
|
|
|
|
* @see TimeUnitFormat
|
2014-03-11 23:14:05 +00:00
|
|
|
* @deprecated ICU 53 Use the MeasureFormat class instead.
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
class U_I18N_API TimeUnitFormat: public MeasureFormat {
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
2010-02-24 23:52:27 +00:00
|
|
|
* Create TimeUnitFormat with default locale, and full name style.
|
2008-09-19 19:05:37 +00:00
|
|
|
* Use setLocale and/or setFormat to modify.
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
TimeUnitFormat(UErrorCode& status);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create TimeUnitFormat given locale, and full name style.
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
TimeUnitFormat(const Locale& locale, UErrorCode& status);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create TimeUnitFormat given locale and style.
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
2011-04-28 16:42:58 +00:00
|
|
|
TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
|
2008-09-19 19:05:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Copy constructor.
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
TimeUnitFormat(const TimeUnitFormat&);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* deconstructor
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
virtual ~TimeUnitFormat();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clone this Format object polymorphically. The caller owns the result and
|
|
|
|
* should delete it when done.
|
|
|
|
* @return A copy of the object.
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
virtual Format* clone(void) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assignment operator
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
TimeUnitFormat& operator=(const TimeUnitFormat& other);
|
|
|
|
|
|
|
|
/**
|
2010-02-24 23:52:27 +00:00
|
|
|
* Return true if the given Format objects are not semantically equal.
|
2008-09-19 19:05:37 +00:00
|
|
|
* Objects of different subclasses are considered unequal.
|
|
|
|
* @param other the object to be compared with.
|
|
|
|
* @return true if the given Format objects are not semantically equal.
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
UBool operator!=(const Format& other) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the locale used for formatting or parsing.
|
|
|
|
* @param locale the locale to be set
|
|
|
|
* @param status output param set to success/failure code on exit
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
void setLocale(const Locale& locale, UErrorCode& status);
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2010-02-24 23:52:27 +00:00
|
|
|
* Set the number format used for formatting or parsing.
|
2008-09-19 19:05:37 +00:00
|
|
|
* @param format the number formatter to be set
|
|
|
|
* @param status output param set to success/failure code on exit
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
void setNumberFormat(const NumberFormat& format, UErrorCode& status);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse a TimeUnitAmount.
|
|
|
|
* @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const;
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
2010-02-24 23:52:27 +00:00
|
|
|
virtual void parseObject(const UnicodeString& source,
|
2008-09-19 19:05:37 +00:00
|
|
|
Formattable& result,
|
|
|
|
ParsePosition& pos) const;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the class ID for this class. This is useful only for comparing to
|
|
|
|
* a return value from getDynamicClassID(). For example:
|
|
|
|
* <pre>
|
|
|
|
* . Base* polymorphic_pointer = createPolymorphicObject();
|
|
|
|
* . if (polymorphic_pointer->getDynamicClassID() ==
|
|
|
|
* . erived::getStaticClassID()) ...
|
|
|
|
* </pre>
|
|
|
|
* @return The class ID for all objects of this class.
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
static UClassID U_EXPORT2 getStaticClassID(void);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
|
|
|
|
* method is to implement a simple version of RTTI, since not all C++
|
|
|
|
* compilers support genuine RTTI. Polymorphic operator==() and clone()
|
|
|
|
* methods call this method.
|
|
|
|
*
|
|
|
|
* @return The class ID for this object. All objects of a
|
|
|
|
* given class have the same class ID. Objects of
|
|
|
|
* other classes have different class IDs.
|
2014-09-16 08:09:06 +00:00
|
|
|
* @deprecated ICU 53
|
2008-09-19 19:05:37 +00:00
|
|
|
*/
|
|
|
|
virtual UClassID getDynamicClassID(void) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT];
|
2011-04-28 17:31:44 +00:00
|
|
|
UTimeUnitFormatStyle fStyle;
|
2008-09-19 19:05:37 +00:00
|
|
|
|
2014-02-04 00:29:17 +00:00
|
|
|
void create(UTimeUnitFormatStyle style, UErrorCode& status);
|
2008-09-19 19:05:37 +00:00
|
|
|
|
|
|
|
// it might actually be simpler to make them Decimal Formats later.
|
|
|
|
// initialize all private data members
|
2010-02-24 23:52:27 +00:00
|
|
|
void setup(UErrorCode& status);
|
|
|
|
|
2008-09-19 19:05:37 +00:00
|
|
|
// initialize data member without fill in data for fTimeUnitToCountToPattern
|
|
|
|
void initDataMembers(UErrorCode& status);
|
|
|
|
|
|
|
|
// initialize fTimeUnitToCountToPatterns from current locale's resource.
|
2012-06-08 19:02:03 +00:00
|
|
|
void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts,
|
|
|
|
UErrorCode& status);
|
2008-09-19 19:05:37 +00:00
|
|
|
|
|
|
|
// check completeness of fTimeUnitToCountToPatterns against all time units,
|
|
|
|
// and all plural rules, fill in fallback as necessary.
|
2011-04-28 16:42:58 +00:00
|
|
|
void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status);
|
2008-09-19 19:05:37 +00:00
|
|
|
|
|
|
|
// fill in fTimeUnitToCountToPatterns from locale fall-back chain
|
2011-04-28 16:42:58 +00:00
|
|
|
void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
|
2011-06-03 05:23:57 +00:00
|
|
|
TimeUnit::UTimeUnitFields field, const UnicodeString&,
|
2008-09-19 19:05:37 +00:00
|
|
|
const char*, Hashtable*, UErrorCode&);
|
|
|
|
|
|
|
|
// initialize hash table
|
|
|
|
Hashtable* initHash(UErrorCode& status);
|
|
|
|
|
|
|
|
// delete hash table
|
|
|
|
void deleteHash(Hashtable* htable);
|
|
|
|
|
|
|
|
// copy hash table
|
|
|
|
void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status);
|
|
|
|
// get time unit name, such as "year", from time unit field enum, such as
|
|
|
|
// UTIMEUNIT_YEAR.
|
|
|
|
static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status);
|
2013-07-25 16:36:25 +00:00
|
|
|
|
2016-06-24 19:50:12 +00:00
|
|
|
friend struct TimeUnitFormatReadSink;
|
2008-09-19 19:05:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
inline UBool
|
|
|
|
TimeUnitFormat::operator!=(const Format& other) const {
|
|
|
|
return !operator==(other);
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
2014-09-16 08:09:06 +00:00
|
|
|
#endif /* U_HIDE_DEPRECATED_API */
|
2008-09-19 19:05:37 +00:00
|
|
|
#endif /* #if !UCONFIG_NO_FORMATTING */
|
|
|
|
|
|
|
|
#endif // __TMUTFMT_H__
|
|
|
|
//eof
|