format_date_parser.hpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  1. #ifndef DATE_TIME_FORMAT_DATE_PARSER_HPP__
  2. #define DATE_TIME_FORMAT_DATE_PARSER_HPP__
  3. /* Copyright (c) 2004-2005 CrystalClear Software, Inc.
  4. * Use, modification and distribution is subject to the
  5. * Boost Software License, Version 1.0. (See accompanying
  6. * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
  7. * Author: Jeff Garland, Bart Garst
  8. * $Date$
  9. */
  10. #include "boost/lexical_cast.hpp"
  11. #include "boost/date_time/string_parse_tree.hpp"
  12. #include "boost/date_time/strings_from_facet.hpp"
  13. #include "boost/date_time/special_values_parser.hpp"
  14. #include <string>
  15. #include <vector>
  16. #include <sstream>
  17. #include <iterator>
  18. #ifndef BOOST_NO_STDC_NAMESPACE
  19. # include <cctype>
  20. #else
  21. # include <ctype.h>
  22. #endif
  23. #ifdef BOOST_NO_STDC_NAMESPACE
  24. namespace std {
  25. using ::isspace;
  26. using ::isdigit;
  27. }
  28. #endif
  29. namespace boost { namespace date_time {
  30. //! Helper function for parsing fixed length strings into integers
  31. /*! Will consume 'length' number of characters from stream. Consumed
  32. * character are transfered to parse_match_result struct.
  33. * Returns '-1' if no number can be parsed or incorrect number of
  34. * digits in stream. */
  35. template<typename int_type, typename charT>
  36. inline
  37. int_type
  38. fixed_string_to_int(std::istreambuf_iterator<charT>& itr,
  39. std::istreambuf_iterator<charT>& stream_end,
  40. parse_match_result<charT>& mr,
  41. unsigned int length,
  42. const charT& fill_char)
  43. {
  44. //typedef std::basic_string<charT> string_type;
  45. unsigned int j = 0;
  46. //string_type s;
  47. while (j < length && itr != stream_end &&
  48. (std::isdigit(*itr) || *itr == fill_char)) {
  49. if(*itr == fill_char) {
  50. /* Since a fill_char can be anything, we convert it to a zero.
  51. * lexical_cast will behave predictably when zero is used as fill. */
  52. mr.cache += ('0');
  53. }
  54. else {
  55. mr.cache += (*itr);
  56. }
  57. itr++;
  58. j++;
  59. }
  60. int_type i = static_cast<int_type>(-1);
  61. // mr.cache will hold leading zeros. size() tells us when input is too short.
  62. if(mr.cache.size() < length) {
  63. return i;
  64. }
  65. try {
  66. i = boost::lexical_cast<int_type>(mr.cache);
  67. }catch(bad_lexical_cast&){
  68. // we want to return -1 if the cast fails so nothing to do here
  69. }
  70. return i;
  71. }
  72. //! Helper function for parsing fixed length strings into integers
  73. /*! Will consume 'length' number of characters from stream. Consumed
  74. * character are transfered to parse_match_result struct.
  75. * Returns '-1' if no number can be parsed or incorrect number of
  76. * digits in stream. */
  77. template<typename int_type, typename charT>
  78. inline
  79. int_type
  80. fixed_string_to_int(std::istreambuf_iterator<charT>& itr,
  81. std::istreambuf_iterator<charT>& stream_end,
  82. parse_match_result<charT>& mr,
  83. unsigned int length)
  84. {
  85. return fixed_string_to_int<int_type, charT>(itr, stream_end, mr, length, '0');
  86. }
  87. //! Helper function for parsing varied length strings into integers
  88. /*! Will consume 'max_length' characters from stream only if those
  89. * characters are digits. Returns '-1' if no number can be parsed.
  90. * Will not parse a number preceeded by a '+' or '-'. */
  91. template<typename int_type, typename charT>
  92. inline
  93. int_type
  94. var_string_to_int(std::istreambuf_iterator<charT>& itr,
  95. const std::istreambuf_iterator<charT>& stream_end,
  96. unsigned int max_length)
  97. {
  98. typedef std::basic_string<charT> string_type;
  99. unsigned int j = 0;
  100. string_type s;
  101. while (itr != stream_end && (j < max_length) && std::isdigit(*itr)) {
  102. s += (*itr);
  103. ++itr;
  104. ++j;
  105. }
  106. int_type i = static_cast<int_type>(-1);
  107. if(!s.empty()) {
  108. i = boost::lexical_cast<int_type>(s);
  109. }
  110. return i;
  111. }
  112. //! Class with generic date parsing using a format string
  113. /*! The following is the set of recognized format specifiers
  114. - %a - Short weekday name
  115. - %A - Long weekday name
  116. - %b - Abbreviated month name
  117. - %B - Full month name
  118. - %d - Day of the month as decimal 01 to 31
  119. - %j - Day of year as decimal from 001 to 366
  120. - %m - Month name as a decimal 01 to 12
  121. - %U - Week number 00 to 53 with first Sunday as the first day of week 1?
  122. - %w - Weekday as decimal number 0 to 6 where Sunday == 0
  123. - %W - Week number 00 to 53 where Monday is first day of week 1
  124. - %x - facet default date representation
  125. - %y - Year without the century - eg: 04 for 2004
  126. - %Y - Year with century
  127. The weekday specifiers (%a and %A) do not add to the date construction,
  128. but they provide a way to skip over the weekday names for formats that
  129. provide them.
  130. todo -- Another interesting feature that this approach could provide is
  131. an option to fill in any missing fields with the current values
  132. from the clock. So if you have %m-%d the parser would detect
  133. the missing year value and fill it in using the clock.
  134. todo -- What to do with the %x. %x in the classic facet is just bad...
  135. */
  136. template<class date_type, typename charT>
  137. class format_date_parser
  138. {
  139. public:
  140. typedef std::basic_string<charT> string_type;
  141. typedef std::basic_istringstream<charT> stringstream_type;
  142. typedef std::istreambuf_iterator<charT> stream_itr_type;
  143. typedef typename string_type::const_iterator const_itr;
  144. typedef typename date_type::year_type year_type;
  145. typedef typename date_type::month_type month_type;
  146. typedef typename date_type::day_type day_type;
  147. typedef typename date_type::duration_type duration_type;
  148. typedef typename date_type::day_of_week_type day_of_week_type;
  149. typedef typename date_type::day_of_year_type day_of_year_type;
  150. typedef string_parse_tree<charT> parse_tree_type;
  151. typedef typename parse_tree_type::parse_match_result_type match_results;
  152. typedef std::vector<std::basic_string<charT> > input_collection_type;
  153. // TODO sv_parser uses its default constructor - write the others
  154. format_date_parser(const string_type& format_str,
  155. const input_collection_type& month_short_names,
  156. const input_collection_type& month_long_names,
  157. const input_collection_type& weekday_short_names,
  158. const input_collection_type& weekday_long_names) :
  159. m_format(format_str),
  160. m_month_short_names(month_short_names, 1),
  161. m_month_long_names(month_long_names, 1),
  162. m_weekday_short_names(weekday_short_names),
  163. m_weekday_long_names(weekday_long_names)
  164. {}
  165. format_date_parser(const string_type& format_str,
  166. const std::locale& locale) :
  167. m_format(format_str),
  168. m_month_short_names(gather_month_strings<charT>(locale), 1),
  169. m_month_long_names(gather_month_strings<charT>(locale, false), 1),
  170. m_weekday_short_names(gather_weekday_strings<charT>(locale)),
  171. m_weekday_long_names(gather_weekday_strings<charT>(locale, false))
  172. {}
  173. format_date_parser(const format_date_parser<date_type,charT>& fdp)
  174. {
  175. this->m_format = fdp.m_format;
  176. this->m_month_short_names = fdp.m_month_short_names;
  177. this->m_month_long_names = fdp.m_month_long_names;
  178. this->m_weekday_short_names = fdp.m_weekday_short_names;
  179. this->m_weekday_long_names = fdp.m_weekday_long_names;
  180. }
  181. string_type format() const
  182. {
  183. return m_format;
  184. }
  185. void format(string_type format_str)
  186. {
  187. m_format = format_str;
  188. }
  189. void short_month_names(const input_collection_type& month_names)
  190. {
  191. m_month_short_names = parse_tree_type(month_names, 1);
  192. }
  193. void long_month_names(const input_collection_type& month_names)
  194. {
  195. m_month_long_names = parse_tree_type(month_names, 1);
  196. }
  197. void short_weekday_names(const input_collection_type& weekday_names)
  198. {
  199. m_weekday_short_names = parse_tree_type(weekday_names);
  200. }
  201. void long_weekday_names(const input_collection_type& weekday_names)
  202. {
  203. m_weekday_long_names = parse_tree_type(weekday_names);
  204. }
  205. date_type
  206. parse_date(const string_type& value,
  207. const string_type& format_str,
  208. const special_values_parser<date_type,charT>& sv_parser) const
  209. {
  210. stringstream_type ss(value);
  211. stream_itr_type sitr(ss);
  212. stream_itr_type stream_end;
  213. return parse_date(sitr, stream_end, format_str, sv_parser);
  214. }
  215. date_type
  216. parse_date(std::istreambuf_iterator<charT>& sitr,
  217. std::istreambuf_iterator<charT>& stream_end,
  218. const special_values_parser<date_type,charT>& sv_parser) const
  219. {
  220. return parse_date(sitr, stream_end, m_format, sv_parser);
  221. }
  222. /*! Of all the objects that the format_date_parser can parse, only a
  223. * date can be a special value. Therefore, only parse_date checks
  224. * for special_values. */
  225. date_type
  226. parse_date(std::istreambuf_iterator<charT>& sitr,
  227. std::istreambuf_iterator<charT>& stream_end,
  228. string_type format_str,
  229. const special_values_parser<date_type,charT>& sv_parser) const
  230. {
  231. bool use_current_char = false;
  232. // skip leading whitespace
  233. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  234. short year(0), month(0), day(0), day_of_year(0);// wkday(0);
  235. /* Initialized the following to their minimum values. These intermediate
  236. * objects are used so we get specific exceptions when part of the input
  237. * is unparsable.
  238. * Ex: "205-Jan-15" will throw a bad_year, "2005-Jsn-15"- bad_month, etc.*/
  239. year_type t_year(1400);
  240. month_type t_month(1);
  241. day_type t_day(1);
  242. day_of_week_type wkday(0);
  243. const_itr itr(format_str.begin());
  244. while (itr != format_str.end() && (sitr != stream_end)) {
  245. if (*itr == '%') {
  246. if ( ++itr == format_str.end())
  247. break;
  248. if (*itr != '%') {
  249. switch(*itr) {
  250. case 'a':
  251. {
  252. //this value is just throw away. It could be used for
  253. //error checking potentially, but it isn't helpful in
  254. //actually constructing the date - we just need to get it
  255. //out of the stream
  256. match_results mr = m_weekday_short_names.match(sitr, stream_end);
  257. if(mr.current_match == match_results::PARSE_ERROR) {
  258. // check special_values
  259. if(sv_parser.match(sitr, stream_end, mr)) {
  260. return date_type(static_cast<special_values>(mr.current_match));
  261. }
  262. }
  263. wkday = mr.current_match;
  264. if (mr.has_remaining()) {
  265. use_current_char = true;
  266. }
  267. break;
  268. }
  269. case 'A':
  270. {
  271. //this value is just throw away. It could be used for
  272. //error checking potentially, but it isn't helpful in
  273. //actually constructing the date - we just need to get it
  274. //out of the stream
  275. match_results mr = m_weekday_long_names.match(sitr, stream_end);
  276. if(mr.current_match == match_results::PARSE_ERROR) {
  277. // check special_values
  278. if(sv_parser.match(sitr, stream_end, mr)) {
  279. return date_type(static_cast<special_values>(mr.current_match));
  280. }
  281. }
  282. wkday = mr.current_match;
  283. if (mr.has_remaining()) {
  284. use_current_char = true;
  285. }
  286. break;
  287. }
  288. case 'b':
  289. {
  290. match_results mr = m_month_short_names.match(sitr, stream_end);
  291. if(mr.current_match == match_results::PARSE_ERROR) {
  292. // check special_values
  293. if(sv_parser.match(sitr, stream_end, mr)) {
  294. return date_type(static_cast<special_values>(mr.current_match));
  295. }
  296. }
  297. t_month = month_type(mr.current_match);
  298. if (mr.has_remaining()) {
  299. use_current_char = true;
  300. }
  301. break;
  302. }
  303. case 'B':
  304. {
  305. match_results mr = m_month_long_names.match(sitr, stream_end);
  306. if(mr.current_match == match_results::PARSE_ERROR) {
  307. // check special_values
  308. if(sv_parser.match(sitr, stream_end, mr)) {
  309. return date_type(static_cast<special_values>(mr.current_match));
  310. }
  311. }
  312. t_month = month_type(mr.current_match);
  313. if (mr.has_remaining()) {
  314. use_current_char = true;
  315. }
  316. break;
  317. }
  318. case 'd':
  319. {
  320. match_results mr;
  321. day = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2);
  322. if(day == -1) {
  323. if(sv_parser.match(sitr, stream_end, mr)) {
  324. return date_type(static_cast<special_values>(mr.current_match));
  325. }
  326. }
  327. t_day = day_type(day);
  328. break;
  329. }
  330. case 'e':
  331. {
  332. match_results mr;
  333. day = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2, ' ');
  334. if(day == -1) {
  335. if(sv_parser.match(sitr, stream_end, mr)) {
  336. return date_type(static_cast<special_values>(mr.current_match));
  337. }
  338. }
  339. t_day = day_type(day);
  340. break;
  341. }
  342. case 'j':
  343. {
  344. match_results mr;
  345. day_of_year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 3);
  346. if(day_of_year == -1) {
  347. if(sv_parser.match(sitr, stream_end, mr)) {
  348. return date_type(static_cast<special_values>(mr.current_match));
  349. }
  350. }
  351. // these next two lines are so we get an exception with bad input
  352. day_of_year_type t_day_of_year(1);
  353. t_day_of_year = day_of_year_type(day_of_year);
  354. break;
  355. }
  356. case 'm':
  357. {
  358. match_results mr;
  359. month = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2);
  360. if(month == -1) {
  361. if(sv_parser.match(sitr, stream_end, mr)) {
  362. return date_type(static_cast<special_values>(mr.current_match));
  363. }
  364. }
  365. t_month = month_type(month);
  366. break;
  367. }
  368. case 'Y':
  369. {
  370. match_results mr;
  371. year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 4);
  372. if(year == -1) {
  373. if(sv_parser.match(sitr, stream_end, mr)) {
  374. return date_type(static_cast<special_values>(mr.current_match));
  375. }
  376. }
  377. t_year = year_type(year);
  378. break;
  379. }
  380. case 'y':
  381. {
  382. match_results mr;
  383. year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2);
  384. if(year == -1) {
  385. if(sv_parser.match(sitr, stream_end, mr)) {
  386. return date_type(static_cast<special_values>(mr.current_match));
  387. }
  388. }
  389. year += 2000; //make 2 digit years in this century
  390. t_year = year_type(year);
  391. break;
  392. }
  393. default:
  394. {} //ignore those we don't understand
  395. }//switch
  396. }
  397. else { // itr == '%', second consecutive
  398. sitr++;
  399. }
  400. itr++; //advance past format specifier
  401. }
  402. else { //skip past chars in format and in buffer
  403. itr++;
  404. if (use_current_char) {
  405. use_current_char = false;
  406. }
  407. else {
  408. sitr++;
  409. }
  410. }
  411. }
  412. if (day_of_year > 0) {
  413. date_type d(static_cast<unsigned short>(year-1),12,31); //end of prior year
  414. return d + duration_type(day_of_year);
  415. }
  416. return date_type(t_year, t_month, t_day); // exceptions were thrown earlier
  417. // if input was no good
  418. }
  419. //! Throws bad_month if unable to parse
  420. month_type
  421. parse_month(std::istreambuf_iterator<charT>& sitr,
  422. std::istreambuf_iterator<charT>& stream_end,
  423. string_type format_str) const
  424. {
  425. match_results mr;
  426. return parse_month(sitr, stream_end, format_str, mr);
  427. }
  428. //! Throws bad_month if unable to parse
  429. month_type
  430. parse_month(std::istreambuf_iterator<charT>& sitr,
  431. std::istreambuf_iterator<charT>& stream_end,
  432. string_type format_str,
  433. match_results& mr) const
  434. {
  435. bool use_current_char = false;
  436. // skip leading whitespace
  437. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  438. short month(0);
  439. const_itr itr(format_str.begin());
  440. while (itr != format_str.end() && (sitr != stream_end)) {
  441. if (*itr == '%') {
  442. if ( ++itr == format_str.end())
  443. break;
  444. if (*itr != '%') {
  445. switch(*itr) {
  446. case 'b':
  447. {
  448. mr = m_month_short_names.match(sitr, stream_end);
  449. month = mr.current_match;
  450. if (mr.has_remaining()) {
  451. use_current_char = true;
  452. }
  453. break;
  454. }
  455. case 'B':
  456. {
  457. mr = m_month_long_names.match(sitr, stream_end);
  458. month = mr.current_match;
  459. if (mr.has_remaining()) {
  460. use_current_char = true;
  461. }
  462. break;
  463. }
  464. case 'm':
  465. {
  466. month = var_string_to_int<short, charT>(sitr, stream_end, 2);
  467. // var_string_to_int returns -1 if parse failed. That will
  468. // cause a bad_month exception to be thrown so we do nothing here
  469. break;
  470. }
  471. default:
  472. {} //ignore those we don't understand
  473. }//switch
  474. }
  475. else { // itr == '%', second consecutive
  476. sitr++;
  477. }
  478. itr++; //advance past format specifier
  479. }
  480. else { //skip past chars in format and in buffer
  481. itr++;
  482. if (use_current_char) {
  483. use_current_char = false;
  484. }
  485. else {
  486. sitr++;
  487. }
  488. }
  489. }
  490. return month_type(month); // throws bad_month exception when values are zero
  491. }
  492. //! Expects 1 or 2 digits 1-31. Throws bad_day_of_month if unable to parse
  493. day_type
  494. parse_var_day_of_month(std::istreambuf_iterator<charT>& sitr,
  495. std::istreambuf_iterator<charT>& stream_end) const
  496. {
  497. // skip leading whitespace
  498. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  499. return day_type(var_string_to_int<short, charT>(sitr, stream_end, 2));
  500. }
  501. //! Expects 2 digits 01-31. Throws bad_day_of_month if unable to parse
  502. day_type
  503. parse_day_of_month(std::istreambuf_iterator<charT>& sitr,
  504. std::istreambuf_iterator<charT>& stream_end) const
  505. {
  506. // skip leading whitespace
  507. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  508. //return day_type(var_string_to_int<short, charT>(sitr, stream_end, 2));
  509. match_results mr;
  510. return day_type(fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2));
  511. }
  512. day_of_week_type
  513. parse_weekday(std::istreambuf_iterator<charT>& sitr,
  514. std::istreambuf_iterator<charT>& stream_end,
  515. string_type format_str) const
  516. {
  517. match_results mr;
  518. return parse_weekday(sitr, stream_end, format_str, mr);
  519. }
  520. day_of_week_type
  521. parse_weekday(std::istreambuf_iterator<charT>& sitr,
  522. std::istreambuf_iterator<charT>& stream_end,
  523. string_type format_str,
  524. match_results& mr) const
  525. {
  526. bool use_current_char = false;
  527. // skip leading whitespace
  528. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  529. short wkday(0);
  530. const_itr itr(format_str.begin());
  531. while (itr != format_str.end() && (sitr != stream_end)) {
  532. if (*itr == '%') {
  533. if ( ++itr == format_str.end())
  534. break;
  535. if (*itr != '%') {
  536. switch(*itr) {
  537. case 'a':
  538. {
  539. //this value is just throw away. It could be used for
  540. //error checking potentially, but it isn't helpful in
  541. //actually constructing the date - we just need to get it
  542. //out of the stream
  543. mr = m_weekday_short_names.match(sitr, stream_end);
  544. wkday = mr.current_match;
  545. if (mr.has_remaining()) {
  546. use_current_char = true;
  547. }
  548. break;
  549. }
  550. case 'A':
  551. {
  552. //this value is just throw away. It could be used for
  553. //error checking potentially, but it isn't helpful in
  554. //actually constructing the date - we just need to get it
  555. //out of the stream
  556. mr = m_weekday_long_names.match(sitr, stream_end);
  557. wkday = mr.current_match;
  558. if (mr.has_remaining()) {
  559. use_current_char = true;
  560. }
  561. break;
  562. }
  563. case 'w':
  564. {
  565. // weekday as number 0-6, Sunday == 0
  566. wkday = var_string_to_int<short, charT>(sitr, stream_end, 2);
  567. break;
  568. }
  569. default:
  570. {} //ignore those we don't understand
  571. }//switch
  572. }
  573. else { // itr == '%', second consecutive
  574. sitr++;
  575. }
  576. itr++; //advance past format specifier
  577. }
  578. else { //skip past chars in format and in buffer
  579. itr++;
  580. if (use_current_char) {
  581. use_current_char = false;
  582. }
  583. else {
  584. sitr++;
  585. }
  586. }
  587. }
  588. return day_of_week_type(wkday); // throws bad_day_of_month exception
  589. // when values are zero
  590. }
  591. //! throws bad_year if unable to parse
  592. year_type
  593. parse_year(std::istreambuf_iterator<charT>& sitr,
  594. std::istreambuf_iterator<charT>& stream_end,
  595. string_type format_str) const
  596. {
  597. match_results mr;
  598. return parse_year(sitr, stream_end, format_str, mr);
  599. }
  600. //! throws bad_year if unable to parse
  601. year_type
  602. parse_year(std::istreambuf_iterator<charT>& sitr,
  603. std::istreambuf_iterator<charT>& stream_end,
  604. string_type format_str,
  605. match_results& mr) const
  606. {
  607. // skip leading whitespace
  608. while(std::isspace(*sitr) && sitr != stream_end) { ++sitr; }
  609. unsigned short year(0);
  610. const_itr itr(format_str.begin());
  611. while (itr != format_str.end() && (sitr != stream_end)) {
  612. if (*itr == '%') {
  613. if ( ++itr == format_str.end())
  614. break;
  615. if (*itr != '%') {
  616. //match_results mr;
  617. switch(*itr) {
  618. case 'Y':
  619. {
  620. // year from 4 digit string
  621. year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 4);
  622. break;
  623. }
  624. case 'y':
  625. {
  626. // year from 2 digit string (no century)
  627. year = fixed_string_to_int<short, charT>(sitr, stream_end, mr, 2);
  628. year += 2000; //make 2 digit years in this century
  629. break;
  630. }
  631. default:
  632. {} //ignore those we don't understand
  633. }//switch
  634. }
  635. else { // itr == '%', second consecutive
  636. sitr++;
  637. }
  638. itr++; //advance past format specifier
  639. }
  640. else { //skip past chars in format and in buffer
  641. itr++;
  642. sitr++;
  643. }
  644. }
  645. return year_type(year); // throws bad_year exception when values are zero
  646. }
  647. private:
  648. string_type m_format;
  649. parse_tree_type m_month_short_names;
  650. parse_tree_type m_month_long_names;
  651. parse_tree_type m_weekday_short_names;
  652. parse_tree_type m_weekday_long_names;
  653. };
  654. } } //namespace
  655. #endif