tmutfmt.h 7.8 KB

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