datefmt.h 40 KB

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