tmutfmt.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 override;
  110. /**
  111. * Assignment operator
  112. * @deprecated ICU 53
  113. */
  114. TimeUnitFormat& operator=(const TimeUnitFormat& other);
  115. /**
  116. * Set the locale used for formatting or parsing.
  117. * @param locale the locale to be set
  118. * @param status output param set to success/failure code on exit
  119. * @deprecated ICU 53
  120. */
  121. void setLocale(const Locale& locale, UErrorCode& status);
  122. /**
  123. * Set the number format used for formatting or parsing.
  124. * @param format the number formatter to be set
  125. * @param status output param set to success/failure code on exit
  126. * @deprecated ICU 53
  127. */
  128. void setNumberFormat(const NumberFormat& format, UErrorCode& status);
  129. /**
  130. * Parse a TimeUnitAmount.
  131. * @see Format#parseObject(const UnicodeString&, Formattable&, ParsePosition&) const;
  132. * @deprecated ICU 53
  133. */
  134. virtual void parseObject(const UnicodeString& source,
  135. Formattable& result,
  136. ParsePosition& pos) const override;
  137. /**
  138. * Return the class ID for this class. This is useful only for comparing to
  139. * a return value from getDynamicClassID(). For example:
  140. * <pre>
  141. * . Base* polymorphic_pointer = createPolymorphicObject();
  142. * . if (polymorphic_pointer->getDynamicClassID() ==
  143. * . erived::getStaticClassID()) ...
  144. * </pre>
  145. * @return The class ID for all objects of this class.
  146. * @deprecated ICU 53
  147. */
  148. static UClassID U_EXPORT2 getStaticClassID();
  149. /**
  150. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
  151. * method is to implement a simple version of RTTI, since not all C++
  152. * compilers support genuine RTTI. Polymorphic operator==() and clone()
  153. * methods call this method.
  154. *
  155. * @return The class ID for this object. All objects of a
  156. * given class have the same class ID. Objects of
  157. * other classes have different class IDs.
  158. * @deprecated ICU 53
  159. */
  160. virtual UClassID getDynamicClassID() const override;
  161. private:
  162. Hashtable* fTimeUnitToCountToPatterns[TimeUnit::UTIMEUNIT_FIELD_COUNT];
  163. UTimeUnitFormatStyle fStyle;
  164. void create(UTimeUnitFormatStyle style, UErrorCode& status);
  165. // it might actually be simpler to make them Decimal Formats later.
  166. // initialize all private data members
  167. void setup(UErrorCode& status);
  168. // initialize data member without fill in data for fTimeUnitToCountToPattern
  169. void initDataMembers(UErrorCode& status);
  170. // initialize fTimeUnitToCountToPatterns from current locale's resource.
  171. void readFromCurrentLocale(UTimeUnitFormatStyle style, const char* key, const UVector& pluralCounts,
  172. UErrorCode& status);
  173. // check completeness of fTimeUnitToCountToPatterns against all time units,
  174. // and all plural rules, fill in fallback as necessary.
  175. void checkConsistency(UTimeUnitFormatStyle style, const char* key, UErrorCode& status);
  176. // fill in fTimeUnitToCountToPatterns from locale fall-back chain
  177. void searchInLocaleChain(UTimeUnitFormatStyle style, const char* key, const char* localeName,
  178. TimeUnit::UTimeUnitFields field, const UnicodeString&,
  179. const char*, Hashtable*, UErrorCode&);
  180. // initialize hash table
  181. Hashtable* initHash(UErrorCode& status);
  182. // delete hash table
  183. void deleteHash(Hashtable* htable);
  184. // copy hash table
  185. void copyHash(const Hashtable* source, Hashtable* target, UErrorCode& status);
  186. // get time unit name, such as "year", from time unit field enum, such as
  187. // UTIMEUNIT_YEAR.
  188. static const char* getTimeUnitName(TimeUnit::UTimeUnitFields field, UErrorCode& status);
  189. friend struct TimeUnitFormatReadSink;
  190. };
  191. U_NAMESPACE_END
  192. #endif /* U_HIDE_DEPRECATED_API */
  193. #endif /* #if !UCONFIG_NO_FORMATTING */
  194. #endif /* U_SHOW_CPLUSPLUS_API */
  195. #endif // __TMUTFMT_H__
  196. //eof