dtitvfmt.h 39 KB

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