dtitvfmt.h 43 KB

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