smpdtfmt.h 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. * Copyright (C) 1997-2016, International Business Machines Corporation and
  5. * others. All Rights Reserved.
  6. *******************************************************************************
  7. *
  8. * File SMPDTFMT.H
  9. *
  10. * Modification History:
  11. *
  12. * Date Name Description
  13. * 02/19/97 aliu Converted from java.
  14. * 07/09/97 helena Make ParsePosition into a class.
  15. * 07/21/98 stephen Added GMT_PLUS, GMT_MINUS
  16. * Changed setTwoDigitStartDate to set2DigitYearStart
  17. * Changed getTwoDigitStartDate to get2DigitYearStart
  18. * Removed subParseLong
  19. * Removed getZoneIndex (added in DateFormatSymbols)
  20. * 06/14/99 stephen Removed fgTimeZoneDataSuffix
  21. * 10/14/99 aliu Updated class doc to describe 2-digit year parsing
  22. * {j28 4182066}.
  23. *******************************************************************************
  24. */
  25. #ifndef SMPDTFMT_H
  26. #define SMPDTFMT_H
  27. #include "unicode/utypes.h"
  28. #if U_SHOW_CPLUSPLUS_API
  29. /**
  30. * \file
  31. * \brief C++ API: Format and parse dates in a language-independent manner.
  32. */
  33. #if !UCONFIG_NO_FORMATTING
  34. #include "unicode/datefmt.h"
  35. #include "unicode/udisplaycontext.h"
  36. #include "unicode/tzfmt.h" /* for UTimeZoneFormatTimeType */
  37. #include "unicode/brkiter.h"
  38. U_NAMESPACE_BEGIN
  39. class DateFormatSymbols;
  40. class DateFormat;
  41. class MessageFormat;
  42. class FieldPositionHandler;
  43. class TimeZoneFormat;
  44. class SharedNumberFormat;
  45. class SimpleDateFormatMutableNFs;
  46. class DateIntervalFormat;
  47. namespace number {
  48. class LocalizedNumberFormatter;
  49. class SimpleNumberFormatter;
  50. }
  51. /**
  52. *
  53. * SimpleDateFormat is a concrete class for formatting and parsing dates in a
  54. * language-independent manner. It allows for formatting (millis -> text),
  55. * parsing (text -> millis), and normalization. Formats/Parses a date or time,
  56. * which is the standard milliseconds since 24:00 GMT, Jan 1, 1970.
  57. * <P>
  58. * Clients are encouraged to create a date-time formatter using DateFormat::getInstance(),
  59. * getDateInstance(), getDateInstance(), or getDateTimeInstance() rather than
  60. * explicitly constructing an instance of SimpleDateFormat. This way, the client
  61. * is guaranteed to get an appropriate formatting pattern for whatever locale the
  62. * program is running in. If the client needs more control, they should consider using
  63. * DateFormat::createInstanceForSkeleton(). However, if the client needs something more unusual than
  64. * the default patterns in the locales, he can construct a SimpleDateFormat directly
  65. * and give it an appropriate pattern (or use one of the factory methods on DateFormat
  66. * and modify the pattern after the fact with toPattern() and applyPattern().
  67. *
  68. * <p><strong>Date and Time Patterns:</strong></p>
  69. *
  70. * <p>Date and time formats are specified by <em>date and time pattern</em> strings.
  71. * The full syntax for date and time patterns can be found at
  72. * <a href="https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns">https://unicode.org/reports/tr35/tr35-dates.html#Date_Format_Patterns</a>.
  73. *
  74. * Within date and time pattern strings, all unquoted ASCII letters [A-Za-z] are reserved
  75. * as pattern letters representing calendar fields. Some of the most commonly used pattern letters are:</p>
  76. *
  77. * <table border="1">
  78. * <tr>
  79. * <th style="text-align: center">Sym.</th>
  80. * <th style="text-align: center">No.</th>
  81. * <th>Example</th>
  82. * <th>Description</th>
  83. * </tr>
  84. * <tr>
  85. * <td style="text-align: center">G</td>
  86. * <td style="text-align: center">1..3</td>
  87. * <td>AD</td>
  88. * <td>Era - Replaced with the Era string for the current date. One to three letters for the
  89. * abbreviated form, four letters for the long (wide) form, five for the narrow form.</td>
  90. * </tr>
  91. * <tr>
  92. * <td style="text-align: center">y</td>
  93. * <td style="text-align: center">1..n</td>
  94. * <td>1996</td>
  95. * <td>Year. Normally the length specifies the padding, but for two letters it also specifies the maximum
  96. * length. Example:<div style="text-align: center">
  97. * <center>
  98. * <table border="1" cellpadding="2" cellspacing="0">
  99. * <tr>
  100. * <th>Year</th>
  101. * <th style="text-align: right">y</th>
  102. * <th style="text-align: right">yy</th>
  103. * <th style="text-align: right">yyy</th>
  104. * <th style="text-align: right">yyyy</th>
  105. * <th style="text-align: right">yyyyy</th>
  106. * </tr>
  107. * <tr>
  108. * <td>AD 1</td>
  109. * <td style="text-align: right">1</td>
  110. * <td style="text-align: right">01</td>
  111. * <td style="text-align: right">001</td>
  112. * <td style="text-align: right">0001</td>
  113. * <td style="text-align: right">00001</td>
  114. * </tr>
  115. * <tr>
  116. * <td>AD 12</td>
  117. * <td style="text-align: right">12</td>
  118. * <td style="text-align: right">12</td>
  119. * <td style="text-align: right">012</td>
  120. * <td style="text-align: right">0012</td>
  121. * <td style="text-align: right">00012</td>
  122. * </tr>
  123. * <tr>
  124. * <td>AD 123</td>
  125. * <td style="text-align: right">123</td>
  126. * <td style="text-align: right">23</td>
  127. * <td style="text-align: right">123</td>
  128. * <td style="text-align: right">0123</td>
  129. * <td style="text-align: right">00123</td>
  130. * </tr>
  131. * <tr>
  132. * <td>AD 1234</td>
  133. * <td style="text-align: right">1234</td>
  134. * <td style="text-align: right">34</td>
  135. * <td style="text-align: right">1234</td>
  136. * <td style="text-align: right">1234</td>
  137. * <td style="text-align: right">01234</td>
  138. * </tr>
  139. * <tr>
  140. * <td>AD 12345</td>
  141. * <td style="text-align: right">12345</td>
  142. * <td style="text-align: right">45</td>
  143. * <td style="text-align: right">12345</td>
  144. * <td style="text-align: right">12345</td>
  145. * <td style="text-align: right">12345</td>
  146. * </tr>
  147. * </table>
  148. * </center></div>
  149. * </td>
  150. * </tr>
  151. * <tr>
  152. * <td rowspan="3" style="text-align: center">Q</td>
  153. * <td style="text-align: center">1..2</td>
  154. * <td>02</td>
  155. * <td rowspan="3">Quarter - Use one or two for the numerical quarter, three for the abbreviation, or four
  156. * for the full (wide) name (five for the narrow name is not yet supported).</td>
  157. * </tr>
  158. * <tr>
  159. * <td style="text-align: center">3</td>
  160. * <td>Q2</td>
  161. * </tr>
  162. * <tr>
  163. * <td style="text-align: center">4</td>
  164. * <td>2nd quarter</td>
  165. * </tr>
  166. * <tr>
  167. * <td rowspan="4" style="text-align: center">M</td>
  168. * <td style="text-align: center">1..2</td>
  169. * <td>09</td>
  170. * <td rowspan="4">Month - Use one or two for the numerical month, three for the abbreviation, four for
  171. * the full (wide) name, or five for the narrow name. With two ("MM"), the month number is zero-padded
  172. * if necessary (e.g. "08").</td>
  173. * </tr>
  174. * <tr>
  175. * <td style="text-align: center">3</td>
  176. * <td>Sep</td>
  177. * </tr>
  178. * <tr>
  179. * <td style="text-align: center">4</td>
  180. * <td>September</td>
  181. * </tr>
  182. * <tr>
  183. * <td style="text-align: center">5</td>
  184. * <td>S</td>
  185. * </tr>
  186. * <tr>
  187. * <td style="text-align: center">d</td>
  188. * <td style="text-align: center">1..2</td>
  189. * <td>1</td>
  190. * <td>Date - Day of the month. Use "d" to show the minimum number of digits, or "dd" to always show
  191. * two digits (zero-padding if necessary, e.g. "08").</td>
  192. * </tr>
  193. * <tr>
  194. * <td rowspan="4" style="text-align: center">E</td>
  195. * <td style="text-align: center">1..3</td>
  196. * <td>Tue</td>
  197. * <td rowspan="4">Day of week - Use one through three letters for the short day, four for the full (wide) name,
  198. * five for the narrow name, or six for the short name.</td>
  199. * </tr>
  200. * <tr>
  201. * <td style="text-align: center">4</td>
  202. * <td>Tuesday</td>
  203. * </tr>
  204. * <tr>
  205. * <td style="text-align: center">5</td>
  206. * <td>T</td>
  207. * </tr>
  208. * <tr>
  209. * <td style="text-align: center">6</td>
  210. * <td>Tu</td>
  211. * </tr>
  212. * <tr>
  213. * <td style="text-align: center">a</td>
  214. * <td style="text-align: center">1</td>
  215. * <td>AM</td>
  216. * <td>AM or PM</td>
  217. * </tr>
  218. * <tr>
  219. * <td style="text-align: center">h</td>
  220. * <td style="text-align: center">1..2</td>
  221. * <td>11</td>
  222. * <td>Hour [1-12]. When used in skeleton data or in a skeleton passed in an API for flexible data pattern
  223. * generation, it should match the 12-hour-cycle format preferred by the locale (h or K); it should not match
  224. * a 24-hour-cycle format (H or k). Use hh for zero padding.</td>
  225. * </tr>
  226. * <tr>
  227. * <td style="text-align: center">H</td>
  228. * <td style="text-align: center">1..2</td>
  229. * <td>13</td>
  230. * <td>Hour [0-23]. When used in skeleton data or in a skeleton passed in an API for flexible data pattern
  231. * generation, it should match the 24-hour-cycle format preferred by the locale (H or k); it should not match a
  232. * 12-hour-cycle format (h or K). Use HH for zero padding.</td>
  233. * </tr>
  234. * <tr>
  235. * <td style="text-align: center">m</td>
  236. * <td style="text-align: center">1..2</td>
  237. * <td>59</td>
  238. * <td>Minute. Use "m" to show the minimum number of digits, or "mm" to always show two digits
  239. * (zero-padding if necessary, e.g. "08")..</td>
  240. * </tr>
  241. * <tr>
  242. * <td style="text-align: center">s</td>
  243. * <td style="text-align: center">1..2</td>
  244. * <td>12</td>
  245. * <td>Second. Use "s" to show the minimum number of digits, or "ss" to always show two digits
  246. * (zero-padding if necessary, e.g. "08").</td>
  247. * </tr>
  248. * <tr>
  249. * <td rowspan="2" style="text-align: center">z</td>
  250. * <td style="text-align: center">1..3</td>
  251. * <td>PDT</td>
  252. * <td>Time zone. The <i>short specific non-location format</i>.
  253. * Where that is unavailable, falls back to the <i>short localized GMT format</i> ("O").</td>
  254. * </tr>
  255. * <tr>
  256. * <td style="text-align: center">4</td>
  257. * <td>Pacific Daylight Time</td>
  258. * <td>The <i>long specific non-location format</i>.
  259. * Where that is unavailable, falls back to the <i>long localized GMT format</i> ("OOOO").</td>
  260. * </tr>
  261. * <tr>
  262. * <td rowspan="2" style="text-align: center">v</td>
  263. * <td style="text-align: center">1</td>
  264. * <td>PT</td>
  265. * <td>Time zone. The <i>short generic non-location format</i>.
  266. * Where that is unavailable, falls back to the <i>generic location format</i> ("VVVV"),
  267. * then the <i>short localized GMT format</i> as the final fallback.</td>
  268. * </tr>
  269. * <tr>
  270. * <td style="text-align: center">4</td>
  271. * <td>Pacific Time</td>
  272. * <td>The <i>long generic non-location format</i>.
  273. * Where that is unavailable, falls back to <i>generic location format</i> ("VVVV").
  274. * </tr>
  275. * </table>
  276. *
  277. * <P>
  278. * Any characters in the pattern that are not in the ranges of ['a'..'z'] and
  279. * ['A'..'Z'] will be treated as quoted text. For instance, characters
  280. * like ':', '.', ' ', '#' and '@' will appear in the resulting time text
  281. * even they are not embraced within single quotes.
  282. * <P>
  283. * A pattern containing any invalid pattern letter will result in a failing
  284. * UErrorCode result during formatting or parsing.
  285. * <P>
  286. * Examples using the US locale:
  287. * <pre>
  288. * \code
  289. * Format Pattern Result
  290. * -------------- -------
  291. * "yyyy.MM.dd G 'at' HH:mm:ss vvvv" ->> 1996.07.10 AD at 15:08:56 Pacific Time
  292. * "EEE, MMM d, ''yy" ->> Wed, July 10, '96
  293. * "h:mm a" ->> 12:08 PM
  294. * "hh 'o''clock' a, zzzz" ->> 12 o'clock PM, Pacific Daylight Time
  295. * "K:mm a, vvv" ->> 0:00 PM, PT
  296. * "yyyyy.MMMMM.dd GGG hh:mm aaa" ->> 1996.July.10 AD 12:08 PM
  297. * \endcode
  298. * </pre>
  299. * Code Sample:
  300. * <pre>
  301. * \code
  302. * UErrorCode success = U_ZERO_ERROR;
  303. * SimpleTimeZone* pdt = new SimpleTimeZone(-8 * 60 * 60 * 1000, "PST");
  304. * pdt->setStartRule( Calendar::APRIL, 1, Calendar::SUNDAY, 2*60*60*1000);
  305. * pdt->setEndRule( Calendar::OCTOBER, -1, Calendar::SUNDAY, 2*60*60*1000);
  306. *
  307. * // Format the current time.
  308. * SimpleDateFormat* formatter
  309. * = new SimpleDateFormat ("yyyy.MM.dd G 'at' hh:mm:ss a zzz", success );
  310. * GregorianCalendar cal(success);
  311. * UDate currentTime_1 = cal.getTime(success);
  312. * FieldPosition fp(FieldPosition::DONT_CARE);
  313. * UnicodeString dateString;
  314. * formatter->format( currentTime_1, dateString, fp );
  315. * cout << "result: " << dateString << endl;
  316. *
  317. * // Parse the previous string back into a Date.
  318. * ParsePosition pp(0);
  319. * UDate currentTime_2 = formatter->parse(dateString, pp );
  320. * \endcode
  321. * </pre>
  322. * In the above example, the time value "currentTime_2" obtained from parsing
  323. * will be equal to currentTime_1. However, they may not be equal if the am/pm
  324. * marker 'a' is left out from the format pattern while the "hour in am/pm"
  325. * pattern symbol is used. This information loss can happen when formatting the
  326. * time in PM.
  327. *
  328. * <p>
  329. * When parsing a date string using the abbreviated year pattern ("y" or "yy"),
  330. * SimpleDateFormat must interpret the abbreviated year
  331. * relative to some century. It does this by adjusting dates to be
  332. * within 80 years before and 20 years after the time the SimpleDateFormat
  333. * instance is created. For example, using a pattern of "MM/dd/yy" and a
  334. * SimpleDateFormat instance created on Jan 1, 1997, the string
  335. * "01/11/12" would be interpreted as Jan 11, 2012 while the string "05/04/64"
  336. * would be interpreted as May 4, 1964.
  337. * During parsing, only strings consisting of exactly two digits, as defined by
  338. * <code>Unicode::isDigit()</code>, will be parsed into the default century.
  339. * Any other numeric string, such as a one digit string, a three or more digit
  340. * string, or a two digit string that isn't all digits (for example, "-1"), is
  341. * interpreted literally. So "01/02/3" or "01/02/003" are parsed (for the
  342. * Gregorian calendar), using the same pattern, as Jan 2, 3 AD. Likewise (but
  343. * only in lenient parse mode, the default) "01/02/-3" is parsed as Jan 2, 4 BC.
  344. *
  345. * <p>
  346. * If the year pattern has more than two 'y' characters, the year is
  347. * interpreted literally, regardless of the number of digits. So using the
  348. * pattern "MM/dd/yyyy", "01/11/12" parses to Jan 11, 12 A.D.
  349. *
  350. * <p>
  351. * When numeric fields abut one another directly, with no intervening delimiter
  352. * characters, they constitute a run of abutting numeric fields. Such runs are
  353. * parsed specially. For example, the format "HHmmss" parses the input text
  354. * "123456" to 12:34:56, parses the input text "12345" to 1:23:45, and fails to
  355. * parse "1234". In other words, the leftmost field of the run is flexible,
  356. * while the others keep a fixed width. If the parse fails anywhere in the run,
  357. * then the leftmost field is shortened by one character, and the entire run is
  358. * parsed again. This is repeated until either the parse succeeds or the
  359. * leftmost field is one character in length. If the parse still fails at that
  360. * point, the parse of the run fails.
  361. *
  362. * <P>
  363. * For time zones that have no names, SimpleDateFormat uses strings GMT+hours:minutes or
  364. * GMT-hours:minutes.
  365. * <P>
  366. * The calendar defines what is the first day of the week, the first week of the
  367. * year, whether hours are zero based or not (0 vs 12 or 24), and the timezone.
  368. * There is one common number format to handle all the numbers; the digit count
  369. * is handled programmatically according to the pattern.
  370. *
  371. * <p><em>User subclasses are not supported.</em> While clients may write
  372. * subclasses, such code will not necessarily work and will not be
  373. * guaranteed to work stably from release to release.
  374. */
  375. class U_I18N_API SimpleDateFormat: public DateFormat {
  376. public:
  377. /**
  378. * Construct a SimpleDateFormat using the default pattern for the default
  379. * locale.
  380. * <P>
  381. * [Note:] Not all locales support SimpleDateFormat; for full generality,
  382. * use the factory methods in the DateFormat class.
  383. * @param status Output param set to success/failure code.
  384. * @stable ICU 2.0
  385. */
  386. SimpleDateFormat(UErrorCode& status);
  387. /**
  388. * Construct a SimpleDateFormat using the given pattern and the default locale.
  389. * The locale is used to obtain the symbols used in formatting (e.g., the
  390. * names of the months), but not to provide the pattern.
  391. * <P>
  392. * [Note:] Not all locales support SimpleDateFormat; for full generality,
  393. * use the factory methods in the DateFormat class.
  394. * @param pattern the pattern for the format.
  395. * @param status Output param set to success/failure code.
  396. * @stable ICU 2.0
  397. */
  398. SimpleDateFormat(const UnicodeString& pattern,
  399. UErrorCode& status);
  400. /**
  401. * Construct a SimpleDateFormat using the given pattern, numbering system override, and the default locale.
  402. * The locale is used to obtain the symbols used in formatting (e.g., the
  403. * names of the months), but not to provide the pattern.
  404. * <P>
  405. * A numbering system override is a string containing either the name of a known numbering system,
  406. * or a set of field and numbering system pairs that specify which fields are to be formatted with
  407. * the alternate numbering system. For example, to specify that all numeric fields in the specified
  408. * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
  409. * as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering,
  410. * use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon
  411. * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
  412. *
  413. * <P>
  414. * [Note:] Not all locales support SimpleDateFormat; for full generality,
  415. * use the factory methods in the DateFormat class.
  416. * @param pattern the pattern for the format.
  417. * @param override the override string.
  418. * @param status Output param set to success/failure code.
  419. * @stable ICU 4.2
  420. */
  421. SimpleDateFormat(const UnicodeString& pattern,
  422. const UnicodeString& override,
  423. UErrorCode& status);
  424. /**
  425. * Construct a SimpleDateFormat using the given pattern and locale.
  426. * The locale is used to obtain the symbols used in formatting (e.g., the
  427. * names of the months), but not to provide the pattern.
  428. * <P>
  429. * [Note:] Not all locales support SimpleDateFormat; for full generality,
  430. * use the factory methods in the DateFormat class.
  431. * @param pattern the pattern for the format.
  432. * @param locale the given locale.
  433. * @param status Output param set to success/failure code.
  434. * @stable ICU 2.0
  435. */
  436. SimpleDateFormat(const UnicodeString& pattern,
  437. const Locale& locale,
  438. UErrorCode& status);
  439. /**
  440. * Construct a SimpleDateFormat using the given pattern, numbering system override, and locale.
  441. * The locale is used to obtain the symbols used in formatting (e.g., the
  442. * names of the months), but not to provide the pattern.
  443. * <P>
  444. * A numbering system override is a string containing either the name of a known numbering system,
  445. * or a set of field and numbering system pairs that specify which fields are to be formatted with
  446. * the alternate numbering system. For example, to specify that all numeric fields in the specified
  447. * date or time pattern are to be rendered using Thai digits, simply specify the numbering system override
  448. * as "thai". To specify that just the year portion of the date be formatted using Hebrew numbering,
  449. * use the override string "y=hebrew". Numbering system overrides can be combined using a semi-colon
  450. * character in the override string, such as "d=decimal;M=arabic;y=hebrew", etc.
  451. * <P>
  452. * [Note:] Not all locales support SimpleDateFormat; for full generality,
  453. * use the factory methods in the DateFormat class.
  454. * @param pattern the pattern for the format.
  455. * @param override the numbering system override.
  456. * @param locale the given locale.
  457. * @param status Output param set to success/failure code.
  458. * @stable ICU 4.2
  459. */
  460. SimpleDateFormat(const UnicodeString& pattern,
  461. const UnicodeString& override,
  462. const Locale& locale,
  463. UErrorCode& status);
  464. /**
  465. * Construct a SimpleDateFormat using the given pattern and locale-specific
  466. * symbol data. The formatter takes ownership of the DateFormatSymbols object;
  467. * the caller is no longer responsible for deleting it.
  468. * @param pattern the given pattern for the format.
  469. * @param formatDataToAdopt the symbols to be adopted.
  470. * @param status Output param set to success/faulure code.
  471. * @stable ICU 2.0
  472. */
  473. SimpleDateFormat(const UnicodeString& pattern,
  474. DateFormatSymbols* formatDataToAdopt,
  475. UErrorCode& status);
  476. /**
  477. * Construct a SimpleDateFormat using the given pattern and locale-specific
  478. * symbol data. The DateFormatSymbols object is NOT adopted; the caller
  479. * remains responsible for deleting it.
  480. * @param pattern the given pattern for the format.
  481. * @param formatData the formatting symbols to be use.
  482. * @param status Output param set to success/faulure code.
  483. * @stable ICU 2.0
  484. */
  485. SimpleDateFormat(const UnicodeString& pattern,
  486. const DateFormatSymbols& formatData,
  487. UErrorCode& status);
  488. /**
  489. * Copy constructor.
  490. * @stable ICU 2.0
  491. */
  492. SimpleDateFormat(const SimpleDateFormat&);
  493. /**
  494. * Assignment operator.
  495. * @stable ICU 2.0
  496. */
  497. SimpleDateFormat& operator=(const SimpleDateFormat&);
  498. /**
  499. * Destructor.
  500. * @stable ICU 2.0
  501. */
  502. virtual ~SimpleDateFormat();
  503. /**
  504. * Clone this Format object polymorphically. The caller owns the result and
  505. * should delete it when done.
  506. * @return A copy of the object.
  507. * @stable ICU 2.0
  508. */
  509. virtual SimpleDateFormat* clone() const override;
  510. /**
  511. * Return true if the given Format objects are semantically equal. Objects
  512. * of different subclasses are considered unequal.
  513. * @param other the object to be compared with.
  514. * @return true if the given Format objects are semantically equal.
  515. * @stable ICU 2.0
  516. */
  517. virtual bool operator==(const Format& other) const override;
  518. using DateFormat::format;
  519. /**
  520. * Format a date or time, which is the standard millis since 24:00 GMT, Jan
  521. * 1, 1970. Overrides DateFormat pure virtual method.
  522. * <P>
  523. * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
  524. * 1996.07.10 AD at 15:08:56 PDT
  525. *
  526. * @param cal Calendar set to the date and time to be formatted
  527. * into a date/time string.
  528. * @param appendTo Output parameter to receive result.
  529. * Result is appended to existing contents.
  530. * @param pos The formatting position. On input: an alignment field,
  531. * if desired. On output: the offsets of the alignment field.
  532. * @return Reference to 'appendTo' parameter.
  533. * @stable ICU 2.1
  534. */
  535. virtual UnicodeString& format( Calendar& cal,
  536. UnicodeString& appendTo,
  537. FieldPosition& pos) const override;
  538. /**
  539. * Format a date or time, which is the standard millis since 24:00 GMT, Jan
  540. * 1, 1970. Overrides DateFormat pure virtual method.
  541. * <P>
  542. * Example: using the US locale: "yyyy.MM.dd e 'at' HH:mm:ss zzz" ->>
  543. * 1996.07.10 AD at 15:08:56 PDT
  544. *
  545. * @param cal Calendar set to the date and time to be formatted
  546. * into a date/time string.
  547. * @param appendTo Output parameter to receive result.
  548. * Result is appended to existing contents.
  549. * @param posIter On return, can be used to iterate over positions
  550. * of fields generated by this format call. Field values
  551. * are defined in UDateFormatField.
  552. * @param status Input/output param set to success/failure code.
  553. * @return Reference to 'appendTo' parameter.
  554. * @stable ICU 4.4
  555. */
  556. virtual UnicodeString& format( Calendar& cal,
  557. UnicodeString& appendTo,
  558. FieldPositionIterator* posIter,
  559. UErrorCode& status) const override;
  560. using DateFormat::parse;
  561. /**
  562. * Parse a date/time string beginning at the given parse position. For
  563. * example, a time text "07/10/96 4:5 PM, PDT" will be parsed into a Date
  564. * that is equivalent to Date(837039928046).
  565. * <P>
  566. * By default, parsing is lenient: If the input is not in the form used by
  567. * this object's format method but can still be parsed as a date, then the
  568. * parse succeeds. Clients may insist on strict adherence to the format by
  569. * calling setLenient(false).
  570. * @see DateFormat::setLenient(boolean)
  571. *
  572. * @param text The date/time string to be parsed
  573. * @param cal A Calendar set on input to the date and time to be used for
  574. * missing values in the date/time string being parsed, and set
  575. * on output to the parsed date/time. When the calendar type is
  576. * different from the internal calendar held by this SimpleDateFormat
  577. * instance, the internal calendar will be cloned to a work
  578. * calendar set to the same milliseconds and time zone as the
  579. * cal parameter, field values will be parsed based on the work
  580. * calendar, then the result (milliseconds and time zone) will
  581. * be set in this calendar.
  582. * @param pos On input, the position at which to start parsing; on
  583. * output, the position at which parsing terminated, or the
  584. * start position if the parse failed.
  585. * @stable ICU 2.1
  586. */
  587. virtual void parse( const UnicodeString& text,
  588. Calendar& cal,
  589. ParsePosition& pos) const override;
  590. /**
  591. * Set the start UDate used to interpret two-digit year strings.
  592. * When dates are parsed having 2-digit year strings, they are placed within
  593. * a assumed range of 100 years starting on the two digit start date. For
  594. * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
  595. * some other year. SimpleDateFormat chooses a year so that the resultant
  596. * date is on or after the two digit start date and within 100 years of the
  597. * two digit start date.
  598. * <P>
  599. * By default, the two digit start date is set to 80 years before the current
  600. * time at which a SimpleDateFormat object is created.
  601. * @param d start UDate used to interpret two-digit year strings.
  602. * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
  603. * an error value if there was a parse error.
  604. * @stable ICU 2.0
  605. */
  606. virtual void set2DigitYearStart(UDate d, UErrorCode& status);
  607. /**
  608. * Get the start UDate used to interpret two-digit year strings.
  609. * When dates are parsed having 2-digit year strings, they are placed within
  610. * a assumed range of 100 years starting on the two digit start date. For
  611. * example, the string "24-Jan-17" may be in the year 1817, 1917, 2017, or
  612. * some other year. SimpleDateFormat chooses a year so that the resultant
  613. * date is on or after the two digit start date and within 100 years of the
  614. * two digit start date.
  615. * <P>
  616. * By default, the two digit start date is set to 80 years before the current
  617. * time at which a SimpleDateFormat object is created.
  618. * @param status Filled in with U_ZERO_ERROR if the parse was successful, and with
  619. * an error value if there was a parse error.
  620. * @stable ICU 2.0
  621. */
  622. UDate get2DigitYearStart(UErrorCode& status) const;
  623. /**
  624. * Return a pattern string describing this date format.
  625. * @param result Output param to receive the pattern.
  626. * @return A reference to 'result'.
  627. * @stable ICU 2.0
  628. */
  629. virtual UnicodeString& toPattern(UnicodeString& result) const;
  630. /**
  631. * Return a localized pattern string describing this date format.
  632. * In most cases, this will return the same thing as toPattern(),
  633. * but a locale can specify characters to use in pattern descriptions
  634. * in place of the ones described in this class's class documentation.
  635. * (Presumably, letters that would be more mnemonic in that locale's
  636. * language.) This function would produce a pattern using those
  637. * letters.
  638. * <p>
  639. * <b>Note:</b> This implementation depends on DateFormatSymbols::getLocalPatternChars()
  640. * to get localized format pattern characters. ICU does not include
  641. * localized pattern character data, therefore, unless user sets localized
  642. * pattern characters manually, this method returns the same result as
  643. * toPattern().
  644. *
  645. * @param result Receives the localized pattern.
  646. * @param status Output param set to success/failure code on
  647. * exit. If the pattern is invalid, this will be
  648. * set to a failure result.
  649. * @return A reference to 'result'.
  650. * @stable ICU 2.0
  651. */
  652. virtual UnicodeString& toLocalizedPattern(UnicodeString& result,
  653. UErrorCode& status) const;
  654. /**
  655. * Apply the given unlocalized pattern string to this date format.
  656. * (i.e., after this call, this formatter will format dates according to
  657. * the new pattern)
  658. *
  659. * @param pattern The pattern to be applied.
  660. * @stable ICU 2.0
  661. */
  662. virtual void applyPattern(const UnicodeString& pattern);
  663. /**
  664. * Apply the given localized pattern string to this date format.
  665. * (see toLocalizedPattern() for more information on localized patterns.)
  666. *
  667. * @param pattern The localized pattern to be applied.
  668. * @param status Output param set to success/failure code on
  669. * exit. If the pattern is invalid, this will be
  670. * set to a failure result.
  671. * @stable ICU 2.0
  672. */
  673. virtual void applyLocalizedPattern(const UnicodeString& pattern,
  674. UErrorCode& status);
  675. /**
  676. * Gets the date/time formatting symbols (this is an object carrying
  677. * the various strings and other symbols used in formatting: e.g., month
  678. * names and abbreviations, time zone names, AM/PM strings, etc.)
  679. * @return a copy of the date-time formatting data associated
  680. * with this date-time formatter.
  681. * @stable ICU 2.0
  682. */
  683. virtual const DateFormatSymbols* getDateFormatSymbols() const;
  684. /**
  685. * Set the date/time formatting symbols. The caller no longer owns the
  686. * DateFormatSymbols object and should not delete it after making this call.
  687. * @param newFormatSymbols the given date-time formatting symbols to copy.
  688. * @stable ICU 2.0
  689. */
  690. virtual void adoptDateFormatSymbols(DateFormatSymbols* newFormatSymbols);
  691. /**
  692. * Set the date/time formatting data.
  693. * @param newFormatSymbols the given date-time formatting symbols to copy.
  694. * @stable ICU 2.0
  695. */
  696. virtual void setDateFormatSymbols(const DateFormatSymbols& newFormatSymbols);
  697. /**
  698. * Return the class ID for this class. This is useful only for comparing to
  699. * a return value from getDynamicClassID(). For example:
  700. * <pre>
  701. * . Base* polymorphic_pointer = createPolymorphicObject();
  702. * . if (polymorphic_pointer->getDynamicClassID() ==
  703. * . erived::getStaticClassID()) ...
  704. * </pre>
  705. * @return The class ID for all objects of this class.
  706. * @stable ICU 2.0
  707. */
  708. static UClassID U_EXPORT2 getStaticClassID();
  709. /**
  710. * Returns a unique class ID POLYMORPHICALLY. Pure virtual override. This
  711. * method is to implement a simple version of RTTI, since not all C++
  712. * compilers support genuine RTTI. Polymorphic operator==() and clone()
  713. * methods call this method.
  714. *
  715. * @return The class ID for this object. All objects of a
  716. * given class have the same class ID. Objects of
  717. * other classes have different class IDs.
  718. * @stable ICU 2.0
  719. */
  720. virtual UClassID getDynamicClassID() const override;
  721. /**
  722. * Set the calendar to be used by this date format. Initially, the default
  723. * calendar for the specified or default locale is used. The caller should
  724. * not delete the Calendar object after it is adopted by this call.
  725. * Adopting a new calendar will change to the default symbols.
  726. *
  727. * @param calendarToAdopt Calendar object to be adopted.
  728. * @stable ICU 2.0
  729. */
  730. virtual void adoptCalendar(Calendar* calendarToAdopt) override;
  731. /* Cannot use #ifndef U_HIDE_INTERNAL_API for the following methods since they are virtual */
  732. /**
  733. * Sets the TimeZoneFormat to be used by this date/time formatter.
  734. * The caller should not delete the TimeZoneFormat object after
  735. * it is adopted by this call.
  736. * @param timeZoneFormatToAdopt The TimeZoneFormat object to be adopted.
  737. * @internal ICU 49 technology preview
  738. */
  739. virtual void adoptTimeZoneFormat(TimeZoneFormat* timeZoneFormatToAdopt);
  740. /**
  741. * Sets the TimeZoneFormat to be used by this date/time formatter.
  742. * @param newTimeZoneFormat The TimeZoneFormat object to copy.
  743. * @internal ICU 49 technology preview
  744. */
  745. virtual void setTimeZoneFormat(const TimeZoneFormat& newTimeZoneFormat);
  746. /**
  747. * Gets the time zone format object associated with this date/time formatter.
  748. * @return the time zone format associated with this date/time formatter.
  749. * @internal ICU 49 technology preview
  750. */
  751. virtual const TimeZoneFormat* getTimeZoneFormat() const;
  752. /**
  753. * Set a particular UDisplayContext value in the formatter, such as
  754. * UDISPCTX_CAPITALIZATION_FOR_STANDALONE. Note: For getContext, see
  755. * DateFormat.
  756. * @param value The UDisplayContext value to set.
  757. * @param status Input/output status. If at entry this indicates a failure
  758. * status, the function will do nothing; otherwise this will be
  759. * updated with any new status from the function.
  760. * @stable ICU 53
  761. */
  762. virtual void setContext(UDisplayContext value, UErrorCode& status) override;
  763. /**
  764. * Overrides base class method and
  765. * This method clears per field NumberFormat instances
  766. * previously set by {@see adoptNumberFormat(const UnicodeString&, NumberFormat*, UErrorCode)}
  767. * @param formatToAdopt the NumbeferFormat used
  768. * @stable ICU 54
  769. */
  770. void adoptNumberFormat(NumberFormat *formatToAdopt) override;
  771. /**
  772. * Allow the user to set the NumberFormat for several fields
  773. * It can be a single field like: "y"(year) or "M"(month)
  774. * It can be several field combined together: "yM"(year and month)
  775. * Note:
  776. * 1 symbol field is enough for multiple symbol field (so "y" will override "yy", "yyy")
  777. * If the field is not numeric, then override has no effect (like "MMM" will use abbreviation, not numerical field)
  778. * Per field NumberFormat can also be cleared in {@see DateFormat::setNumberFormat(const NumberFormat& newNumberFormat)}
  779. *
  780. * @param fields the fields to override(like y)
  781. * @param formatToAdopt the NumbeferFormat used
  782. * @param status Receives a status code, which will be U_ZERO_ERROR
  783. * if the operation succeeds.
  784. * @stable ICU 54
  785. */
  786. void adoptNumberFormat(const UnicodeString& fields, NumberFormat *formatToAdopt, UErrorCode &status);
  787. /**
  788. * Get the numbering system to be used for a particular field.
  789. * @param field The UDateFormatField to get
  790. * @stable ICU 54
  791. */
  792. const NumberFormat * getNumberFormatForField(char16_t field) const;
  793. #ifndef U_HIDE_INTERNAL_API
  794. /**
  795. * This is for ICU internal use only. Please do not use.
  796. * Check whether the 'field' is smaller than all the fields covered in
  797. * pattern, return true if it is. The sequence of calendar field,
  798. * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
  799. * @param field the calendar field need to check against
  800. * @return true if the 'field' is smaller than all the fields
  801. * covered in pattern. false otherwise.
  802. * @internal ICU 4.0
  803. */
  804. UBool isFieldUnitIgnored(UCalendarDateFields field) const;
  805. /**
  806. * This is for ICU internal use only. Please do not use.
  807. * Check whether the 'field' is smaller than all the fields covered in
  808. * pattern, return true if it is. The sequence of calendar field,
  809. * from large to small is: ERA, YEAR, MONTH, DATE, AM_PM, HOUR, MINUTE,...
  810. * @param pattern the pattern to check against
  811. * @param field the calendar field need to check against
  812. * @return true if the 'field' is smaller than all the fields
  813. * covered in pattern. false otherwise.
  814. * @internal ICU 4.0
  815. */
  816. static UBool isFieldUnitIgnored(const UnicodeString& pattern,
  817. UCalendarDateFields field);
  818. /**
  819. * This is for ICU internal use only. Please do not use.
  820. * Get the locale of this simple date formatter.
  821. * It is used in DateIntervalFormat.
  822. *
  823. * @return locale in this simple date formatter
  824. * @internal ICU 4.0
  825. */
  826. const Locale& getSmpFmtLocale() const;
  827. #endif /* U_HIDE_INTERNAL_API */
  828. private:
  829. friend class DateFormat;
  830. friend class DateIntervalFormat;
  831. void initializeDefaultCentury();
  832. void initializeBooleanAttributes();
  833. SimpleDateFormat() = delete; // default constructor not implemented
  834. /**
  835. * Used by the DateFormat factory methods to construct a SimpleDateFormat.
  836. * @param timeStyle the time style.
  837. * @param dateStyle the date style.
  838. * @param locale the given locale.
  839. * @param status Output param set to success/failure code on
  840. * exit.
  841. */
  842. SimpleDateFormat(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
  843. /**
  844. * Construct a SimpleDateFormat for the given locale. If no resource data
  845. * is available, create an object of last resort, using hard-coded strings.
  846. * This is an internal method, called by DateFormat. It should never fail.
  847. * @param locale the given locale.
  848. * @param status Output param set to success/failure code on
  849. * exit.
  850. */
  851. SimpleDateFormat(const Locale& locale, UErrorCode& status); // Use default pattern
  852. /**
  853. * Hook called by format(... FieldPosition& ...) and format(...FieldPositionIterator&...)
  854. */
  855. UnicodeString& _format(Calendar& cal, UnicodeString& appendTo, FieldPositionHandler& handler, UErrorCode& status) const;
  856. /**
  857. * Called by format() to format a single field.
  858. *
  859. * @param appendTo Output parameter to receive result.
  860. * Result is appended to existing contents.
  861. * @param ch The format character we encountered in the pattern.
  862. * @param count Number of characters in the current pattern symbol (e.g.,
  863. * "yyyy" in the pattern would result in a call to this function
  864. * with ch equal to 'y' and count equal to 4)
  865. * @param capitalizationContext Capitalization context for this date format.
  866. * @param fieldNum Zero-based numbering of current field within the overall format.
  867. * @param handler Records information about field positions.
  868. * @param cal Calendar to use
  869. * @param status Receives a status code, which will be U_ZERO_ERROR if the operation
  870. * succeeds.
  871. */
  872. void subFormat(UnicodeString &appendTo,
  873. char16_t ch,
  874. int32_t count,
  875. UDisplayContext capitalizationContext,
  876. int32_t fieldNum,
  877. char16_t fieldToOutput,
  878. FieldPositionHandler& handler,
  879. Calendar& cal,
  880. UErrorCode& status) const; // in case of illegal argument
  881. /**
  882. * Used by subFormat() to format a numeric value.
  883. * Appends to toAppendTo a string representation of "value"
  884. * having a number of digits between "minDigits" and
  885. * "maxDigits". Uses the DateFormat's NumberFormat.
  886. *
  887. * @param currentNumberFormat
  888. * @param appendTo Output parameter to receive result.
  889. * Formatted number is appended to existing contents.
  890. * @param value Value to format.
  891. * @param minDigits Minimum number of digits the result should have
  892. * @param maxDigits Maximum number of digits the result should have
  893. */
  894. void zeroPaddingNumber(const NumberFormat *currentNumberFormat,
  895. UnicodeString &appendTo,
  896. int32_t value,
  897. int32_t minDigits,
  898. int32_t maxDigits) const;
  899. /**
  900. * Return true if the given format character, occurring count
  901. * times, represents a numeric field.
  902. */
  903. static UBool isNumeric(char16_t formatChar, int32_t count);
  904. /**
  905. * Returns true if the patternOffset is at the start of a numeric field.
  906. */
  907. static UBool isAtNumericField(const UnicodeString &pattern, int32_t patternOffset);
  908. /**
  909. * Returns true if the patternOffset is right after a non-numeric field.
  910. */
  911. static UBool isAfterNonNumericField(const UnicodeString &pattern, int32_t patternOffset);
  912. /**
  913. * initializes fCalendar from parameters. Returns fCalendar as a convenience.
  914. * @param adoptZone Zone to be adopted, or nullptr for TimeZone::createDefault().
  915. * @param locale Locale of the calendar
  916. * @param status Error code
  917. * @return the newly constructed fCalendar
  918. */
  919. Calendar *initializeCalendar(TimeZone* adoptZone, const Locale& locale, UErrorCode& status);
  920. /**
  921. * Called by several of the constructors to load pattern data and formatting symbols
  922. * out of a resource bundle and initialize the locale based on it.
  923. * @param timeStyle The time style, as passed to DateFormat::createDateInstance().
  924. * @param dateStyle The date style, as passed to DateFormat::createTimeInstance().
  925. * @param locale The locale to load the patterns from.
  926. * @param status Filled in with an error code if loading the data from the
  927. * resources fails.
  928. */
  929. void construct(EStyle timeStyle, EStyle dateStyle, const Locale& locale, UErrorCode& status);
  930. /**
  931. * Called by construct() and the various constructors to set up the SimpleDateFormat's
  932. * Calendar and NumberFormat objects.
  933. * @param locale The locale for which we want a Calendar and a NumberFormat.
  934. * @param status Filled in with an error code if creating either subobject fails.
  935. */
  936. void initialize(const Locale& locale, UErrorCode& status);
  937. /**
  938. * Private code-size reduction function used by subParse.
  939. * @param text the time text being parsed.
  940. * @param start where to start parsing.
  941. * @param field the date field being parsed.
  942. * @param stringArray the string array to parsed.
  943. * @param stringArrayCount the size of the array.
  944. * @param monthPattern pointer to leap month pattern, or nullptr if none.
  945. * @param cal a Calendar set to the date and time to be formatted
  946. * into a date/time string.
  947. * @return the new start position if matching succeeded; a negative number
  948. * indicating matching failure, otherwise.
  949. */
  950. int32_t matchString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
  951. const UnicodeString* stringArray, int32_t stringArrayCount,
  952. const UnicodeString* monthPattern, Calendar& cal) const;
  953. /**
  954. * Private code-size reduction function used by subParse. Only for UCAL_MONTH
  955. * @param text the time text being parsed.
  956. * @param start where to start parsing.
  957. * @param wideStringArray the wide string array to parsed.
  958. * @param shortStringArray the short string array to parsed.
  959. * @param stringArrayCount the size of the string arrays.
  960. * @param cal a Calendar set to the date and time to be formatted
  961. * into a date/time string.
  962. * @return the new start position if matching succeeded; a negative number
  963. * indicating matching failure, otherwise.
  964. */
  965. int32_t matchAlphaMonthStrings(const UnicodeString& text, int32_t start,
  966. const UnicodeString* wideStringArray, const UnicodeString* shortStringArray,
  967. int32_t stringArrayCount, Calendar& cal) const;
  968. /**
  969. * Private code-size reduction function used by subParse.
  970. * @param text the time text being parsed.
  971. * @param start where to start parsing.
  972. * @param field the date field being parsed.
  973. * @param stringArray the string array to parsed.
  974. * @param stringArrayCount the size of the array.
  975. * @param cal a Calendar set to the date and time to be formatted
  976. * into a date/time string.
  977. * @return the new start position if matching succeeded; a negative number
  978. * indicating matching failure, otherwise.
  979. */
  980. int32_t matchQuarterString(const UnicodeString& text, int32_t start, UCalendarDateFields field,
  981. const UnicodeString* stringArray, int32_t stringArrayCount, Calendar& cal) const;
  982. /**
  983. * Used by subParse() to match localized day period strings.
  984. */
  985. int32_t matchDayPeriodStrings(const UnicodeString& text, int32_t start,
  986. const UnicodeString* stringArray, int32_t stringArrayCount,
  987. int32_t &dayPeriod) const;
  988. /**
  989. * Private function used by subParse to match literal pattern text.
  990. *
  991. * @param pattern the pattern string
  992. * @param patternOffset the starting offset into the pattern text. On
  993. * output will be set the offset of the first non-literal character in the pattern
  994. * @param text the text being parsed
  995. * @param textOffset the starting offset into the text. On output
  996. * will be set to the offset of the character after the match
  997. * @param whitespaceLenient <code>true</code> if whitespace parse is lenient, <code>false</code> otherwise.
  998. * @param partialMatchLenient <code>true</code> if partial match parse is lenient, <code>false</code> otherwise.
  999. * @param oldLeniency <code>true</code> if old leniency control is lenient, <code>false</code> otherwise.
  1000. *
  1001. * @return <code>true</code> if the literal text could be matched, <code>false</code> otherwise.
  1002. */
  1003. static UBool matchLiterals(const UnicodeString &pattern, int32_t &patternOffset,
  1004. const UnicodeString &text, int32_t &textOffset,
  1005. UBool whitespaceLenient, UBool partialMatchLenient, UBool oldLeniency);
  1006. /**
  1007. * Private member function that converts the parsed date strings into
  1008. * timeFields. Returns -start (for ParsePosition) if failed.
  1009. * @param text the time text to be parsed.
  1010. * @param start where to start parsing.
  1011. * @param ch the pattern character for the date field text to be parsed.
  1012. * @param count the count of a pattern character.
  1013. * @param obeyCount if true then the count is strictly obeyed.
  1014. * @param allowNegative
  1015. * @param ambiguousYear If true then the two-digit year == the default start year.
  1016. * @param saveHebrewMonth Used to hang onto month until year is known.
  1017. * @param cal a Calendar set to the date and time to be formatted
  1018. * into a date/time string.
  1019. * @param patLoc
  1020. * @param numericLeapMonthFormatter If non-null, used to parse numeric leap months.
  1021. * @param tzTimeType the type of parsed time zone - standard, daylight or unknown (output).
  1022. * This parameter can be nullptr if caller does not need the information.
  1023. * @return the new start position if matching succeeded; a negative number
  1024. * indicating matching failure, otherwise.
  1025. */
  1026. int32_t subParse(const UnicodeString& text, int32_t& start, char16_t ch, int32_t count,
  1027. UBool obeyCount, UBool allowNegative, UBool ambiguousYear[], int32_t& saveHebrewMonth, Calendar& cal,
  1028. int32_t patLoc, MessageFormat * numericLeapMonthFormatter, UTimeZoneFormatTimeType *tzTimeType,
  1029. int32_t *dayPeriod=nullptr) const;
  1030. void parseInt(const UnicodeString& text,
  1031. Formattable& number,
  1032. ParsePosition& pos,
  1033. UBool allowNegative,
  1034. const NumberFormat *fmt) const;
  1035. void parseInt(const UnicodeString& text,
  1036. Formattable& number,
  1037. int32_t maxDigits,
  1038. ParsePosition& pos,
  1039. UBool allowNegative,
  1040. const NumberFormat *fmt) const;
  1041. int32_t checkIntSuffix(const UnicodeString& text, int32_t start,
  1042. int32_t patLoc, UBool isNegative) const;
  1043. /**
  1044. * Counts number of digit code points in the specified text.
  1045. *
  1046. * @param text input text
  1047. * @param start start index, inclusive
  1048. * @param end end index, exclusive
  1049. * @return number of digits found in the text in the specified range.
  1050. */
  1051. int32_t countDigits(const UnicodeString& text, int32_t start, int32_t end) const;
  1052. /**
  1053. * Translate a pattern, mapping each character in the from string to the
  1054. * corresponding character in the to string. Return an error if the original
  1055. * pattern contains an unmapped character, or if a quote is unmatched.
  1056. * Quoted (single quotes only) material is not translated.
  1057. * @param originalPattern the original pattern.
  1058. * @param translatedPattern Output param to receive the translited pattern.
  1059. * @param from the characters to be translited from.
  1060. * @param to the characters to be translited to.
  1061. * @param status Receives a status code, which will be U_ZERO_ERROR
  1062. * if the operation succeeds.
  1063. */
  1064. static void translatePattern(const UnicodeString& originalPattern,
  1065. UnicodeString& translatedPattern,
  1066. const UnicodeString& from,
  1067. const UnicodeString& to,
  1068. UErrorCode& status);
  1069. /**
  1070. * Sets the starting date of the 100-year window that dates with 2-digit years
  1071. * are considered to fall within.
  1072. * @param startDate the start date
  1073. * @param status Receives a status code, which will be U_ZERO_ERROR
  1074. * if the operation succeeds.
  1075. */
  1076. void parseAmbiguousDatesAsAfter(UDate startDate, UErrorCode& status);
  1077. /**
  1078. * Return the length matched by the given affix, or -1 if none.
  1079. * Runs of white space in the affix, match runs of white space in
  1080. * the input.
  1081. * @param affix pattern string, taken as a literal
  1082. * @param input input text
  1083. * @param pos offset into input at which to begin matching
  1084. * @return length of input that matches, or -1 if match failure
  1085. */
  1086. int32_t compareSimpleAffix(const UnicodeString& affix,
  1087. const UnicodeString& input,
  1088. int32_t pos) const;
  1089. /**
  1090. * Skip over a run of zero or more Pattern_White_Space characters at
  1091. * pos in text.
  1092. */
  1093. int32_t skipPatternWhiteSpace(const UnicodeString& text, int32_t pos) const;
  1094. /**
  1095. * Skip over a run of zero or more isUWhiteSpace() characters at pos
  1096. * in text.
  1097. */
  1098. int32_t skipUWhiteSpace(const UnicodeString& text, int32_t pos) const;
  1099. /**
  1100. * Initialize SimpleNumberFormat instance
  1101. */
  1102. void initSimpleNumberFormatter(UErrorCode &status);
  1103. /**
  1104. * Initialize NumberFormat instances used for numbering system overrides.
  1105. */
  1106. void initNumberFormatters(const Locale &locale,UErrorCode &status);
  1107. /**
  1108. * Parse the given override string and set up structures for number formats
  1109. */
  1110. void processOverrideString(const Locale &locale, const UnicodeString &str, int8_t type, UErrorCode &status);
  1111. /**
  1112. * Used to map pattern characters to Calendar field identifiers.
  1113. */
  1114. static const UCalendarDateFields fgPatternIndexToCalendarField[];
  1115. /**
  1116. * Map index into pattern character string to DateFormat field number
  1117. */
  1118. static const UDateFormatField fgPatternIndexToDateFormatField[];
  1119. /**
  1120. * Lazy TimeZoneFormat instantiation, semantically const
  1121. */
  1122. TimeZoneFormat *tzFormat(UErrorCode &status) const;
  1123. const NumberFormat* getNumberFormatByIndex(UDateFormatField index) const;
  1124. /**
  1125. * Used to map Calendar field to field level.
  1126. * The larger the level, the smaller the field unit.
  1127. * For example, UCAL_ERA level is 0, UCAL_YEAR level is 10,
  1128. * UCAL_MONTH level is 20.
  1129. */
  1130. static const int32_t fgCalendarFieldToLevel[];
  1131. /**
  1132. * Map calendar field letter into calendar field level.
  1133. */
  1134. static int32_t getLevelFromChar(char16_t ch);
  1135. /**
  1136. * Tell if a character can be used to define a field in a format string.
  1137. */
  1138. static UBool isSyntaxChar(char16_t ch);
  1139. /**
  1140. * The formatting pattern for this formatter.
  1141. */
  1142. UnicodeString fPattern;
  1143. /**
  1144. * The numbering system override for dates.
  1145. */
  1146. UnicodeString fDateOverride;
  1147. /**
  1148. * The numbering system override for times.
  1149. */
  1150. UnicodeString fTimeOverride;
  1151. /**
  1152. * The original locale used (for reloading symbols)
  1153. */
  1154. Locale fLocale;
  1155. /**
  1156. * A pointer to an object containing the strings to use in formatting (e.g.,
  1157. * month and day names, AM and PM strings, time zone names, etc.)
  1158. */
  1159. DateFormatSymbols* fSymbols = nullptr; // Owned
  1160. /**
  1161. * The time zone formatter
  1162. */
  1163. TimeZoneFormat* fTimeZoneFormat = nullptr;
  1164. /**
  1165. * If dates have ambiguous years, we map them into the century starting
  1166. * at defaultCenturyStart, which may be any date. If defaultCenturyStart is
  1167. * set to SYSTEM_DEFAULT_CENTURY, which it is by default, then the system
  1168. * values are used. The instance values defaultCenturyStart and
  1169. * defaultCenturyStartYear are only used if explicitly set by the user
  1170. * through the API method parseAmbiguousDatesAsAfter().
  1171. */
  1172. UDate fDefaultCenturyStart;
  1173. UBool fHasMinute;
  1174. UBool fHasSecond;
  1175. UBool fHasHanYearChar; // pattern contains the Han year character \u5E74
  1176. /**
  1177. * Sets fHasMinutes and fHasSeconds.
  1178. */
  1179. void parsePattern();
  1180. /**
  1181. * See documentation for defaultCenturyStart.
  1182. */
  1183. /*transient*/ int32_t fDefaultCenturyStartYear;
  1184. struct NSOverride : public UMemory {
  1185. const SharedNumberFormat *snf;
  1186. int32_t hash;
  1187. NSOverride *next;
  1188. void free();
  1189. NSOverride() : snf(nullptr), hash(0), next(nullptr) {
  1190. }
  1191. ~NSOverride();
  1192. };
  1193. /**
  1194. * The number format in use for each date field. nullptr means fall back
  1195. * to fNumberFormat in DateFormat.
  1196. */
  1197. const SharedNumberFormat **fSharedNumberFormatters = nullptr;
  1198. /**
  1199. * Number formatter pre-allocated for fast performance
  1200. *
  1201. * This references the decimal symbols from fNumberFormatter if it is an instance
  1202. * of DecimalFormat (and is otherwise null). This should always be cleaned up before
  1203. * destroying fNumberFormatter.
  1204. */
  1205. const number::SimpleNumberFormatter* fSimpleNumberFormatter = nullptr;
  1206. UBool fHaveDefaultCentury;
  1207. const BreakIterator* fCapitalizationBrkIter = nullptr;
  1208. };
  1209. inline UDate
  1210. SimpleDateFormat::get2DigitYearStart(UErrorCode& /*status*/) const
  1211. {
  1212. return fDefaultCenturyStart;
  1213. }
  1214. U_NAMESPACE_END
  1215. #endif /* #if !UCONFIG_NO_FORMATTING */
  1216. #endif /* U_SHOW_CPLUSPLUS_API */
  1217. #endif // _SMPDTFMT
  1218. //eof