tmutfmt.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. *******************************************************************************
  5. * Copyright (C) 2008-2014, Google, International Business Machines Corporation
  6. * and others. All Rights Reserved.
  7. *******************************************************************************
  8. */
  9. #ifndef __TMUTFMT_H__
  10. #define __TMUTFMT_H__
  11. #include "unicode/utypes.h"
  12. /**
  13. * \file
  14. * \brief C++ API: Format and parse duration in single time unit
  15. */
  16. #if U_SHOW_CPLUSPLUS_API
  17. #if !UCONFIG_NO_FORMATTING
  18. #include "unicode/unistr.h"
  19. #include "unicode/tmunit.h"
  20. #include "unicode/tmutamt.h"
  21. #include "unicode/measfmt.h"
  22. #include "unicode/numfmt.h"
  23. #include "unicode/plurrule.h"
  24. #ifndef U_HIDE_DEPRECATED_API
  25. /**
  26. * Constants for various styles.
  27. * There are 2 styles: full name and abbreviated name.
  28. * For example, for English, the full name for hour duration is "3 hours",
  29. * and the abbreviated name is "3 hrs".
  30. * @deprecated ICU 53 Use MeasureFormat and UMeasureFormatWidth instead.
  31. */
  32. enum UTimeUnitFormatStyle {
  33. /** @deprecated ICU 53 */
  34. UTMUTFMT_FULL_STYLE,
  35. /** @deprecated ICU 53 */
  36. UTMUTFMT_ABBREVIATED_STYLE,
  37. /** @deprecated ICU 53 */
  38. UTMUTFMT_FORMAT_STYLE_COUNT
  39. };
  40. typedef enum UTimeUnitFormatStyle UTimeUnitFormatStyle; /**< @deprecated ICU 53 */
  41. U_NAMESPACE_BEGIN
  42. class Hashtable;
  43. class UVector;
  44. struct TimeUnitFormatReadSink;
  45. /**
  46. * Format or parse a TimeUnitAmount, using plural rules for the units where available.
  47. *
  48. * <P>
  49. * Code Sample:
  50. * <pre>
  51. * // create time unit amount instance - a combination of Number and time unit
  52. * UErrorCode status = U_ZERO_ERROR;
  53. * TimeUnitAmount* source = new TimeUnitAmount(2, TimeUnit::UTIMEUNIT_YEAR, status);
  54. * // create time unit format instance
  55. * TimeUnitFormat* format = new TimeUnitFormat(Locale("en"), status);
  56. * // format a time unit amount
  57. * UnicodeString formatted;
  58. * Formattable formattable;
  59. * if (U_SUCCESS(status)) {
  60. * formattable.adoptObject(source);
  61. * formatted = ((Format*)format)->format(formattable, formatted, status);
  62. * Formattable result;
  63. * ((Format*)format)->parseObject(formatted, result, status);
  64. * if (U_SUCCESS(status)) {
  65. * assert (result == formattable);
  66. * }
  67. * }
  68. * </pre>
  69. *
  70. * <P>
  71. * @see TimeUnitAmount
  72. * @see TimeUnitFormat
  73. * @deprecated ICU 53 Use the MeasureFormat class instead.
  74. */
  75. class U_I18N_API TimeUnitFormat: public MeasureFormat {
  76. public:
  77. /**
  78. * Create TimeUnitFormat with default locale, and full name style.
  79. * Use setLocale and/or setFormat to modify.
  80. * @deprecated ICU 53
  81. */
  82. TimeUnitFormat(UErrorCode& status);
  83. /**
  84. * Create TimeUnitFormat given locale, and full name style.
  85. * @deprecated ICU 53
  86. */
  87. TimeUnitFormat(const Locale& locale, UErrorCode& status);
  88. /**
  89. * Create TimeUnitFormat given locale and style.
  90. * @deprecated ICU 53
  91. */
  92. TimeUnitFormat(const Locale& locale, UTimeUnitFormatStyle style, UErrorCode& status);
  93. /**
  94. * Copy constructor.
  95. * @deprecated ICU 53
  96. */
  97. TimeUnitFormat(const TimeUnitFormat&);
  98. /**
  99. * deconstructor
  100. * @deprecated ICU 53
  101. */
  102. virtual ~TimeUnitFormat();
  103. /**
  104. * Clone this Format object polymorphically. The caller owns the result and
  105. * should delete it when done.
  106. * @return A copy of the object.
  107. * @deprecated ICU 53
  108. */
  109. virtual TimeUnitFormat* clone() const;
  110. /**
  111. * Assignment operator
  112. * @deprecated ICU 53
  113. */
  114. TimeUnitFormat& operator=(const TimeUnitFormat& other);
  115. /**
  116. * Return true if the given Format objects are not semantically equal.
  117. * Objects of different subclasses are considered unequal.
  118. * @param other the object to be compared with.
  119. * @return true if the given Format objects are not semantically equal.
  120. * @deprecated ICU 53
  121. */
  122. UBool operator!=(const Format& other) const;
  123. /**
  124. * Set the locale used for formatting or parsing.
  125. * @param locale the locale to be set
  126. * @param status output param set to success/failure code on exit
  127. * @deprecated ICU 53
  128. */
  129. void setLocale(const Locale& locale, UErrorCode& status);
  130. /**
  131. * Set the number format used for formatting or parsing.
  132. * @param format the number formatter to be set
  133. * @param status output param set to success/failure code on exit
  134. * @deprecated ICU 53
  135. */
  136. void setNumberFormat(const NumberFormat& format, UErrorCode& status);
  137. /**
  138. * Parse a TimeUnitAmount.
  139. * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const;
  140. * @deprecated ICU 53
  141. */
  142. virtual void parseObject(const UnicodeString& source,
  143. Formattable& result,
  144. ParsePosition& pos) const;
  145. /**
  146. * Return the class ID for this class. This is useful only for comparing to
  147. * a return value from getDynamicClassID(). For example:
  148. * <pre>
  149. * . Base* polymorphic_pointer = createPolymorphicObject();
  150. * . if (polymorphic_pointer->getDynamicClassID() ==
  151. * . erived::getStaticClassID()) ...
  152. * </pre>
  153. * @return The class ID for all objects of this class.
  154. * @deprecated ICU 53
  155. */
  156. static UClassID U_EXPORT2 getStaticClassID(void);
  157. /**
  158. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
  159. * method is to implement a simple version of RTTI, since not all C++
  160. * compilers support genuine RTTI. Polymorphic operator==() and clone()
  161. * methods call this method.
  162. *
  163. * @return The class ID for this object. All objects of a
  164. * given class have the same class ID. Objects of
  165. * other classes have different class IDs.
  166. * @deprecated ICU 53
  167. */
  168. virtual UClassID getDynamicClassID(void) const;
  169. private:
  170. Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT];
  171. UTimeUnitFormatStyle fStyle;
  172. void create(UTimeUnitFormatStyle style, UErrorCode& status);
  173. // it might actually be simpler to make them Decimal Formats later.
  174. // initialize all private data members
  175. void setup(UErrorCode& status);
  176. // initialize data member without fill in data for fTimeUnitToCountToPattern
  177. void initDataMembers(UErrorCode& status);
  178. // initialize fTimeUnitToCountToPatterns from current locale's resource.
  179. void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts,
  180. UErrorCode& status);
  181. // check completeness of fTimeUnitToCountToPatterns against all time units,
  182. // and all plural rules, fill in fallback as necessary.
  183. void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status);
  184. // fill in fTimeUnitToCountToPatterns from locale fall-back chain
  185. void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
  186. TimeUnit::UTimeUnitFields field, const UnicodeString&,
  187. const char*, Hashtable*, UErrorCode&);
  188. // initialize hash table
  189. Hashtable* initHash(UErrorCode& status);
  190. // delete hash table
  191. void deleteHash(Hashtable* htable);
  192. // copy hash table
  193. void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status);
  194. // get time unit name, such as "year", from time unit field enum, such as
  195. // UTIMEUNIT_YEAR.
  196. static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status);
  197. friend struct TimeUnitFormatReadSink;
  198. };
  199. inline UBool
  200. TimeUnitFormat::operator!=(const Format& other) const {
  201. return !operator==(other);
  202. }
  203. U_NAMESPACE_END
  204. #endif /* U_HIDE_DEPRECATED_API */
  205. #endif /* #if !UCONFIG_NO_FORMATTING */
  206. #endif /* U_SHOW_CPLUSPLUS_API */
  207. #endif // __TMUTFMT_H__
  208. //eof