dtitvfmt.h 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /********************************************************************************
  4. * Copyright (C) 2008-2016, International Business Machines Corporation and
  5. * others. All Rights Reserved.
  6. *******************************************************************************
  7. *
  8. * File DTITVFMT.H
  9. *
  10. *******************************************************************************
  11. */
  12. #ifndef __DTITVFMT_H__
  13. #define __DTITVFMT_H__
  14. #include "unicode/utypes.h"
  15. #if U_SHOW_CPLUSPLUS_API
  16. /**
  17. * \file
  18. * \brief C++ API: Format and parse date interval in a language-independent manner.
  19. */
  20. #if !UCONFIG_NO_FORMATTING
  21. #include "unicode/ucal.h"
  22. #include "unicode/smpdtfmt.h"
  23. #include "unicode/dtintrv.h"
  24. #include "unicode/dtitvinf.h"
  25. #include "unicode/dtptngen.h"
  26. #include "unicode/formattedvalue.h"
  27. U_NAMESPACE_BEGIN
  28. class FormattedDateIntervalData;
  29. class DateIntervalFormat;
  30. /**
  31. * An immutable class containing the result of a date interval formatting operation.
  32. *
  33. * Instances of this class are immutable and thread-safe.
  34. *
  35. * When calling nextPosition():
  36. * The fields are returned from left to right. The special field category
  37. * UFIELD_CATEGORY_DATE_INTERVAL_SPAN is used to indicate which datetime
  38. * primitives came from which arguments: 0 means fromCalendar, and 1 means
  39. * toCalendar. The span category will always occur before the
  40. * corresponding fields in UFIELD_CATEGORY_DATE
  41. * in the nextPosition() iterator.
  42. *
  43. * Not intended for public subclassing.
  44. *
  45. * @stable ICU 64
  46. */
  47. class U_I18N_API FormattedDateInterval : public UMemory, public FormattedValue {
  48. public:
  49. /**
  50. * Default constructor; makes an empty FormattedDateInterval.
  51. * @stable ICU 64
  52. */
  53. FormattedDateInterval() : fData(nullptr), fErrorCode(U_INVALID_STATE_ERROR) {}
  54. /**
  55. * Move constructor: Leaves the source FormattedDateInterval in an undefined state.
  56. * @stable ICU 64
  57. */
  58. FormattedDateInterval(FormattedDateInterval&& src) U_NOEXCEPT;
  59. /**
  60. * Destruct an instance of FormattedDateInterval.
  61. * @stable ICU 64
  62. */
  63. virtual ~FormattedDateInterval() U_OVERRIDE;
  64. /** Copying not supported; use move constructor instead. */
  65. FormattedDateInterval(const FormattedDateInterval&) = delete;
  66. /** Copying not supported; use move assignment instead. */
  67. FormattedDateInterval& operator=(const FormattedDateInterval&) = delete;
  68. /**
  69. * Move assignment: Leaves the source FormattedDateInterval in an undefined state.
  70. * @stable ICU 64
  71. */
  72. FormattedDateInterval& operator=(FormattedDateInterval&& src) U_NOEXCEPT;
  73. /** @copydoc FormattedValue::toString() */
  74. UnicodeString toString(UErrorCode& status) const U_OVERRIDE;
  75. /** @copydoc FormattedValue::toTempString() */
  76. UnicodeString toTempString(UErrorCode& status) const U_OVERRIDE;
  77. /** @copydoc FormattedValue::appendTo() */
  78. Appendable &appendTo(Appendable& appendable, UErrorCode& status) const U_OVERRIDE;
  79. /** @copydoc FormattedValue::nextPosition() */
  80. UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const U_OVERRIDE;
  81. private:
  82. FormattedDateIntervalData *fData;
  83. UErrorCode fErrorCode;
  84. explicit FormattedDateInterval(FormattedDateIntervalData *results)
  85. : fData(results), fErrorCode(U_ZERO_ERROR) {}
  86. explicit FormattedDateInterval(UErrorCode errorCode)
  87. : fData(nullptr), fErrorCode(errorCode) {}
  88. friend class DateIntervalFormat;
  89. };
  90. /**
  91. * DateIntervalFormat is a class for formatting and parsing date
  92. * intervals in a language-independent manner.
  93. * Only formatting is supported, parsing is not supported.
  94. *
  95. * <P>
  96. * Date interval means from one date to another date,
  97. * for example, from "Jan 11, 2008" to "Jan 18, 2008".
  98. * We introduced class DateInterval to represent it.
  99. * DateInterval is a pair of UDate, which is
  100. * the standard milliseconds since 24:00 GMT, Jan 1, 1970.
  101. *
  102. * <P>
  103. * DateIntervalFormat formats a DateInterval into
  104. * text as compactly as possible.
  105. * For example, the date interval format from "Jan 11, 2008" to "Jan 18,. 2008"
  106. * is "Jan 11-18, 2008" for English.
  107. * And it parses text into DateInterval,
  108. * although initially, parsing is not supported.
  109. *
  110. * <P>
  111. * There is no structural information in date time patterns.
  112. * For any punctuations and string literals inside a date time pattern,
  113. * we do not know whether it is just a separator, or a prefix, or a suffix.
  114. * Without such information, so, it is difficult to generate a sub-pattern
  115. * (or super-pattern) by algorithm.
  116. * So, formatting a DateInterval is pattern-driven. It is very
  117. * similar to formatting in SimpleDateFormat.
  118. * We introduce class DateIntervalInfo to save date interval
  119. * patterns, similar to date time pattern in SimpleDateFormat.
  120. *
  121. * <P>
  122. * Logically, the interval patterns are mappings
  123. * from (skeleton, the_largest_different_calendar_field)
  124. * to (date_interval_pattern).
  125. *
  126. * <P>
  127. * A skeleton
  128. * <ol>
  129. * <li>
  130. * only keeps the field pattern letter and ignores all other parts
  131. * in a pattern, such as space, punctuations, and string literals.
  132. * </li>
  133. * <li>
  134. * hides the order of fields.
  135. * </li>
  136. * <li>
  137. * might hide a field's pattern letter length.
  138. * </li>
  139. * </ol>
  140. *
  141. * For those non-digit calendar fields, the pattern letter length is
  142. * important, such as MMM, MMMM, and MMMMM; EEE and EEEE,
  143. * and the field's pattern letter length is honored.
  144. *
  145. * For the digit calendar fields, such as M or MM, d or dd, yy or yyyy,
  146. * the field pattern length is ignored and the best match, which is defined
  147. * in date time patterns, will be returned without honor the field pattern
  148. * letter length in skeleton.
  149. *
  150. * <P>
  151. * The calendar fields we support for interval formatting are:
  152. * year, month, date, day-of-week, am-pm, hour, hour-of-day, minute, second,
  153. * and millisecond.
  154. * (though we do not currently have specific intervalFormat date for skeletons
  155. * with seconds and millisecond).
  156. * Those calendar fields can be defined in the following order:
  157. * year > month > date > hour (in day) > minute > second > millisecond
  158. *
  159. * The largest different calendar fields between 2 calendars is the
  160. * first different calendar field in above order.
  161. *
  162. * For example: the largest different calendar fields between "Jan 10, 2007"
  163. * and "Feb 20, 2008" is year.
  164. *
  165. * <P>
  166. * For other calendar fields, the compact interval formatting is not
  167. * supported. And the interval format will be fall back to fall-back
  168. * patterns, which is mostly "{date0} - {date1}".
  169. *
  170. * <P>
  171. * There is a set of pre-defined static skeleton strings.
  172. * There are pre-defined interval patterns for those pre-defined skeletons
  173. * in locales' resource files.
  174. * For example, for a skeleton UDAT_YEAR_ABBR_MONTH_DAY, which is &quot;yMMMd&quot;,
  175. * in en_US, if the largest different calendar field between date1 and date2
  176. * is &quot;year&quot;, the date interval pattern is &quot;MMM d, yyyy - MMM d, yyyy&quot;,
  177. * such as &quot;Jan 10, 2007 - Jan 10, 2008&quot;.
  178. * If the largest different calendar field between date1 and date2 is &quot;month&quot;,
  179. * the date interval pattern is &quot;MMM d - MMM d, yyyy&quot;,
  180. * such as &quot;Jan 10 - Feb 10, 2007&quot;.
  181. * If the largest different calendar field between date1 and date2 is &quot;day&quot;,
  182. * the date interval pattern is &quot;MMM d-d, yyyy&quot;, such as &quot;Jan 10-20, 2007&quot;.
  183. *
  184. * For date skeleton, the interval patterns when year, or month, or date is
  185. * different are defined in resource files.
  186. * For time skeleton, the interval patterns when am/pm, or hour, or minute is
  187. * different are defined in resource files.
  188. *
  189. * <P>
  190. * If a skeleton is not found in a locale's DateIntervalInfo, which means
  191. * the interval patterns for the skeleton is not defined in resource file,
  192. * the interval pattern will falls back to the interval "fallback" pattern
  193. * defined in resource file.
  194. * If the interval "fallback" pattern is not defined, the default fall-back
  195. * is "{date0} - {data1}".
  196. *
  197. * <P>
  198. * For the combination of date and time,
  199. * The rule to generate interval patterns are:
  200. * <ol>
  201. * <li>
  202. * when the year, month, or day differs, falls back to fall-back
  203. * interval pattern, which mostly is the concatenate the two original
  204. * expressions with a separator between,
  205. * For example, interval pattern from "Jan 10, 2007 10:10 am"
  206. * to "Jan 11, 2007 10:10am" is
  207. * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
  208. * </li>
  209. * <li>
  210. * otherwise, present the date followed by the range expression
  211. * for the time.
  212. * For example, interval pattern from "Jan 10, 2007 10:10 am"
  213. * to "Jan 10, 2007 11:10am" is "Jan 10, 2007 10:10 am - 11:10am"
  214. * </li>
  215. * </ol>
  216. *
  217. *
  218. * <P>
  219. * If two dates are the same, the interval pattern is the single date pattern.
  220. * For example, interval pattern from "Jan 10, 2007" to "Jan 10, 2007" is
  221. * "Jan 10, 2007".
  222. *
  223. * Or if the presenting fields between 2 dates have the exact same values,
  224. * the interval pattern is the single date pattern.
  225. * For example, if user only requests year and month,
  226. * the interval pattern from "Jan 10, 2007" to "Jan 20, 2007" is "Jan 2007".
  227. *
  228. * <P>
  229. * DateIntervalFormat needs the following information for correct
  230. * formatting: time zone, calendar type, pattern, date format symbols,
  231. * and date interval patterns.
  232. * It can be instantiated in 2 ways:
  233. * <ol>
  234. * <li>
  235. * create an instance using default or given locale plus given skeleton.
  236. * Users are encouraged to created date interval formatter this way and
  237. * to use the pre-defined skeleton macros, such as
  238. * UDAT_YEAR_NUM_MONTH, which consists the calendar fields and
  239. * the format style.
  240. * </li>
  241. * <li>
  242. * create an instance using default or given locale plus given skeleton
  243. * plus a given DateIntervalInfo.
  244. * This factory method is for powerful users who want to provide their own
  245. * interval patterns.
  246. * Locale provides the timezone, calendar, and format symbols information.
  247. * Local plus skeleton provides full pattern information.
  248. * DateIntervalInfo provides the date interval patterns.
  249. * </li>
  250. * </ol>
  251. *
  252. * <P>
  253. * For the calendar field pattern letter, such as G, y, M, d, a, h, H, m, s etc.
  254. * DateIntervalFormat uses the same syntax as that of
  255. * DateTime format.
  256. *
  257. * <P>
  258. * Code Sample: general usage
  259. * <pre>
  260. * \code
  261. * // the date interval object which the DateIntervalFormat formats on
  262. * // and parses into
  263. * DateInterval* dtInterval = new DateInterval(1000*3600*24, 1000*3600*24*2);
  264. * UErrorCode status = U_ZERO_ERROR;
  265. * DateIntervalFormat* dtIntervalFmt = DateIntervalFormat::createInstance(
  266. * UDAT_YEAR_MONTH_DAY,
  267. * Locale("en", "GB", ""), status);
  268. * UnicodeUnicodeString dateIntervalString;
  269. * FieldPosition pos = 0;
  270. * // formatting
  271. * dtIntervalFmt->format(dtInterval, dateIntervalUnicodeString, pos, status);
  272. * delete dtIntervalFmt;
  273. * \endcode
  274. * </pre>
  275. */
  276. class U_I18N_API DateIntervalFormat : public Format {
  277. public:
  278. /**
  279. * Construct a DateIntervalFormat from skeleton and the default locale.
  280. *
  281. * This is a convenient override of
  282. * createInstance(const UnicodeString& skeleton, const Locale& locale,
  283. * UErrorCode&)
  284. * with the value of locale as default locale.
  285. *
  286. * @param skeleton the skeleton on which interval format based.
  287. * @param status output param set to success/failure code on exit
  288. * @return a date time interval formatter which the caller owns.
  289. * @stable ICU 4.0
  290. */
  291. static DateIntervalFormat* U_EXPORT2 createInstance(
  292. const UnicodeString& skeleton,
  293. UErrorCode& status);
  294. /**
  295. * Construct a DateIntervalFormat from skeleton and a given locale.
  296. * <P>
  297. * In this factory method,
  298. * the date interval pattern information is load from resource files.
  299. * Users are encouraged to created date interval formatter this way and
  300. * to use the pre-defined skeleton macros.
  301. *
  302. * <P>
  303. * There are pre-defined skeletons (defined in udate.h) having predefined
  304. * interval patterns in resource files.
  305. * Users are encouraged to use those macros.
  306. * For example:
  307. * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
  308. *
  309. * The given Locale provides the interval patterns.
  310. * For example, for en_GB, if skeleton is UDAT_YEAR_ABBR_MONTH_WEEKDAY_DAY,
  311. * which is "yMMMEEEd",
  312. * the interval patterns defined in resource file to above skeleton are:
  313. * "EEE, d MMM, yyyy - EEE, d MMM, yyyy" for year differs,
  314. * "EEE, d MMM - EEE, d MMM, yyyy" for month differs,
  315. * "EEE, d - EEE, d MMM, yyyy" for day differs,
  316. * @param skeleton the skeleton on which the interval format is based.
  317. * @param locale the given locale
  318. * @param status output param set to success/failure code on exit
  319. * @return a date time interval formatter which the caller owns.
  320. * @stable ICU 4.0
  321. * <p>
  322. * <h4>Sample code</h4>
  323. * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
  324. * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined
  325. * <p>
  326. */
  327. static DateIntervalFormat* U_EXPORT2 createInstance(
  328. const UnicodeString& skeleton,
  329. const Locale& locale,
  330. UErrorCode& status);
  331. /**
  332. * Construct a DateIntervalFormat from skeleton
  333. * DateIntervalInfo, and default locale.
  334. *
  335. * This is a convenient override of
  336. * createInstance(const UnicodeString& skeleton, const Locale& locale,
  337. * const DateIntervalInfo& dtitvinf, UErrorCode&)
  338. * with the locale value as default locale.
  339. *
  340. * @param skeleton the skeleton on which interval format based.
  341. * @param dtitvinf the DateIntervalInfo object.
  342. * @param status output param set to success/failure code on exit
  343. * @return a date time interval formatter which the caller owns.
  344. * @stable ICU 4.0
  345. */
  346. static DateIntervalFormat* U_EXPORT2 createInstance(
  347. const UnicodeString& skeleton,
  348. const DateIntervalInfo& dtitvinf,
  349. UErrorCode& status);
  350. /**
  351. * Construct a DateIntervalFormat from skeleton
  352. * a DateIntervalInfo, and the given locale.
  353. *
  354. * <P>
  355. * In this factory method, user provides its own date interval pattern
  356. * information, instead of using those pre-defined data in resource file.
  357. * This factory method is for powerful users who want to provide their own
  358. * interval patterns.
  359. * <P>
  360. * There are pre-defined skeletons (defined in udate.h) having predefined
  361. * interval patterns in resource files.
  362. * Users are encouraged to use those macros.
  363. * For example:
  364. * DateIntervalFormat::createInstance(UDAT_MONTH_DAY, status)
  365. *
  366. * The DateIntervalInfo provides the interval patterns.
  367. * and the DateIntervalInfo ownership remains to the caller.
  368. *
  369. * User are encouraged to set default interval pattern in DateIntervalInfo
  370. * as well, if they want to set other interval patterns ( instead of
  371. * reading the interval patterns from resource files).
  372. * When the corresponding interval pattern for a largest calendar different
  373. * field is not found ( if user not set it ), interval format fallback to
  374. * the default interval pattern.
  375. * If user does not provide default interval pattern, it fallback to
  376. * "{date0} - {date1}"
  377. *
  378. * @param skeleton the skeleton on which interval format based.
  379. * @param locale the given locale
  380. * @param dtitvinf the DateIntervalInfo object.
  381. * @param status output param set to success/failure code on exit
  382. * @return a date time interval formatter which the caller owns.
  383. * @stable ICU 4.0
  384. * <p>
  385. * <h4>Sample code</h4>
  386. * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtPreDefined1
  387. * \snippet samples/dtitvfmtsample/dtitvfmtsample.cpp dtitvfmtCustomized
  388. * <p>
  389. */
  390. static DateIntervalFormat* U_EXPORT2 createInstance(
  391. const UnicodeString& skeleton,
  392. const Locale& locale,
  393. const DateIntervalInfo& dtitvinf,
  394. UErrorCode& status);
  395. /**
  396. * Destructor.
  397. * @stable ICU 4.0
  398. */
  399. virtual ~DateIntervalFormat();
  400. /**
  401. * Clone this Format object polymorphically. The caller owns the result and
  402. * should delete it when done.
  403. * @return A copy of the object.
  404. * @stable ICU 4.0
  405. */
  406. virtual DateIntervalFormat* clone() const;
  407. /**
  408. * Return true if the given Format objects are semantically equal. Objects
  409. * of different subclasses are considered unequal.
  410. * @param other the object to be compared with.
  411. * @return true if the given Format objects are semantically equal.
  412. * @stable ICU 4.0
  413. */
  414. virtual UBool operator==(const Format& other) const;
  415. /**
  416. * Return true if the given Format objects are not semantically equal.
  417. * Objects of different subclasses are considered unequal.
  418. * @param other the object to be compared with.
  419. * @return true if the given Format objects are not semantically equal.
  420. * @stable ICU 4.0
  421. */
  422. UBool operator!=(const Format& other) const;
  423. using Format::format;
  424. /**
  425. * Format an object to produce a string. This method handles Formattable
  426. * objects with a DateInterval type.
  427. * If a the Formattable object type is not a DateInterval,
  428. * then it returns a failing UErrorCode.
  429. *
  430. * @param obj The object to format.
  431. * Must be a DateInterval.
  432. * @param appendTo Output parameter to receive result.
  433. * Result is appended to existing contents.
  434. * @param fieldPosition On input: an alignment field, if desired.
  435. * On output: the offsets of the alignment field.
  436. * There may be multiple instances of a given field type
  437. * in an interval format; in this case the fieldPosition
  438. * offsets refer to the first instance.
  439. * @param status Output param filled with success/failure status.
  440. * @return Reference to 'appendTo' parameter.
  441. * @stable ICU 4.0
  442. */
  443. virtual UnicodeString& format(const Formattable& obj,
  444. UnicodeString& appendTo,
  445. FieldPosition& fieldPosition,
  446. UErrorCode& status) const ;
  447. /**
  448. * Format a DateInterval to produce a string.
  449. *
  450. * @param dtInterval DateInterval to be formatted.
  451. * @param appendTo Output parameter to receive result.
  452. * Result is appended to existing contents.
  453. * @param fieldPosition On input: an alignment field, if desired.
  454. * On output: the offsets of the alignment field.
  455. * There may be multiple instances of a given field type
  456. * in an interval format; in this case the fieldPosition
  457. * offsets refer to the first instance.
  458. * @param status Output param filled with success/failure status.
  459. * @return Reference to 'appendTo' parameter.
  460. * @stable ICU 4.0
  461. */
  462. UnicodeString& format(const DateInterval* dtInterval,
  463. UnicodeString& appendTo,
  464. FieldPosition& fieldPosition,
  465. UErrorCode& status) const ;
  466. /**
  467. * Format a DateInterval to produce a FormattedDateInterval.
  468. *
  469. * The FormattedDateInterval exposes field information about the formatted string.
  470. *
  471. * @param dtInterval DateInterval to be formatted.
  472. * @param status Set if an error occurs.
  473. * @return A FormattedDateInterval containing the format result.
  474. * @stable ICU 64
  475. */
  476. FormattedDateInterval formatToValue(
  477. const DateInterval& dtInterval,
  478. UErrorCode& status) const;
  479. /**
  480. * Format 2 Calendars to produce a string.
  481. *
  482. * Note: "fromCalendar" and "toCalendar" are not const,
  483. * since calendar is not const in SimpleDateFormat::format(Calendar&),
  484. *
  485. * @param fromCalendar calendar set to the from date in date interval
  486. * to be formatted into date interval string
  487. * @param toCalendar calendar set to the to date in date interval
  488. * to be formatted into date interval string
  489. * @param appendTo Output parameter to receive result.
  490. * Result is appended to existing contents.
  491. * @param fieldPosition On input: an alignment field, if desired.
  492. * On output: the offsets of the alignment field.
  493. * There may be multiple instances of a given field type
  494. * in an interval format; in this case the fieldPosition
  495. * offsets refer to the first instance.
  496. * @param status Output param filled with success/failure status.
  497. * Caller needs to make sure it is SUCCESS
  498. * at the function entrance
  499. * @return Reference to 'appendTo' parameter.
  500. * @stable ICU 4.0
  501. */
  502. UnicodeString& format(Calendar& fromCalendar,
  503. Calendar& toCalendar,
  504. UnicodeString& appendTo,
  505. FieldPosition& fieldPosition,
  506. UErrorCode& status) const ;
  507. /**
  508. * Format 2 Calendars to produce a FormattedDateInterval.
  509. *
  510. * The FormattedDateInterval exposes field information about the formatted string.
  511. *
  512. * Note: "fromCalendar" and "toCalendar" are not const,
  513. * since calendar is not const in SimpleDateFormat::format(Calendar&),
  514. *
  515. * @param fromCalendar calendar set to the from date in date interval
  516. * to be formatted into date interval string
  517. * @param toCalendar calendar set to the to date in date interval
  518. * to be formatted into date interval string
  519. * @param status Set if an error occurs.
  520. * @return A FormattedDateInterval containing the format result.
  521. * @stable ICU 64
  522. */
  523. FormattedDateInterval formatToValue(
  524. Calendar& fromCalendar,
  525. Calendar& toCalendar,
  526. UErrorCode& status) const;
  527. /**
  528. * Date interval parsing is not supported. Please do not use.
  529. * <P>
  530. * This method should handle parsing of
  531. * date time interval strings into Formattable objects with
  532. * DateInterval type, which is a pair of UDate.
  533. * <P>
  534. * Before calling, set parse_pos.index to the offset you want to start
  535. * parsing at in the source. After calling, parse_pos.index is the end of
  536. * the text you parsed. If error occurs, index is unchanged.
  537. * <P>
  538. * When parsing, leading whitespace is discarded (with a successful parse),
  539. * while trailing whitespace is left as is.
  540. * <P>
  541. * See Format::parseObject() for more.
  542. *
  543. * @param source The string to be parsed into an object.
  544. * @param result Formattable to be set to the parse result.
  545. * If parse fails, return contents are undefined.
  546. * @param parse_pos The position to start parsing at. Since no parsing
  547. * is supported, upon return this param is unchanged.
  548. * @return A newly created Formattable* object, or NULL
  549. * on failure. The caller owns this and should
  550. * delete it when done.
  551. * @internal ICU 4.0
  552. */
  553. virtual void parseObject(const UnicodeString& source,
  554. Formattable& result,
  555. ParsePosition& parse_pos) const;
  556. /**
  557. * Gets the date time interval patterns.
  558. * @return the date time interval patterns associated with
  559. * this date interval formatter.
  560. * @stable ICU 4.0
  561. */
  562. const DateIntervalInfo* getDateIntervalInfo(void) const;
  563. /**
  564. * Set the date time interval patterns.
  565. * @param newIntervalPatterns the given interval patterns to copy.
  566. * @param status output param set to success/failure code on exit
  567. * @stable ICU 4.0
  568. */
  569. void setDateIntervalInfo(const DateIntervalInfo& newIntervalPatterns,
  570. UErrorCode& status);
  571. /**
  572. * Gets the date formatter. The DateIntervalFormat instance continues to own
  573. * the returned DateFormatter object, and will use and possibly modify it
  574. * during format operations. In a multi-threaded environment, the returned
  575. * DateFormat can only be used if it is certain that no other threads are
  576. * concurrently using this DateIntervalFormatter, even for nominally const
  577. * functions.
  578. *
  579. * @return the date formatter associated with this date interval formatter.
  580. * @stable ICU 4.0
  581. */
  582. const DateFormat* getDateFormat(void) const;
  583. /**
  584. * Returns a reference to the TimeZone used by this DateIntervalFormat's calendar.
  585. * @return the time zone associated with the calendar of DateIntervalFormat.
  586. * @stable ICU 4.8
  587. */
  588. virtual const TimeZone& getTimeZone(void) const;
  589. /**
  590. * Sets the time zone for the calendar used by this DateIntervalFormat object. The
  591. * caller no longer owns the TimeZone object and should not delete it after this call.
  592. * @param zoneToAdopt the TimeZone to be adopted.
  593. * @stable ICU 4.8
  594. */
  595. virtual void adoptTimeZone(TimeZone* zoneToAdopt);
  596. /**
  597. * Sets the time zone for the calendar used by this DateIntervalFormat object.
  598. * @param zone the new time zone.
  599. * @stable ICU 4.8
  600. */
  601. virtual void setTimeZone(const TimeZone& zone);
  602. /**
  603. * Return the class ID for this class. This is useful only for comparing to
  604. * a return value from getDynamicClassID(). For example:
  605. * <pre>
  606. * . Base* polymorphic_pointer = createPolymorphicObject();
  607. * . if (polymorphic_pointer->getDynamicClassID() ==
  608. * . erived::getStaticClassID()) ...
  609. * </pre>
  610. * @return The class ID for all objects of this class.
  611. * @stable ICU 4.0
  612. */
  613. static UClassID U_EXPORT2 getStaticClassID(void);
  614. /**
  615. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
  616. * method is to implement a simple version of RTTI, since not all C++
  617. * compilers support genuine RTTI. Polymorphic operator==() and clone()
  618. * methods call this method.
  619. *
  620. * @return The class ID for this object. All objects of a
  621. * given class have the same class ID. Objects of
  622. * other classes have different class IDs.
  623. * @stable ICU 4.0
  624. */
  625. virtual UClassID getDynamicClassID(void) const;
  626. protected:
  627. /**
  628. * Copy constructor.
  629. * @stable ICU 4.0
  630. */
  631. DateIntervalFormat(const DateIntervalFormat&);
  632. /**
  633. * Assignment operator.
  634. * @stable ICU 4.0
  635. */
  636. DateIntervalFormat& operator=(const DateIntervalFormat&);
  637. private:
  638. /*
  639. * This is for ICU internal use only. Please do not use.
  640. * Save the interval pattern information.
  641. * Interval pattern consists of 2 single date patterns and the separator.
  642. * For example, interval pattern "MMM d - MMM d, yyyy" consists
  643. * a single date pattern "MMM d", another single date pattern "MMM d, yyyy",
  644. * and a separator "-".
  645. * The pattern is divided into 2 parts. For above example,
  646. * the first part is "MMM d - ", and the second part is "MMM d, yyyy".
  647. * Also, the first date appears in an interval pattern could be
  648. * the earlier date or the later date.
  649. * And such information is saved in the interval pattern as well.
  650. */
  651. struct PatternInfo {
  652. UnicodeString firstPart;
  653. UnicodeString secondPart;
  654. /**
  655. * Whether the first date in interval pattern is later date or not.
  656. * Fallback format set the default ordering.
  657. * And for a particular interval pattern, the order can be
  658. * overriden by prefixing the interval pattern with "latestFirst:" or
  659. * "earliestFirst:"
  660. * For example, given 2 date, Jan 10, 2007 to Feb 10, 2007.
  661. * if the fallback format is "{0} - {1}",
  662. * and the pattern is "d MMM - d MMM yyyy", the interval format is
  663. * "10 Jan - 10 Feb, 2007".
  664. * If the pattern is "latestFirst:d MMM - d MMM yyyy",
  665. * the interval format is "10 Feb - 10 Jan, 2007"
  666. */
  667. UBool laterDateFirst;
  668. };
  669. /**
  670. * default constructor
  671. * @internal (private)
  672. */
  673. DateIntervalFormat();
  674. /**
  675. * Construct a DateIntervalFormat from DateFormat,
  676. * a DateIntervalInfo, and skeleton.
  677. * DateFormat provides the timezone, calendar,
  678. * full pattern, and date format symbols information.
  679. * It should be a SimpleDateFormat object which
  680. * has a pattern in it.
  681. * the DateIntervalInfo provides the interval patterns.
  682. *
  683. * Note: the DateIntervalFormat takes ownership of both
  684. * DateFormat and DateIntervalInfo objects.
  685. * Caller should not delete them.
  686. *
  687. * @param locale the locale of this date interval formatter.
  688. * @param dtItvInfo the DateIntervalInfo object to be adopted.
  689. * @param skeleton the skeleton of the date formatter
  690. * @param status output param set to success/failure code on exit
  691. */
  692. DateIntervalFormat(const Locale& locale, DateIntervalInfo* dtItvInfo,
  693. const UnicodeString* skeleton, UErrorCode& status);
  694. /**
  695. * Construct a DateIntervalFormat from DateFormat
  696. * and a DateIntervalInfo.
  697. *
  698. * It is a wrapper of the constructor.
  699. *
  700. * @param locale the locale of this date interval formatter.
  701. * @param dtitvinf the DateIntervalInfo object to be adopted.
  702. * @param skeleton the skeleton of this formatter.
  703. * @param status Output param set to success/failure code.
  704. * @return a date time interval formatter which the caller owns.
  705. */
  706. static DateIntervalFormat* U_EXPORT2 create(const Locale& locale,
  707. DateIntervalInfo* dtitvinf,
  708. const UnicodeString* skeleton,
  709. UErrorCode& status);
  710. /**
  711. * Below are for generating interval patterns local to the formatter
  712. */
  713. /** Like fallbackFormat, but only formats the range part of the fallback. */
  714. void fallbackFormatRange(
  715. Calendar& fromCalendar,
  716. Calendar& toCalendar,
  717. UnicodeString& appendTo,
  718. int8_t& firstIndex,
  719. FieldPositionHandler& fphandler,
  720. UErrorCode& status) const;
  721. /**
  722. * Format 2 Calendars using fall-back interval pattern
  723. *
  724. * The full pattern used in this fall-back format is the
  725. * full pattern of the date formatter.
  726. *
  727. * gFormatterMutex must already be locked when calling this function.
  728. *
  729. * @param fromCalendar calendar set to the from date in date interval
  730. * to be formatted into date interval string
  731. * @param toCalendar calendar set to the to date in date interval
  732. * to be formatted into date interval string
  733. * @param fromToOnSameDay TRUE iff from and to dates are on the same day
  734. * (any difference is in ampm/hours or below)
  735. * @param appendTo Output parameter to receive result.
  736. * Result is appended to existing contents.
  737. * @param firstIndex See formatImpl for more information.
  738. * @param fphandler See formatImpl for more information.
  739. * @param status output param set to success/failure code on exit
  740. * @return Reference to 'appendTo' parameter.
  741. * @internal (private)
  742. */
  743. UnicodeString& fallbackFormat(Calendar& fromCalendar,
  744. Calendar& toCalendar,
  745. UBool fromToOnSameDay,
  746. UnicodeString& appendTo,
  747. int8_t& firstIndex,
  748. FieldPositionHandler& fphandler,
  749. UErrorCode& status) const;
  750. /**
  751. * Initialize interval patterns locale to this formatter
  752. *
  753. * This code is a bit complicated since
  754. * 1. the interval patterns saved in resource bundle files are interval
  755. * patterns based on date or time only.
  756. * It does not have interval patterns based on both date and time.
  757. * Interval patterns on both date and time are algorithm generated.
  758. *
  759. * For example, it has interval patterns on skeleton "dMy" and "hm",
  760. * but it does not have interval patterns on skeleton "dMyhm".
  761. *
  762. * The rule to generate interval patterns for both date and time skeleton are
  763. * 1) when the year, month, or day differs, concatenate the two original
  764. * expressions with a separator between,
  765. * For example, interval pattern from "Jan 10, 2007 10:10 am"
  766. * to "Jan 11, 2007 10:10am" is
  767. * "Jan 10, 2007 10:10 am - Jan 11, 2007 10:10am"
  768. *
  769. * 2) otherwise, present the date followed by the range expression
  770. * for the time.
  771. * For example, interval pattern from "Jan 10, 2007 10:10 am"
  772. * to "Jan 10, 2007 11:10am" is
  773. * "Jan 10, 2007 10:10 am - 11:10am"
  774. *
  775. * 2. even a pattern does not request a certain calendar field,
  776. * the interval pattern needs to include such field if such fields are
  777. * different between 2 dates.
  778. * For example, a pattern/skeleton is "hm", but the interval pattern
  779. * includes year, month, and date when year, month, and date differs.
  780. *
  781. *
  782. * @param status output param set to success/failure code on exit
  783. */
  784. void initializePattern(UErrorCode& status);
  785. /**
  786. * Set fall back interval pattern given a calendar field,
  787. * a skeleton, and a date time pattern generator.
  788. * @param field the largest different calendar field
  789. * @param skeleton a skeleton
  790. * @param status output param set to success/failure code on exit
  791. */
  792. void setFallbackPattern(UCalendarDateFields field,
  793. const UnicodeString& skeleton,
  794. UErrorCode& status);
  795. /**
  796. * get separated date and time skeleton from a combined skeleton.
  797. *
  798. * The difference between date skeleton and normalizedDateSkeleton are:
  799. * 1. both 'y' and 'd' are appeared only once in normalizeDateSkeleton
  800. * 2. 'E' and 'EE' are normalized into 'EEE'
  801. * 3. 'MM' is normalized into 'M'
  802. *
  803. ** the difference between time skeleton and normalizedTimeSkeleton are:
  804. * 1. both 'H' and 'h' are normalized as 'h' in normalized time skeleton,
  805. * 2. 'a' is omitted in normalized time skeleton.
  806. * 3. there is only one appearance for 'h', 'm','v', 'z' in normalized time
  807. * skeleton
  808. *
  809. *
  810. * @param skeleton given combined skeleton.
  811. * @param date Output parameter for date only skeleton.
  812. * @param normalizedDate Output parameter for normalized date only
  813. *
  814. * @param time Output parameter for time only skeleton.
  815. * @param normalizedTime Output parameter for normalized time only
  816. * skeleton.
  817. *
  818. */
  819. static void U_EXPORT2 getDateTimeSkeleton(const UnicodeString& skeleton,
  820. UnicodeString& date,
  821. UnicodeString& normalizedDate,
  822. UnicodeString& time,
  823. UnicodeString& normalizedTime);
  824. /**
  825. * Generate date or time interval pattern from resource,
  826. * and set them into the interval pattern locale to this formatter.
  827. *
  828. * It needs to handle the following:
  829. * 1. need to adjust field width.
  830. * For example, the interval patterns saved in DateIntervalInfo
  831. * includes "dMMMy", but not "dMMMMy".
  832. * Need to get interval patterns for dMMMMy from dMMMy.
  833. * Another example, the interval patterns saved in DateIntervalInfo
  834. * includes "hmv", but not "hmz".
  835. * Need to get interval patterns for "hmz' from 'hmv'
  836. *
  837. * 2. there might be no pattern for 'y' differ for skeleton "Md",
  838. * in order to get interval patterns for 'y' differ,
  839. * need to look for it from skeleton 'yMd'
  840. *
  841. * @param dateSkeleton normalized date skeleton
  842. * @param timeSkeleton normalized time skeleton
  843. * @return whether the resource is found for the skeleton.
  844. * TRUE if interval pattern found for the skeleton,
  845. * FALSE otherwise.
  846. */
  847. UBool setSeparateDateTimePtn(const UnicodeString& dateSkeleton,
  848. const UnicodeString& timeSkeleton);
  849. /**
  850. * Generate interval pattern from existing resource
  851. *
  852. * It not only save the interval patterns,
  853. * but also return the extended skeleton and its best match skeleton.
  854. *
  855. * @param field largest different calendar field
  856. * @param skeleton skeleton
  857. * @param bestSkeleton the best match skeleton which has interval pattern
  858. * defined in resource
  859. * @param differenceInfo the difference between skeleton and best skeleton
  860. * 0 means the best matched skeleton is the same as input skeleton
  861. * 1 means the fields are the same, but field width are different
  862. * 2 means the only difference between fields are v/z,
  863. * -1 means there are other fields difference
  864. *
  865. * @param extendedSkeleton extended skeleton
  866. * @param extendedBestSkeleton extended best match skeleton
  867. * @return whether the interval pattern is found
  868. * through extending skeleton or not.
  869. * TRUE if interval pattern is found by
  870. * extending skeleton, FALSE otherwise.
  871. */
  872. UBool setIntervalPattern(UCalendarDateFields field,
  873. const UnicodeString* skeleton,
  874. const UnicodeString* bestSkeleton,
  875. int8_t differenceInfo,
  876. UnicodeString* extendedSkeleton = NULL,
  877. UnicodeString* extendedBestSkeleton = NULL);
  878. /**
  879. * Adjust field width in best match interval pattern to match
  880. * the field width in input skeleton.
  881. *
  882. * TODO (xji) make a general solution
  883. * The adjusting rule can be:
  884. * 1. always adjust
  885. * 2. never adjust
  886. * 3. default adjust, which means adjust according to the following rules
  887. * 3.1 always adjust string, such as MMM and MMMM
  888. * 3.2 never adjust between string and numeric, such as MM and MMM
  889. * 3.3 always adjust year
  890. * 3.4 do not adjust 'd', 'h', or 'm' if h presents
  891. * 3.5 do not adjust 'M' if it is numeric(?)
  892. *
  893. * Since date interval format is well-formed format,
  894. * date and time skeletons are normalized previously,
  895. * till this stage, the adjust here is only "adjust strings, such as MMM
  896. * and MMMM, EEE and EEEE.
  897. *
  898. * @param inputSkeleton the input skeleton
  899. * @param bestMatchSkeleton the best match skeleton
  900. * @param bestMatchIntervalPattern the best match interval pattern
  901. * @param differenceInfo the difference between 2 skeletons
  902. * 1 means only field width differs
  903. * 2 means v/z exchange
  904. * @param adjustedIntervalPattern adjusted interval pattern
  905. */
  906. static void U_EXPORT2 adjustFieldWidth(
  907. const UnicodeString& inputSkeleton,
  908. const UnicodeString& bestMatchSkeleton,
  909. const UnicodeString& bestMatchIntervalPattern,
  910. int8_t differenceInfo,
  911. UnicodeString& adjustedIntervalPattern);
  912. /**
  913. * Concat a single date pattern with a time interval pattern,
  914. * set it into the intervalPatterns, while field is time field.
  915. * This is used to handle time interval patterns on skeleton with
  916. * both time and date. Present the date followed by
  917. * the range expression for the time.
  918. * @param format date and time format
  919. * @param datePattern date pattern
  920. * @param field time calendar field: AM_PM, HOUR, MINUTE
  921. * @param status output param set to success/failure code on exit
  922. */
  923. void concatSingleDate2TimeInterval(UnicodeString& format,
  924. const UnicodeString& datePattern,
  925. UCalendarDateFields field,
  926. UErrorCode& status);
  927. /**
  928. * check whether a calendar field present in a skeleton.
  929. * @param field calendar field need to check
  930. * @param skeleton given skeleton on which to check the calendar field
  931. * @return true if field present in a skeleton.
  932. */
  933. static UBool U_EXPORT2 fieldExistsInSkeleton(UCalendarDateFields field,
  934. const UnicodeString& skeleton);
  935. /**
  936. * Split interval patterns into 2 part.
  937. * @param intervalPattern interval pattern
  938. * @return the index in interval pattern which split the pattern into 2 part
  939. */
  940. static int32_t U_EXPORT2 splitPatternInto2Part(const UnicodeString& intervalPattern);
  941. /**
  942. * Break interval patterns as 2 part and save them into pattern info.
  943. * @param field calendar field
  944. * @param intervalPattern interval pattern
  945. */
  946. void setIntervalPattern(UCalendarDateFields field,
  947. const UnicodeString& intervalPattern);
  948. /**
  949. * Break interval patterns as 2 part and save them into pattern info.
  950. * @param field calendar field
  951. * @param intervalPattern interval pattern
  952. * @param laterDateFirst whether later date appear first in interval pattern
  953. */
  954. void setIntervalPattern(UCalendarDateFields field,
  955. const UnicodeString& intervalPattern,
  956. UBool laterDateFirst);
  957. /**
  958. * Set pattern information.
  959. *
  960. * @param field calendar field
  961. * @param firstPart the first part in interval pattern
  962. * @param secondPart the second part in interval pattern
  963. * @param laterDateFirst whether the first date in intervalPattern
  964. * is earlier date or later date
  965. */
  966. void setPatternInfo(UCalendarDateFields field,
  967. const UnicodeString* firstPart,
  968. const UnicodeString* secondPart,
  969. UBool laterDateFirst);
  970. /**
  971. * Format 2 Calendars to produce a string.
  972. * Implementation of the similar public format function.
  973. * Must be called with gFormatterMutex already locked.
  974. *
  975. * Note: "fromCalendar" and "toCalendar" are not const,
  976. * since calendar is not const in SimpleDateFormat::format(Calendar&),
  977. *
  978. * @param fromCalendar calendar set to the from date in date interval
  979. * to be formatted into date interval string
  980. * @param toCalendar calendar set to the to date in date interval
  981. * to be formatted into date interval string
  982. * @param appendTo Output parameter to receive result.
  983. * Result is appended to existing contents.
  984. * @param firstIndex 0 if the first output date is fromCalendar;
  985. * 1 if it corresponds to toCalendar;
  986. * -1 if there is only one date printed.
  987. * @param fphandler Handler for field position information.
  988. * The fields will be from the UDateFormatField enum.
  989. * @param status Output param filled with success/failure status.
  990. * Caller needs to make sure it is SUCCESS
  991. * at the function entrance
  992. * @return Reference to 'appendTo' parameter.
  993. * @internal (private)
  994. */
  995. UnicodeString& formatImpl(Calendar& fromCalendar,
  996. Calendar& toCalendar,
  997. UnicodeString& appendTo,
  998. int8_t& firstIndex,
  999. FieldPositionHandler& fphandler,
  1000. UErrorCode& status) const ;
  1001. /** Version of formatImpl for DateInterval. */
  1002. UnicodeString& formatIntervalImpl(const DateInterval& dtInterval,
  1003. UnicodeString& appendTo,
  1004. int8_t& firstIndex,
  1005. FieldPositionHandler& fphandler,
  1006. UErrorCode& status) const;
  1007. // from calendar field to pattern letter
  1008. static const char16_t fgCalendarFieldToPatternLetter[];
  1009. /**
  1010. * The interval patterns for this locale.
  1011. */
  1012. DateIntervalInfo* fInfo;
  1013. /**
  1014. * The DateFormat object used to format single pattern
  1015. */
  1016. SimpleDateFormat* fDateFormat;
  1017. /**
  1018. * The 2 calendars with the from and to date.
  1019. * could re-use the calendar in fDateFormat,
  1020. * but keeping 2 calendars make it clear and clean.
  1021. */
  1022. Calendar* fFromCalendar;
  1023. Calendar* fToCalendar;
  1024. Locale fLocale;
  1025. /**
  1026. * Following are interval information relevant (locale) to this formatter.
  1027. */
  1028. UnicodeString fSkeleton;
  1029. PatternInfo fIntervalPatterns[DateIntervalInfo::kIPI_MAX_INDEX];
  1030. /**
  1031. * Patterns for fallback formatting.
  1032. */
  1033. UnicodeString* fDatePattern;
  1034. UnicodeString* fTimePattern;
  1035. UnicodeString* fDateTimeFormat;
  1036. };
  1037. inline UBool
  1038. DateIntervalFormat::operator!=(const Format& other) const {
  1039. return !operator==(other);
  1040. }
  1041. U_NAMESPACE_END
  1042. #endif /* #if !UCONFIG_NO_FORMATTING */
  1043. #endif /* U_SHOW_CPLUSPLUS_API */
  1044. #endif // _DTITVFMT_H__
  1045. //eof