reldtfmt.h 13 KB

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