datefmt.h 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  1. /*
  2. ********************************************************************************
  3. * Copyright (C) 1997-2016, International Business Machines
  4. * Corporation and others. All Rights Reserved.
  5. ********************************************************************************
  6. *
  7. * File DATEFMT.H
  8. *
  9. * Modification History:
  10. *
  11. * Date Name Description
  12. * 02/19/97 aliu Converted from java.
  13. * 04/01/97 aliu Added support for centuries.
  14. * 07/23/98 stephen JDK 1.2 sync
  15. * 11/15/99 weiv Added support for week of year/day of week formatting
  16. ********************************************************************************
  17. */
  18. #ifndef DATEFMT_H
  19. #define DATEFMT_H
  20. #include "unicode/utypes.h"
  21. #if !UCONFIG_NO_FORMATTING
  22. #include "unicode/udat.h"
  23. #include "unicode/calendar.h"
  24. #include "unicode/numfmt.h"
  25. #include "unicode/format.h"
  26. #include "unicode/locid.h"
  27. #include "unicode/enumset.h"
  28. #include "unicode/udisplaycontext.h"
  29. /**
  30. * \file
  31. * \brief C++ API: Abstract class for converting dates.
  32. */
  33. U_NAMESPACE_BEGIN
  34. class TimeZone;
  35. class DateTimePatternGenerator;
  36. // explicit template instantiation. see digitlst.h
  37. #if defined (_MSC_VER)
  38. template class U_I18N_API EnumSet<UDateFormatBooleanAttribute,
  39. 0,
  40. UDAT_BOOLEAN_ATTRIBUTE_COUNT>;
  41. #endif
  42. /**
  43. * DateFormat is an abstract class for a family of classes that convert dates and
  44. * times from their internal representations to textual form and back again in a
  45. * language-independent manner. Converting from the internal representation (milliseconds
  46. * since midnight, January 1, 1970) to text is known as "formatting," and converting
  47. * from text to millis is known as "parsing." We currently define only one concrete
  48. * subclass of DateFormat: SimpleDateFormat, which can handle pretty much all normal
  49. * date formatting and parsing actions.
  50. * <P>
  51. * DateFormat helps you to format and parse dates for any locale. Your code can
  52. * be completely independent of the locale conventions for months, days of the
  53. * week, or even the calendar format: lunar vs. solar.
  54. * <P>
  55. * To format a date for the current Locale, use one of the static factory
  56. * methods:
  57. * <pre>
  58. * \code
  59. * DateFormat* dfmt = DateFormat::createDateInstance();
  60. * UDate myDate = Calendar::getNow();
  61. * UnicodeString myString;
  62. * myString = dfmt->format( myDate, myString );
  63. * \endcode
  64. * </pre>
  65. * If you are formatting multiple numbers, it is more efficient to get the
  66. * format and use it multiple times so that the system doesn't have to fetch the
  67. * information about the local language and country conventions multiple times.
  68. * <pre>
  69. * \code
  70. * DateFormat* df = DateFormat::createDateInstance();
  71. * UnicodeString myString;
  72. * UDate myDateArr[] = { 0.0, 100000000.0, 2000000000.0 }; // test values
  73. * for (int32_t i = 0; i < 3; ++i) {
  74. * myString.remove();
  75. * cout << df->format( myDateArr[i], myString ) << endl;
  76. * }
  77. * \endcode
  78. * </pre>
  79. * To get specific fields of a date, you can use UFieldPosition to
  80. * get specific fields.
  81. * <pre>
  82. * \code
  83. * DateFormat* dfmt = DateFormat::createDateInstance();
  84. * FieldPosition pos(DateFormat::YEAR_FIELD);
  85. * UnicodeString myString;
  86. * myString = dfmt->format( myDate, myString );
  87. * cout << myString << endl;
  88. * cout << pos.getBeginIndex() << "," << pos. getEndIndex() << endl;
  89. * \endcode
  90. * </pre>
  91. * To format a date for a different Locale, specify it in the call to
  92. * createDateInstance().
  93. * <pre>
  94. * \code
  95. * DateFormat* df =
  96. * DateFormat::createDateInstance( DateFormat::SHORT, Locale::getFrance());
  97. * \endcode
  98. * </pre>
  99. * You can use a DateFormat to parse also.
  100. * <pre>
  101. * \code
  102. * UErrorCode status = U_ZERO_ERROR;
  103. * UDate myDate = df->parse(myString, status);
  104. * \endcode
  105. * </pre>
  106. * Use createDateInstance() to produce the normal date format for that country.
  107. * There are other static factory methods available. Use createTimeInstance()
  108. * to produce the normal time format for that country. Use createDateTimeInstance()
  109. * to produce a DateFormat that formats both date and time. You can pass in
  110. * different options to these factory methods to control the length of the
  111. * result; from SHORT to MEDIUM to LONG to FULL. The exact result depends on the
  112. * locale, but generally:
  113. * <ul type=round>
  114. * <li> SHORT is completely numeric, such as 12/13/52 or 3:30pm
  115. * <li> MEDIUM is longer, such as Jan 12, 1952
  116. * <li> LONG is longer, such as January 12, 1952 or 3:30:32pm
  117. * <li> FULL is pretty completely specified, such as
  118. * Tuesday, April 12, 1952 AD or 3:30:42pm PST.
  119. * </ul>
  120. * You can also set the time zone on the format if you wish. If you want even
  121. * more control over the format or parsing, (or want to give your users more
  122. * control), you can try casting the DateFormat you get from the factory methods
  123. * to a SimpleDateFormat. This will work for the majority of countries; just
  124. * remember to chck getDynamicClassID() before carrying out the cast.
  125. * <P>
  126. * You can also use forms of the parse and format methods with ParsePosition and
  127. * FieldPosition to allow you to
  128. * <ul type=round>
  129. * <li> Progressively parse through pieces of a string.
  130. * <li> Align any particular field, or find out where it is for selection
  131. * on the screen.
  132. * </ul>
  133. *
  134. * <p><em>User subclasses are not supported.</em> While clients may write
  135. * subclasses, such code will not necessarily work and will not be
  136. * guaranteed to work stably from release to release.
  137. */
  138. class U_I18N_API DateFormat : public Format {
  139. public:
  140. /**
  141. * Constants for various style patterns. These reflect the order of items in
  142. * the DateTimePatterns resource. There are 4 time patterns, 4 date patterns,
  143. * the default date-time pattern, and 4 date-time patterns. Each block of 4 values
  144. * in the resource occurs in the order full, long, medium, short.
  145. * @stable ICU 2.4
  146. */
  147. enum EStyle
  148. {
  149. kNone = -1,
  150. kFull = 0,
  151. kLong = 1,
  152. kMedium = 2,
  153. kShort = 3,
  154. kDateOffset = kShort + 1,
  155. // kFull + kDateOffset = 4
  156. // kLong + kDateOffset = 5
  157. // kMedium + kDateOffset = 6
  158. // kShort + kDateOffset = 7
  159. kDateTime = 8,
  160. // Default DateTime
  161. kDateTimeOffset = kDateTime + 1,
  162. // kFull + kDateTimeOffset = 9
  163. // kLong + kDateTimeOffset = 10
  164. // kMedium + kDateTimeOffset = 11
  165. // kShort + kDateTimeOffset = 12
  166. // relative dates
  167. kRelative = (1 << 7),
  168. kFullRelative = (kFull | kRelative),
  169. kLongRelative = kLong | kRelative,
  170. kMediumRelative = kMedium | kRelative,
  171. kShortRelative = kShort | kRelative,
  172. kDefault = kMedium,
  173. /**
  174. * These constants are provided for backwards compatibility only.
  175. * Please use the C++ style constants defined above.
  176. */
  177. FULL = kFull,
  178. LONG = kLong,
  179. MEDIUM = kMedium,
  180. SHORT = kShort,
  181. DEFAULT = kDefault,
  182. DATE_OFFSET = kDateOffset,
  183. NONE = kNone,
  184. DATE_TIME = kDateTime
  185. };
  186. /**
  187. * Destructor.
  188. * @stable ICU 2.0
  189. */
  190. virtual ~DateFormat();
  191. /**
  192. * Equality operator. Returns true if the two formats have the same behavior.
  193. * @stable ICU 2.0
  194. */
  195. virtual UBool operator==(const Format&) const;
  196. using Format::format;
  197. /**
  198. * Format an object to produce a string. This method handles Formattable
  199. * objects with a UDate type. If a the Formattable object type is not a Date,
  200. * then it returns a failing UErrorCode.
  201. *
  202. * @param obj The object to format. Must be a Date.
  203. * @param appendTo Output parameter to receive result.
  204. * Result is appended to existing contents.
  205. * @param pos On input: an alignment field, if desired.
  206. * On output: the offsets of the alignment field.
  207. * @param status Output param filled with success/failure status.
  208. * @return Reference to 'appendTo' parameter.
  209. * @stable ICU 2.0
  210. */
  211. virtual UnicodeString& format(const Formattable& obj,
  212. UnicodeString& appendTo,
  213. FieldPosition& pos,
  214. UErrorCode& status) const;
  215. /**
  216. * Format an object to produce a string. This method handles Formattable
  217. * objects with a UDate type. If a the Formattable object type is not a Date,
  218. * then it returns a failing UErrorCode.
  219. *
  220. * @param obj The object to format. Must be a Date.
  221. * @param appendTo Output parameter to receive result.
  222. * Result is appended to existing contents.
  223. * @param posIter On return, can be used to iterate over positions
  224. * of fields generated by this format call. Field values
  225. * are defined in UDateFormatField. Can be NULL.
  226. * @param status Output param filled with success/failure status.
  227. * @return Reference to 'appendTo' parameter.
  228. * @stable ICU 4.4
  229. */
  230. virtual UnicodeString& format(const Formattable& obj,
  231. UnicodeString& appendTo,
  232. FieldPositionIterator* posIter,
  233. UErrorCode& status) const;
  234. /**
  235. * Formats a date into a date/time string. This is an abstract method which
  236. * concrete subclasses must implement.
  237. * <P>
  238. * On input, the FieldPosition parameter may have its "field" member filled with
  239. * an enum value specifying a field. On output, the FieldPosition will be filled
  240. * in with the text offsets for that field.
  241. * <P> For example, given a time text
  242. * "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition.field is
  243. * UDAT_YEAR_FIELD, the offsets fieldPosition.beginIndex and
  244. * statfieldPositionus.getEndIndex will be set to 0 and 4, respectively.
  245. * <P> Notice
  246. * that if the same time field appears more than once in a pattern, the status will
  247. * be set for the first occurence of that time field. For instance,
  248. * formatting a UDate to the time string "1 PM PDT (Pacific Daylight Time)"
  249. * using the pattern "h a z (zzzz)" and the alignment field
  250. * DateFormat::TIMEZONE_FIELD, the offsets fieldPosition.beginIndex and
  251. * fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first
  252. * occurence of the timezone pattern character 'z'.
  253. *
  254. * @param cal Calendar set to the date and time to be formatted
  255. * into a date/time string. When the calendar type is
  256. * different from the internal calendar held by this
  257. * DateFormat instance, the date and the time zone will
  258. * be inherited from the input calendar, but other calendar
  259. * field values will be calculated by the internal calendar.
  260. * @param appendTo Output parameter to receive result.
  261. * Result is appended to existing contents.
  262. * @param fieldPosition On input: an alignment field, if desired (see examples above)
  263. * On output: the offsets of the alignment field (see examples above)
  264. * @return Reference to 'appendTo' parameter.
  265. * @stable ICU 2.1
  266. */
  267. virtual UnicodeString& format( Calendar& cal,
  268. UnicodeString& appendTo,
  269. FieldPosition& fieldPosition) const = 0;
  270. /**
  271. * Formats a date into a date/time string. Subclasses should implement this method.
  272. *
  273. * @param cal Calendar set to the date and time to be formatted
  274. * into a date/time string. When the calendar type is
  275. * different from the internal calendar held by this
  276. * DateFormat instance, the date and the time zone will
  277. * be inherited from the input calendar, but other calendar
  278. * field values will be calculated by the internal calendar.
  279. * @param appendTo Output parameter to receive result.
  280. * Result is appended to existing contents.
  281. * @param posIter On return, can be used to iterate over positions
  282. * of fields generated by this format call. Field values
  283. * are defined in UDateFormatField. Can be NULL.
  284. * @param status error status.
  285. * @return Reference to 'appendTo' parameter.
  286. * @stable ICU 4.4
  287. */
  288. virtual UnicodeString& format(Calendar& cal,
  289. UnicodeString& appendTo,
  290. FieldPositionIterator* posIter,
  291. UErrorCode& status) const;
  292. /**
  293. * Formats a UDate into a date/time string.
  294. * <P>
  295. * On input, the FieldPosition parameter may have its "field" member filled with
  296. * an enum value specifying a field. On output, the FieldPosition will be filled
  297. * in with the text offsets for that field.
  298. * <P> For example, given a time text
  299. * "1996.07.10 AD at 15:08:56 PDT", if the given fieldPosition.field is
  300. * UDAT_YEAR_FIELD, the offsets fieldPosition.beginIndex and
  301. * statfieldPositionus.getEndIndex will be set to 0 and 4, respectively.
  302. * <P> Notice
  303. * that if the same time field appears more than once in a pattern, the status will
  304. * be set for the first occurence of that time field. For instance,
  305. * formatting a UDate to the time string "1 PM PDT (Pacific Daylight Time)"
  306. * using the pattern "h a z (zzzz)" and the alignment field
  307. * DateFormat::TIMEZONE_FIELD, the offsets fieldPosition.beginIndex and
  308. * fieldPosition.getEndIndex will be set to 5 and 8, respectively, for the first
  309. * occurence of the timezone pattern character 'z'.
  310. *
  311. * @param date UDate to be formatted into a date/time string.
  312. * @param appendTo Output parameter to receive result.
  313. * Result is appended to existing contents.
  314. * @param fieldPosition On input: an alignment field, if desired (see examples above)
  315. * On output: the offsets of the alignment field (see examples above)
  316. * @return Reference to 'appendTo' parameter.
  317. * @stable ICU 2.0
  318. */
  319. UnicodeString& format( UDate date,
  320. UnicodeString& appendTo,
  321. FieldPosition& fieldPosition) const;
  322. /**
  323. * Formats a UDate into a date/time string.
  324. *
  325. * @param date UDate to be formatted into a date/time string.
  326. * @param appendTo Output parameter to receive result.
  327. * Result is appended to existing contents.
  328. * @param posIter On return, can be used to iterate over positions
  329. * of fields generated by this format call. Field values
  330. * are defined in UDateFormatField. Can be NULL.
  331. * @param status error status.
  332. * @return Reference to 'appendTo' parameter.
  333. * @stable ICU 4.4
  334. */
  335. UnicodeString& format(UDate date,
  336. UnicodeString& appendTo,
  337. FieldPositionIterator* posIter,
  338. UErrorCode& status) const;
  339. /**
  340. * Formats a UDate into a date/time string. If there is a problem, you won't
  341. * know, using this method. Use the overloaded format() method which takes a
  342. * FieldPosition& to detect formatting problems.
  343. *
  344. * @param date The UDate value to be formatted into a string.
  345. * @param appendTo Output parameter to receive result.
  346. * Result is appended to existing contents.
  347. * @return Reference to 'appendTo' parameter.
  348. * @stable ICU 2.0
  349. */
  350. UnicodeString& format(UDate date, UnicodeString& appendTo) const;
  351. /**
  352. * Parse a date/time string. For example, a time text "07/10/96 4:5 PM, PDT"
  353. * will be parsed into a UDate that is equivalent to Date(837039928046).
  354. * Parsing begins at the beginning of the string and proceeds as far as
  355. * possible. Assuming no parse errors were encountered, this function
  356. * doesn't return any information about how much of the string was consumed
  357. * by the parsing. If you need that information, use the version of
  358. * parse() that takes a ParsePosition.
  359. * <P>
  360. * By default, parsing is lenient: If the input is not in the form used by
  361. * this object's format method but can still be parsed as a date, then the
  362. * parse succeeds. Clients may insist on strict adherence to the format by
  363. * calling setLenient(false).
  364. * @see DateFormat::setLenient(boolean)
  365. * <P>
  366. * Note that the normal date formats associated with some calendars - such
  367. * as the Chinese lunar calendar - do not specify enough fields to enable
  368. * dates to be parsed unambiguously. In the case of the Chinese lunar
  369. * calendar, while the year within the current 60-year cycle is specified,
  370. * the number of such cycles since the start date of the calendar (in the
  371. * ERA field of the Calendar object) is not normally part of the format,
  372. * and parsing may assume the wrong era. For cases such as this it is
  373. * recommended that clients parse using the method
  374. * parse(const UnicodeString&, Calendar& cal, ParsePosition&)
  375. * with the Calendar passed in set to the current date, or to a date
  376. * within the era/cycle that should be assumed if absent in the format.
  377. *
  378. * @param text The date/time string to be parsed into a UDate value.
  379. * @param status Output param to be set to success/failure code. If
  380. * 'text' cannot be parsed, it will be set to a failure
  381. * code.
  382. * @return The parsed UDate value, if successful.
  383. * @stable ICU 2.0
  384. */
  385. virtual UDate parse( const UnicodeString& text,
  386. UErrorCode& status) const;
  387. /**
  388. * Parse a date/time string beginning at the given parse position. For
  389. * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
  390. * that is equivalent to Date(837039928046).
  391. * <P>
  392. * By default, parsing is lenient: If the input is not in the form used by
  393. * this object's format method but can still be parsed as a date, then the
  394. * parse succeeds. Clients may insist on strict adherence to the format by
  395. * calling setLenient(false).
  396. * @see DateFormat::setLenient(boolean)
  397. *
  398. * @param text The date/time string to be parsed.
  399. * @param cal A Calendar set on input to the date and time to be used for
  400. * missing values in the date/time string being parsed, and set
  401. * on output to the parsed date/time. When the calendar type is
  402. * different from the internal calendar held by this DateFormat
  403. * instance, the internal calendar will be cloned to a work
  404. * calendar set to the same milliseconds and time zone as the
  405. * cal parameter, field values will be parsed based on the work
  406. * calendar, then the result (milliseconds and time zone) will
  407. * be set in this calendar.
  408. * @param pos On input, the position at which to start parsing; on
  409. * output, the position at which parsing terminated, or the
  410. * start position if the parse failed.
  411. * @stable ICU 2.1
  412. */
  413. virtual void parse( const UnicodeString& text,
  414. Calendar& cal,
  415. ParsePosition& pos) const = 0;
  416. /**
  417. * Parse a date/time string beginning at the given parse position. For
  418. * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
  419. * that is equivalent to Date(837039928046).
  420. * <P>
  421. * By default, parsing is lenient: If the input is not in the form used by
  422. * this object's format method but can still be parsed as a date, then the
  423. * parse succeeds. Clients may insist on strict adherence to the format by
  424. * calling setLenient(false).
  425. * @see DateFormat::setLenient(boolean)
  426. * <P>
  427. * Note that the normal date formats associated with some calendars - such
  428. * as the Chinese lunar calendar - do not specify enough fields to enable
  429. * dates to be parsed unambiguously. In the case of the Chinese lunar
  430. * calendar, while the year within the current 60-year cycle is specified,
  431. * the number of such cycles since the start date of the calendar (in the
  432. * ERA field of the Calendar object) is not normally part of the format,
  433. * and parsing may assume the wrong era. For cases such as this it is
  434. * recommended that clients parse using the method
  435. * parse(const UnicodeString&, Calendar& cal, ParsePosition&)
  436. * with the Calendar passed in set to the current date, or to a date
  437. * within the era/cycle that should be assumed if absent in the format.
  438. *
  439. * @param text The date/time string to be parsed into a UDate value.
  440. * @param pos On input, the position at which to start parsing; on
  441. * output, the position at which parsing terminated, or the
  442. * start position if the parse failed.
  443. * @return A valid UDate if the input could be parsed.
  444. * @stable ICU 2.0
  445. */
  446. UDate parse( const UnicodeString& text,
  447. ParsePosition& pos) const;
  448. /**
  449. * Parse a string to produce an object. This methods handles parsing of
  450. * date/time strings into Formattable objects with UDate types.
  451. * <P>
  452. * Before calling, set parse_pos.index to the offset you want to start
  453. * parsing at in the source. After calling, parse_pos.index is the end of
  454. * the text you parsed. If error occurs, index is unchanged.
  455. * <P>
  456. * When parsing, leading whitespace is discarded (with a successful parse),
  457. * while trailing whitespace is left as is.
  458. * <P>
  459. * See Format::parseObject() for more.
  460. *
  461. * @param source The string to be parsed into an object.
  462. * @param result Formattable to be set to the parse result.
  463. * If parse fails, return contents are undefined.
  464. * @param parse_pos The position to start parsing at. Upon return
  465. * this param is set to the position after the
  466. * last character successfully parsed. If the
  467. * source is not parsed successfully, this param
  468. * will remain unchanged.
  469. * @stable ICU 2.0
  470. */
  471. virtual void parseObject(const UnicodeString& source,
  472. Formattable& result,
  473. ParsePosition& parse_pos) const;
  474. /**
  475. * Create a default date/time formatter that uses the SHORT style for both
  476. * the date and the time.
  477. *
  478. * @return A date/time formatter which the caller owns.
  479. * @stable ICU 2.0
  480. */
  481. static DateFormat* U_EXPORT2 createInstance(void);
  482. /**
  483. * Creates a time formatter with the given formatting style for the given
  484. * locale.
  485. *
  486. * @param style The given formatting style. For example,
  487. * SHORT for "h:mm a" in the US locale. Relative
  488. * time styles are not currently supported.
  489. * @param aLocale The given locale.
  490. * @return A time formatter which the caller owns.
  491. * @stable ICU 2.0
  492. */
  493. static DateFormat* U_EXPORT2 createTimeInstance(EStyle style = kDefault,
  494. const Locale& aLocale = Locale::getDefault());
  495. /**
  496. * Creates a date formatter with the given formatting style for the given
  497. * const locale.
  498. *
  499. * @param style The given formatting style. For example, SHORT for "M/d/yy" in the
  500. * US locale. As currently implemented, relative date formatting only
  501. * affects a limited range of calendar days before or after the
  502. * current date, based on the CLDR &lt;field type="day"&gt;/&lt;relative&gt; data:
  503. * For example, in English, "Yesterday", "Today", and "Tomorrow".
  504. * Outside of this range, dates are formatted using the corresponding
  505. * non-relative style.
  506. * @param aLocale The given locale.
  507. * @return A date formatter which the caller owns.
  508. * @stable ICU 2.0
  509. */
  510. static DateFormat* U_EXPORT2 createDateInstance(EStyle style = kDefault,
  511. const Locale& aLocale = Locale::getDefault());
  512. /**
  513. * Creates a date/time formatter with the given formatting styles for the
  514. * given locale.
  515. *
  516. * @param dateStyle The given formatting style for the date portion of the result.
  517. * For example, SHORT for "M/d/yy" in the US locale. As currently
  518. * implemented, relative date formatting only affects a limited range
  519. * of calendar days before or after the current date, based on the
  520. * CLDR &lt;field type="day"&gt;/&lt;relative&gt; data: For example, in English,
  521. * "Yesterday", "Today", and "Tomorrow". Outside of this range, dates
  522. * are formatted using the corresponding non-relative style.
  523. * @param timeStyle The given formatting style for the time portion of the result.
  524. * For example, SHORT for "h:mm a" in the US locale. Relative
  525. * time styles are not currently supported.
  526. * @param aLocale The given locale.
  527. * @return A date/time formatter which the caller owns.
  528. * @stable ICU 2.0
  529. */
  530. static DateFormat* U_EXPORT2 createDateTimeInstance(EStyle dateStyle = kDefault,
  531. EStyle timeStyle = kDefault,
  532. const Locale& aLocale = Locale::getDefault());
  533. #ifndef U_HIDE_INTERNAL_API
  534. /**
  535. * Returns the best pattern given a skeleton and locale.
  536. * @param locale the locale
  537. * @param skeleton the skeleton
  538. * @param status ICU error returned here
  539. * @return the best pattern.
  540. * @internal For ICU use only.
  541. */
  542. static UnicodeString getBestPattern(
  543. const Locale &locale,
  544. const UnicodeString &skeleton,
  545. UErrorCode &status);
  546. #endif /* U_HIDE_INTERNAL_API */
  547. /**
  548. * Creates a date/time formatter for the given skeleton and
  549. * default locale.
  550. *
  551. * @param skeleton The skeleton e.g "yMMMMd." Fields in the skeleton can
  552. * be in any order, and this method uses the locale to
  553. * map the skeleton to a pattern that includes locale
  554. * specific separators with the fields in the appropriate
  555. * order for that locale.
  556. * @param status Any error returned here.
  557. * @return A date/time formatter which the caller owns.
  558. * @stable ICU 55
  559. */
  560. static DateFormat* U_EXPORT2 createInstanceForSkeleton(
  561. const UnicodeString& skeleton,
  562. UErrorCode &status);
  563. /**
  564. * Creates a date/time formatter for the given skeleton and locale.
  565. *
  566. * @param skeleton The skeleton e.g "yMMMMd." Fields in the skeleton can
  567. * be in any order, and this method uses the locale to
  568. * map the skeleton to a pattern that includes locale
  569. * specific separators with the fields in the appropriate
  570. * order for that locale.
  571. * @param locale The given locale.
  572. * @param status Any error returned here.
  573. * @return A date/time formatter which the caller owns.
  574. * @stable ICU 55
  575. */
  576. static DateFormat* U_EXPORT2 createInstanceForSkeleton(
  577. const UnicodeString& skeleton,
  578. const Locale &locale,
  579. UErrorCode &status);
  580. /**
  581. * Creates a date/time formatter for the given skeleton and locale.
  582. *
  583. * @param calendarToAdopt the calendar returned DateFormat is to use.
  584. * @param skeleton The skeleton e.g "yMMMMd." Fields in the skeleton can
  585. * be in any order, and this method uses the locale to
  586. * map the skeleton to a pattern that includes locale
  587. * specific separators with the fields in the appropriate
  588. * order for that locale.
  589. * @param locale The given locale.
  590. * @param status Any error returned here.
  591. * @return A date/time formatter which the caller owns.
  592. * @stable ICU 55
  593. */
  594. static DateFormat* U_EXPORT2 createInstanceForSkeleton(
  595. Calendar *calendarToAdopt,
  596. const UnicodeString& skeleton,
  597. const Locale &locale,
  598. UErrorCode &status);
  599. /**
  600. * Gets the set of locales for which DateFormats are installed.
  601. * @param count Filled in with the number of locales in the list that is returned.
  602. * @return the set of locales for which DateFormats are installed. The caller
  603. * does NOT own this list and must not delete it.
  604. * @stable ICU 2.0
  605. */
  606. static const Locale* U_EXPORT2 getAvailableLocales(int32_t& count);
  607. /**
  608. * Returns whether both date/time parsing in the encapsulated Calendar object and DateFormat whitespace &
  609. * numeric processing is lenient.
  610. * @stable ICU 2.0
  611. */
  612. virtual UBool isLenient(void) const;
  613. /**
  614. * Specifies whether date/time parsing is to be lenient. With
  615. * lenient parsing, the parser may use heuristics to interpret inputs that
  616. * do not precisely match this object's format. Without lenient parsing,
  617. * inputs must match this object's format more closely.
  618. *
  619. * Note: ICU 53 introduced finer grained control of leniency (and added
  620. * new control points) making the preferred method a combination of
  621. * setCalendarLenient() & setBooleanAttribute() calls.
  622. * This method supports prior functionality but may not support all
  623. * future leniency control & behavior of DateFormat. For control of pre 53 leniency,
  624. * Calendar and DateFormat whitespace & numeric tolerance, this method is safe to
  625. * use. However, mixing leniency control via this method and modification of the
  626. * newer attributes via setBooleanAttribute() may produce undesirable
  627. * results.
  628. *
  629. * @param lenient True specifies date/time interpretation to be lenient.
  630. * @see Calendar::setLenient
  631. * @stable ICU 2.0
  632. */
  633. virtual void setLenient(UBool lenient);
  634. /**
  635. * Returns whether date/time parsing in the encapsulated Calendar object processing is lenient.
  636. * @stable ICU 53
  637. */
  638. virtual UBool isCalendarLenient(void) const;
  639. /**
  640. * Specifies whether encapsulated Calendar date/time parsing is to be lenient. With
  641. * lenient parsing, the parser may use heuristics to interpret inputs that
  642. * do not precisely match this object's format. Without lenient parsing,
  643. * inputs must match this object's format more closely.
  644. * @param lenient when true, parsing is lenient
  645. * @see com.ibm.icu.util.Calendar#setLenient
  646. * @stable ICU 53
  647. */
  648. virtual void setCalendarLenient(UBool lenient);
  649. /**
  650. * Gets the calendar associated with this date/time formatter.
  651. * The calendar is owned by the formatter and must not be modified.
  652. * Also, the calendar does not reflect the results of a parse operation.
  653. * To parse to a calendar, use {@link #parse(const UnicodeString&, Calendar& cal, ParsePosition&) const parse(const UnicodeString&, Calendar& cal, ParsePosition&)}
  654. * @return the calendar associated with this date/time formatter.
  655. * @stable ICU 2.0
  656. */
  657. virtual const Calendar* getCalendar(void) const;
  658. /**
  659. * Set the calendar to be used by this date format. Initially, the default
  660. * calendar for the specified or default locale is used. The caller should
  661. * not delete the Calendar object after it is adopted by this call.
  662. * Adopting a new calendar will change to the default symbols.
  663. *
  664. * @param calendarToAdopt Calendar object to be adopted.
  665. * @stable ICU 2.0
  666. */
  667. virtual void adoptCalendar(Calendar* calendarToAdopt);
  668. /**
  669. * Set the calendar to be used by this date format. Initially, the default
  670. * calendar for the specified or default locale is used.
  671. *
  672. * @param newCalendar Calendar object to be set.
  673. * @stable ICU 2.0
  674. */
  675. virtual void setCalendar(const Calendar& newCalendar);
  676. /**
  677. * Gets the number formatter which this date/time formatter uses to format
  678. * and parse the numeric portions of the pattern.
  679. * @return the number formatter which this date/time formatter uses.
  680. * @stable ICU 2.0
  681. */
  682. virtual const NumberFormat* getNumberFormat(void) const;
  683. /**
  684. * Allows you to set the number formatter. The caller should
  685. * not delete the NumberFormat object after it is adopted by this call.
  686. * @param formatToAdopt NumberFormat object to be adopted.
  687. * @stable ICU 2.0
  688. */
  689. virtual void adoptNumberFormat(NumberFormat* formatToAdopt);
  690. /**
  691. * Allows you to set the number formatter.
  692. * @param newNumberFormat NumberFormat object to be set.
  693. * @stable ICU 2.0
  694. */
  695. virtual void setNumberFormat(const NumberFormat& newNumberFormat);
  696. /**
  697. * Returns a reference to the TimeZone used by this DateFormat's calendar.
  698. * @return the time zone associated with the calendar of DateFormat.
  699. * @stable ICU 2.0
  700. */
  701. virtual const TimeZone& getTimeZone(void) const;
  702. /**
  703. * Sets the time zone for the calendar of this DateFormat object. The caller
  704. * no longer owns the TimeZone object and should not delete it after this call.
  705. * @param zoneToAdopt the TimeZone to be adopted.
  706. * @stable ICU 2.0
  707. */
  708. virtual void adoptTimeZone(TimeZone* zoneToAdopt);
  709. /**
  710. * Sets the time zone for the calendar of this DateFormat object.
  711. * @param zone the new time zone.
  712. * @stable ICU 2.0
  713. */
  714. virtual void setTimeZone(const TimeZone& zone);
  715. /**
  716. * Set a particular UDisplayContext value in the formatter, such as
  717. * UDISPCTX_CAPITALIZATION_FOR_STANDALONE.
  718. * @param value The UDisplayContext value to set.
  719. * @param status Input/output status. If at entry this indicates a failure
  720. * status, the function will do nothing; otherwise this will be
  721. * updated with any new status from the function.
  722. * @stable ICU 53
  723. */
  724. virtual void setContext(UDisplayContext value, UErrorCode& status);
  725. /**
  726. * Get the formatter's UDisplayContext value for the specified UDisplayContextType,
  727. * such as UDISPCTX_TYPE_CAPITALIZATION.
  728. * @param type The UDisplayContextType whose value to return
  729. * @param status Input/output status. If at entry this indicates a failure
  730. * status, the function will do nothing; otherwise this will be
  731. * updated with any new status from the function.
  732. * @return The UDisplayContextValue for the specified type.
  733. * @stable ICU 53
  734. */
  735. virtual UDisplayContext getContext(UDisplayContextType type, UErrorCode& status) const;
  736. /**
  737. * Sets an boolean attribute on this DateFormat.
  738. * May return U_UNSUPPORTED_ERROR if this instance does not support
  739. * the specified attribute.
  740. * @param attr the attribute to set
  741. * @param newvalue new value
  742. * @param status the error type
  743. * @return *this - for chaining (example: format.setAttribute(...).setAttribute(...) )
  744. * @stable ICU 53
  745. */
  746. virtual DateFormat& U_EXPORT2 setBooleanAttribute(UDateFormatBooleanAttribute attr,
  747. UBool newvalue,
  748. UErrorCode &status);
  749. /**
  750. * Returns a boolean from this DateFormat
  751. * May return U_UNSUPPORTED_ERROR if this instance does not support
  752. * the specified attribute.
  753. * @param attr the attribute to set
  754. * @param status the error type
  755. * @return the attribute value. Undefined if there is an error.
  756. * @stable ICU 53
  757. */
  758. virtual UBool U_EXPORT2 getBooleanAttribute(UDateFormatBooleanAttribute attr, UErrorCode &status) const;
  759. protected:
  760. /**
  761. * Default constructor. Creates a DateFormat with no Calendar or NumberFormat
  762. * associated with it. This constructor depends on the subclasses to fill in
  763. * the calendar and numberFormat fields.
  764. * @stable ICU 2.0
  765. */
  766. DateFormat();
  767. /**
  768. * Copy constructor.
  769. * @stable ICU 2.0
  770. */
  771. DateFormat(const DateFormat&);
  772. /**
  773. * Default assignment operator.
  774. * @stable ICU 2.0
  775. */
  776. DateFormat& operator=(const DateFormat&);
  777. /**
  778. * The calendar that DateFormat uses to produce the time field values needed
  779. * to implement date/time formatting. Subclasses should generally initialize
  780. * this to the default calendar for the locale associated with this DateFormat.
  781. * @stable ICU 2.4
  782. */
  783. Calendar* fCalendar;
  784. /**
  785. * The number formatter that DateFormat uses to format numbers in dates and
  786. * times. Subclasses should generally initialize this to the default number
  787. * format for the locale associated with this DateFormat.
  788. * @stable ICU 2.4
  789. */
  790. NumberFormat* fNumberFormat;
  791. private:
  792. /**
  793. * Gets the date/time formatter with the given formatting styles for the
  794. * given locale.
  795. * @param dateStyle the given date formatting style.
  796. * @param timeStyle the given time formatting style.
  797. * @param inLocale the given locale.
  798. * @return a date/time formatter, or 0 on failure.
  799. */
  800. static DateFormat* U_EXPORT2 create(EStyle timeStyle, EStyle dateStyle, const Locale& inLocale);
  801. /**
  802. * enum set of active boolean attributes for this instance
  803. */
  804. EnumSet<UDateFormatBooleanAttribute, 0, UDAT_BOOLEAN_ATTRIBUTE_COUNT> fBoolFlags;
  805. UDisplayContext fCapitalizationContext;
  806. friend class DateFmtKeyByStyle;
  807. public:
  808. #ifndef U_HIDE_OBSOLETE_API
  809. /**
  810. * Field selector for FieldPosition for DateFormat fields.
  811. * @obsolete ICU 3.4 use UDateFormatField instead, since this API will be
  812. * removed in that release
  813. */
  814. enum EField
  815. {
  816. // Obsolete; use UDateFormatField instead
  817. kEraField = UDAT_ERA_FIELD,
  818. kYearField = UDAT_YEAR_FIELD,
  819. kMonthField = UDAT_MONTH_FIELD,
  820. kDateField = UDAT_DATE_FIELD,
  821. kHourOfDay1Field = UDAT_HOUR_OF_DAY1_FIELD,
  822. kHourOfDay0Field = UDAT_HOUR_OF_DAY0_FIELD,
  823. kMinuteField = UDAT_MINUTE_FIELD,
  824. kSecondField = UDAT_SECOND_FIELD,
  825. kMillisecondField = UDAT_FRACTIONAL_SECOND_FIELD,
  826. kDayOfWeekField = UDAT_DAY_OF_WEEK_FIELD,
  827. kDayOfYearField = UDAT_DAY_OF_YEAR_FIELD,
  828. kDayOfWeekInMonthField = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
  829. kWeekOfYearField = UDAT_WEEK_OF_YEAR_FIELD,
  830. kWeekOfMonthField = UDAT_WEEK_OF_MONTH_FIELD,
  831. kAmPmField = UDAT_AM_PM_FIELD,
  832. kHour1Field = UDAT_HOUR1_FIELD,
  833. kHour0Field = UDAT_HOUR0_FIELD,
  834. kTimezoneField = UDAT_TIMEZONE_FIELD,
  835. kYearWOYField = UDAT_YEAR_WOY_FIELD,
  836. kDOWLocalField = UDAT_DOW_LOCAL_FIELD,
  837. kExtendedYearField = UDAT_EXTENDED_YEAR_FIELD,
  838. kJulianDayField = UDAT_JULIAN_DAY_FIELD,
  839. kMillisecondsInDayField = UDAT_MILLISECONDS_IN_DAY_FIELD,
  840. // Obsolete; use UDateFormatField instead
  841. ERA_FIELD = UDAT_ERA_FIELD,
  842. YEAR_FIELD = UDAT_YEAR_FIELD,
  843. MONTH_FIELD = UDAT_MONTH_FIELD,
  844. DATE_FIELD = UDAT_DATE_FIELD,
  845. HOUR_OF_DAY1_FIELD = UDAT_HOUR_OF_DAY1_FIELD,
  846. HOUR_OF_DAY0_FIELD = UDAT_HOUR_OF_DAY0_FIELD,
  847. MINUTE_FIELD = UDAT_MINUTE_FIELD,
  848. SECOND_FIELD = UDAT_SECOND_FIELD,
  849. MILLISECOND_FIELD = UDAT_FRACTIONAL_SECOND_FIELD,
  850. DAY_OF_WEEK_FIELD = UDAT_DAY_OF_WEEK_FIELD,
  851. DAY_OF_YEAR_FIELD = UDAT_DAY_OF_YEAR_FIELD,
  852. DAY_OF_WEEK_IN_MONTH_FIELD = UDAT_DAY_OF_WEEK_IN_MONTH_FIELD,
  853. WEEK_OF_YEAR_FIELD = UDAT_WEEK_OF_YEAR_FIELD,
  854. WEEK_OF_MONTH_FIELD = UDAT_WEEK_OF_MONTH_FIELD,
  855. AM_PM_FIELD = UDAT_AM_PM_FIELD,
  856. HOUR1_FIELD = UDAT_HOUR1_FIELD,
  857. HOUR0_FIELD = UDAT_HOUR0_FIELD,
  858. TIMEZONE_FIELD = UDAT_TIMEZONE_FIELD
  859. };
  860. #endif /* U_HIDE_OBSOLETE_API */
  861. };
  862. U_NAMESPACE_END
  863. #endif /* #if !UCONFIG_NO_FORMATTING */
  864. #endif // _DATEFMT
  865. //eof