datefmt.h 41 KB

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