date_facet.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. #ifndef _DATE_TIME_DATE_FACET__HPP___
  2. #define _DATE_TIME_DATE_FACET__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: Martin Andrian, Jeff Garland, Bart Garst
  8. * $Date: 2009-06-04 07:40:18 -0400 (Thu, 04 Jun 2009) $
  9. */
  10. #include <locale>
  11. #include <string>
  12. #include <vector>
  13. #include <iterator> // ostreambuf_iterator
  14. #include <boost/throw_exception.hpp>
  15. #include <boost/algorithm/string/replace.hpp>
  16. #include <boost/date_time/compiler_config.hpp>
  17. #include <boost/date_time/period.hpp>
  18. #include <boost/date_time/special_defs.hpp>
  19. #include <boost/date_time/special_values_formatter.hpp>
  20. #include <boost/date_time/period_formatter.hpp>
  21. #include <boost/date_time/period_parser.hpp>
  22. #include <boost/date_time/date_generator_formatter.hpp>
  23. #include <boost/date_time/date_generator_parser.hpp>
  24. #include <boost/date_time/format_date_parser.hpp>
  25. namespace boost { namespace date_time {
  26. /*! Class that provides format based I/O facet for date types.
  27. *
  28. * This class allows the formatting of dates by using format string.
  29. * Format strings are:
  30. *
  31. * - %A => long_weekday_format - Full name Ex: Tuesday
  32. * - %a => short_weekday_format - Three letter abbreviation Ex: Tue
  33. * - %B => long_month_format - Full name Ex: October
  34. * - %b => short_month_format - Three letter abbreviation Ex: Oct
  35. * - %x => standard_format_specifier - defined by the locale
  36. * - %Y-%b-%d => default_date_format - YYYY-Mon-dd
  37. *
  38. * Default month format == %b
  39. * Default weekday format == %a
  40. */
  41. template <class date_type,
  42. class CharT,
  43. class OutItrT = std::ostreambuf_iterator<CharT, std::char_traits<CharT> > >
  44. class date_facet : public std::locale::facet {
  45. public:
  46. typedef typename date_type::duration_type duration_type;
  47. // greg_weekday is gregorian_calendar::day_of_week_type
  48. typedef typename date_type::day_of_week_type day_of_week_type;
  49. typedef typename date_type::day_type day_type;
  50. typedef typename date_type::month_type month_type;
  51. typedef boost::date_time::period<date_type,duration_type> period_type;
  52. typedef std::basic_string<CharT> string_type;
  53. typedef CharT char_type;
  54. typedef boost::date_time::period_formatter<CharT> period_formatter_type;
  55. typedef boost::date_time::special_values_formatter<CharT> special_values_formatter_type;
  56. typedef std::vector<std::basic_string<CharT> > input_collection_type;
  57. // used for the output of the date_generators
  58. typedef date_generator_formatter<date_type, CharT> date_gen_formatter_type;
  59. typedef partial_date<date_type> partial_date_type;
  60. typedef nth_kday_of_month<date_type> nth_kday_type;
  61. typedef first_kday_of_month<date_type> first_kday_type;
  62. typedef last_kday_of_month<date_type> last_kday_type;
  63. typedef first_kday_after<date_type> kday_after_type;
  64. typedef first_kday_before<date_type> kday_before_type;
  65. static const char_type long_weekday_format[3];
  66. static const char_type short_weekday_format[3];
  67. static const char_type long_month_format[3];
  68. static const char_type short_month_format[3];
  69. static const char_type default_period_separator[4];
  70. static const char_type standard_format_specifier[3];
  71. static const char_type iso_format_specifier[7];
  72. static const char_type iso_format_extended_specifier[9];
  73. static const char_type default_date_format[9]; // YYYY-Mon-DD
  74. static std::locale::id id;
  75. #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
  76. std::locale::id& __get_id (void) const { return id; }
  77. #endif
  78. explicit date_facet(::size_t a_ref = 0)
  79. : std::locale::facet(a_ref),
  80. //m_format(standard_format_specifier)
  81. m_format(default_date_format),
  82. m_month_format(short_month_format),
  83. m_weekday_format(short_weekday_format)
  84. {}
  85. explicit date_facet(const char_type* format_str,
  86. const input_collection_type& short_names,
  87. ::size_t ref_count = 0)
  88. : std::locale::facet(ref_count),
  89. m_format(format_str),
  90. m_month_format(short_month_format),
  91. m_weekday_format(short_weekday_format),
  92. m_month_short_names(short_names)
  93. {}
  94. explicit date_facet(const char_type* format_str,
  95. period_formatter_type per_formatter = period_formatter_type(),
  96. special_values_formatter_type sv_formatter = special_values_formatter_type(),
  97. date_gen_formatter_type dg_formatter = date_gen_formatter_type(),
  98. ::size_t ref_count = 0)
  99. : std::locale::facet(ref_count),
  100. m_format(format_str),
  101. m_month_format(short_month_format),
  102. m_weekday_format(short_weekday_format),
  103. m_period_formatter(per_formatter),
  104. m_date_gen_formatter(dg_formatter),
  105. m_special_values_formatter(sv_formatter)
  106. {}
  107. void format(const char_type* const format_str) {
  108. m_format = format_str;
  109. }
  110. virtual void set_iso_format()
  111. {
  112. m_format = iso_format_specifier;
  113. }
  114. virtual void set_iso_extended_format()
  115. {
  116. m_format = iso_format_extended_specifier;
  117. }
  118. void month_format(const char_type* const format_str) {
  119. m_month_format = format_str;
  120. }
  121. void weekday_format(const char_type* const format_str) {
  122. m_weekday_format = format_str;
  123. }
  124. void period_formatter(period_formatter_type per_formatter) {
  125. m_period_formatter= per_formatter;
  126. }
  127. void special_values_formatter(const special_values_formatter_type& svf)
  128. {
  129. m_special_values_formatter = svf;
  130. }
  131. void short_weekday_names(const input_collection_type& short_names)
  132. {
  133. m_weekday_short_names = short_names;
  134. }
  135. void long_weekday_names(const input_collection_type& long_names)
  136. {
  137. m_weekday_long_names = long_names;
  138. }
  139. void short_month_names(const input_collection_type& short_names)
  140. {
  141. m_month_short_names = short_names;
  142. }
  143. void long_month_names(const input_collection_type& long_names)
  144. {
  145. m_month_long_names = long_names;
  146. }
  147. void date_gen_phrase_strings(const input_collection_type& new_strings,
  148. typename date_gen_formatter_type::phrase_elements beg_pos=date_gen_formatter_type::first)
  149. {
  150. m_date_gen_formatter.elements(new_strings, beg_pos);
  151. }
  152. OutItrT put(OutItrT next,
  153. std::ios_base& a_ios,
  154. char_type fill_char,
  155. const date_type& d) const
  156. {
  157. if (d.is_special()) {
  158. return do_put_special(next, a_ios, fill_char, d.as_special());
  159. }
  160. //The following line of code required the date to support a to_tm function
  161. return do_put_tm(next, a_ios, fill_char, to_tm(d), m_format);
  162. }
  163. OutItrT put(OutItrT next,
  164. std::ios_base& a_ios,
  165. char_type fill_char,
  166. const duration_type& dd) const
  167. {
  168. if (dd.is_special()) {
  169. return do_put_special(next, a_ios, fill_char, dd.get_rep().as_special());
  170. }
  171. typedef std::num_put<CharT, OutItrT> num_put;
  172. if (std::has_facet<num_put>(a_ios.getloc())) {
  173. return std::use_facet<num_put>(a_ios.getloc()).put(next, a_ios, fill_char, dd.get_rep().as_number());
  174. }
  175. else {
  176. num_put* f = new num_put();
  177. std::locale l = std::locale(a_ios.getloc(), f);
  178. a_ios.imbue(l);
  179. return f->put(next, a_ios, fill_char, dd.get_rep().as_number());
  180. }
  181. }
  182. OutItrT put(OutItrT next,
  183. std::ios_base& a_ios,
  184. char_type fill_char,
  185. const month_type& m) const
  186. {
  187. //if (d.is_special()) {
  188. // return do_put_special(next, a_ios, fill_char, d.as_special());
  189. //}
  190. //The following line of code required the date to support a to_tm function
  191. std::tm dtm = {};
  192. dtm.tm_mon = m - 1;
  193. return do_put_tm(next, a_ios, fill_char, dtm, m_month_format);
  194. }
  195. //! puts the day of month
  196. OutItrT put(OutItrT next,
  197. std::ios_base& a_ios,
  198. char_type fill_char,
  199. const day_type& day) const
  200. {
  201. std::tm dtm = {};
  202. dtm.tm_mday = day.as_number();
  203. char_type tmp[3] = {'%','d'};
  204. string_type temp_format(tmp);
  205. return do_put_tm(next, a_ios, fill_char, dtm, temp_format);
  206. }
  207. OutItrT put(OutItrT next,
  208. std::ios_base& a_ios,
  209. char_type fill_char,
  210. const day_of_week_type& dow) const
  211. {
  212. //if (d.is_special()) {
  213. // return do_put_special(next, a_ios, fill_char, d.as_special());
  214. //}
  215. //The following line of code required the date to support a to_tm function
  216. std::tm dtm = {};
  217. dtm.tm_wday = dow;
  218. return do_put_tm(next, a_ios, fill_char, dtm, m_weekday_format);
  219. }
  220. OutItrT put(OutItrT next,
  221. std::ios_base& a_ios,
  222. char_type fill_char,
  223. const period_type& p) const
  224. {
  225. return m_period_formatter.put_period(next, a_ios, fill_char, p, *this);
  226. }
  227. OutItrT put(OutItrT next,
  228. std::ios_base& a_ios,
  229. char_type fill_char,
  230. const partial_date_type& pd) const
  231. {
  232. return m_date_gen_formatter.put_partial_date(next, a_ios, fill_char, pd, *this);
  233. }
  234. OutItrT put(OutItrT next,
  235. std::ios_base& a_ios,
  236. char_type fill_char,
  237. const nth_kday_type& nkd) const
  238. {
  239. return m_date_gen_formatter.put_nth_kday(next, a_ios, fill_char, nkd, *this);
  240. }
  241. OutItrT put(OutItrT next,
  242. std::ios_base& a_ios,
  243. char_type fill_char,
  244. const first_kday_type& fkd) const
  245. {
  246. return m_date_gen_formatter.put_first_kday(next, a_ios, fill_char, fkd, *this);
  247. }
  248. OutItrT put(OutItrT next,
  249. std::ios_base& a_ios,
  250. char_type fill_char,
  251. const last_kday_type& lkd) const
  252. {
  253. return m_date_gen_formatter.put_last_kday(next, a_ios, fill_char, lkd, *this);
  254. }
  255. OutItrT put(OutItrT next,
  256. std::ios_base& a_ios,
  257. char_type fill_char,
  258. const kday_before_type& fkb) const
  259. {
  260. return m_date_gen_formatter.put_kday_before(next, a_ios, fill_char, fkb, *this);
  261. }
  262. OutItrT put(OutItrT next,
  263. std::ios_base& a_ios,
  264. char_type fill_char,
  265. const kday_after_type& fka) const
  266. {
  267. return m_date_gen_formatter.put_kday_after(next, a_ios, fill_char, fka, *this);
  268. }
  269. protected:
  270. virtual OutItrT do_put_special(OutItrT next,
  271. std::ios_base& /*a_ios*/,
  272. char_type /*fill_char*/,
  273. const boost::date_time::special_values sv) const
  274. {
  275. m_special_values_formatter.put_special(next, sv);
  276. return next;
  277. }
  278. virtual OutItrT do_put_tm(OutItrT next,
  279. std::ios_base& a_ios,
  280. char_type fill_char,
  281. const tm& tm_value,
  282. string_type a_format) const
  283. {
  284. // update format string with custom names
  285. if (m_weekday_long_names.size()) {
  286. boost::algorithm::replace_all(a_format,
  287. long_weekday_format,
  288. m_weekday_long_names[tm_value.tm_wday]);
  289. }
  290. if (m_weekday_short_names.size()) {
  291. boost::algorithm::replace_all(a_format,
  292. short_weekday_format,
  293. m_weekday_short_names[tm_value.tm_wday]);
  294. }
  295. if (m_month_long_names.size()) {
  296. boost::algorithm::replace_all(a_format,
  297. long_month_format,
  298. m_month_long_names[tm_value.tm_mon]);
  299. }
  300. if (m_month_short_names.size()) {
  301. boost::algorithm::replace_all(a_format,
  302. short_month_format,
  303. m_month_short_names[tm_value.tm_mon]);
  304. }
  305. // use time_put facet to create final string
  306. const char_type* p_format = a_format.c_str();
  307. return std::use_facet<std::time_put<CharT> >(a_ios.getloc()).put(next, a_ios,
  308. fill_char,
  309. &tm_value,
  310. p_format,
  311. p_format + a_format.size());
  312. }
  313. protected:
  314. string_type m_format;
  315. string_type m_month_format;
  316. string_type m_weekday_format;
  317. period_formatter_type m_period_formatter;
  318. date_gen_formatter_type m_date_gen_formatter;
  319. special_values_formatter_type m_special_values_formatter;
  320. input_collection_type m_month_short_names;
  321. input_collection_type m_month_long_names;
  322. input_collection_type m_weekday_short_names;
  323. input_collection_type m_weekday_long_names;
  324. private:
  325. };
  326. template <class date_type, class CharT, class OutItrT>
  327. std::locale::id date_facet<date_type, CharT, OutItrT>::id;
  328. template <class date_type, class CharT, class OutItrT>
  329. const typename date_facet<date_type, CharT, OutItrT>::char_type
  330. date_facet<date_type, CharT, OutItrT>::long_weekday_format[3] = {'%','A'};
  331. template <class date_type, class CharT, class OutItrT>
  332. const typename date_facet<date_type, CharT, OutItrT>::char_type
  333. date_facet<date_type, CharT, OutItrT>::short_weekday_format[3] = {'%','a'};
  334. template <class date_type, class CharT, class OutItrT>
  335. const typename date_facet<date_type, CharT, OutItrT>::char_type
  336. date_facet<date_type, CharT, OutItrT>::long_month_format[3] = {'%','B'};
  337. template <class date_type, class CharT, class OutItrT>
  338. const typename date_facet<date_type, CharT, OutItrT>::char_type
  339. date_facet<date_type, CharT, OutItrT>::short_month_format[3] = {'%','b'};
  340. template <class date_type, class CharT, class OutItrT>
  341. const typename date_facet<date_type, CharT, OutItrT>::char_type
  342. date_facet<date_type, CharT, OutItrT>::default_period_separator[4] = { ' ', '/', ' '};
  343. template <class date_type, class CharT, class OutItrT>
  344. const typename date_facet<date_type, CharT, OutItrT>::char_type
  345. date_facet<date_type, CharT, OutItrT>::standard_format_specifier[3] =
  346. {'%', 'x' };
  347. template <class date_type, class CharT, class OutItrT>
  348. const typename date_facet<date_type, CharT, OutItrT>::char_type
  349. date_facet<date_type, CharT, OutItrT>::iso_format_specifier[7] =
  350. {'%', 'Y', '%', 'm', '%', 'd' };
  351. template <class date_type, class CharT, class OutItrT>
  352. const typename date_facet<date_type, CharT, OutItrT>::char_type
  353. date_facet<date_type, CharT, OutItrT>::iso_format_extended_specifier[9] =
  354. {'%', 'Y', '-', '%', 'm', '-', '%', 'd' };
  355. template <class date_type, class CharT, class OutItrT>
  356. const typename date_facet<date_type, CharT, OutItrT>::char_type
  357. date_facet<date_type, CharT, OutItrT>::default_date_format[9] =
  358. {'%','Y','-','%','b','-','%','d'};
  359. //! Input facet
  360. template <class date_type,
  361. class CharT,
  362. class InItrT = std::istreambuf_iterator<CharT, std::char_traits<CharT> > >
  363. class date_input_facet : public std::locale::facet {
  364. public:
  365. typedef typename date_type::duration_type duration_type;
  366. // greg_weekday is gregorian_calendar::day_of_week_type
  367. typedef typename date_type::day_of_week_type day_of_week_type;
  368. typedef typename date_type::day_type day_type;
  369. typedef typename date_type::month_type month_type;
  370. typedef typename date_type::year_type year_type;
  371. typedef boost::date_time::period<date_type,duration_type> period_type;
  372. typedef std::basic_string<CharT> string_type;
  373. typedef CharT char_type;
  374. typedef boost::date_time::period_parser<date_type, CharT> period_parser_type;
  375. typedef boost::date_time::special_values_parser<date_type,CharT> special_values_parser_type;
  376. typedef std::vector<std::basic_string<CharT> > input_collection_type;
  377. typedef format_date_parser<date_type, CharT> format_date_parser_type;
  378. // date_generators stuff goes here
  379. typedef date_generator_parser<date_type, CharT> date_gen_parser_type;
  380. typedef partial_date<date_type> partial_date_type;
  381. typedef nth_kday_of_month<date_type> nth_kday_type;
  382. typedef first_kday_of_month<date_type> first_kday_type;
  383. typedef last_kday_of_month<date_type> last_kday_type;
  384. typedef first_kday_after<date_type> kday_after_type;
  385. typedef first_kday_before<date_type> kday_before_type;
  386. static const char_type long_weekday_format[3];
  387. static const char_type short_weekday_format[3];
  388. static const char_type long_month_format[3];
  389. static const char_type short_month_format[3];
  390. static const char_type four_digit_year_format[3];
  391. static const char_type two_digit_year_format[3];
  392. static const char_type default_period_separator[4];
  393. static const char_type standard_format_specifier[3];
  394. static const char_type iso_format_specifier[7];
  395. static const char_type iso_format_extended_specifier[9];
  396. static const char_type default_date_format[9]; // YYYY-Mon-DD
  397. static std::locale::id id;
  398. explicit date_input_facet(::size_t a_ref = 0)
  399. : std::locale::facet(a_ref),
  400. m_format(default_date_format),
  401. m_month_format(short_month_format),
  402. m_weekday_format(short_weekday_format),
  403. m_year_format(four_digit_year_format),
  404. m_parser(m_format, std::locale::classic())
  405. // default period_parser & special_values_parser used
  406. {}
  407. explicit date_input_facet(const string_type& format_str,
  408. ::size_t a_ref = 0)
  409. : std::locale::facet(a_ref),
  410. m_format(format_str),
  411. m_month_format(short_month_format),
  412. m_weekday_format(short_weekday_format),
  413. m_year_format(four_digit_year_format),
  414. m_parser(m_format, std::locale::classic())
  415. // default period_parser & special_values_parser used
  416. {}
  417. explicit date_input_facet(const string_type& format_str,
  418. const format_date_parser_type& date_parser,
  419. const special_values_parser_type& sv_parser,
  420. const period_parser_type& per_parser,
  421. const date_gen_parser_type& date_gen_parser,
  422. ::size_t ref_count = 0)
  423. : std::locale::facet(ref_count),
  424. m_format(format_str),
  425. m_month_format(short_month_format),
  426. m_weekday_format(short_weekday_format),
  427. m_year_format(four_digit_year_format),
  428. m_parser(date_parser),
  429. m_date_gen_parser(date_gen_parser),
  430. m_period_parser(per_parser),
  431. m_sv_parser(sv_parser)
  432. {}
  433. void format(const char_type* const format_str) {
  434. m_format = format_str;
  435. }
  436. virtual void set_iso_format()
  437. {
  438. m_format = iso_format_specifier;
  439. }
  440. virtual void set_iso_extended_format()
  441. {
  442. m_format = iso_format_extended_specifier;
  443. }
  444. void month_format(const char_type* const format_str) {
  445. m_month_format = format_str;
  446. }
  447. void weekday_format(const char_type* const format_str) {
  448. m_weekday_format = format_str;
  449. }
  450. void year_format(const char_type* const format_str) {
  451. m_year_format = format_str;
  452. }
  453. void period_parser(period_parser_type per_parser) {
  454. m_period_parser = per_parser;
  455. }
  456. void short_weekday_names(const input_collection_type& weekday_names)
  457. {
  458. m_parser.short_weekday_names(weekday_names);
  459. }
  460. void long_weekday_names(const input_collection_type& weekday_names)
  461. {
  462. m_parser.long_weekday_names(weekday_names);
  463. }
  464. void short_month_names(const input_collection_type& month_names)
  465. {
  466. m_parser.short_month_names(month_names);
  467. }
  468. void long_month_names(const input_collection_type& month_names)
  469. {
  470. m_parser.long_month_names(month_names);
  471. }
  472. void date_gen_element_strings(const input_collection_type& col)
  473. {
  474. m_date_gen_parser.element_strings(col);
  475. }
  476. void date_gen_element_strings(const string_type& first,
  477. const string_type& second,
  478. const string_type& third,
  479. const string_type& fourth,
  480. const string_type& fifth,
  481. const string_type& last,
  482. const string_type& before,
  483. const string_type& after,
  484. const string_type& of)
  485. {
  486. m_date_gen_parser.element_strings(first,second,third,fourth,fifth,last,before,after,of);
  487. }
  488. void special_values_parser(special_values_parser_type sv_parser)
  489. {
  490. m_sv_parser = sv_parser;
  491. }
  492. InItrT get(InItrT& from,
  493. InItrT& to,
  494. std::ios_base& /*a_ios*/,
  495. date_type& d) const
  496. {
  497. d = m_parser.parse_date(from, to, m_format, m_sv_parser);
  498. return from;
  499. }
  500. InItrT get(InItrT& from,
  501. InItrT& to,
  502. std::ios_base& /*a_ios*/,
  503. month_type& m) const
  504. {
  505. m = m_parser.parse_month(from, to, m_month_format);
  506. return from;
  507. }
  508. InItrT get(InItrT& from,
  509. InItrT& to,
  510. std::ios_base& /*a_ios*/,
  511. day_of_week_type& wd) const
  512. {
  513. wd = m_parser.parse_weekday(from, to, m_weekday_format);
  514. return from;
  515. }
  516. //! Expects 1 or 2 digit day range: 1-31
  517. InItrT get(InItrT& from,
  518. InItrT& to,
  519. std::ios_base& /*a_ios*/,
  520. day_type& d) const
  521. {
  522. d = m_parser.parse_var_day_of_month(from, to);
  523. return from;
  524. }
  525. InItrT get(InItrT& from,
  526. InItrT& to,
  527. std::ios_base& /*a_ios*/,
  528. year_type& y) const
  529. {
  530. y = m_parser.parse_year(from, to, m_year_format);
  531. return from;
  532. }
  533. InItrT get(InItrT& from,
  534. InItrT& to,
  535. std::ios_base& a_ios,
  536. duration_type& dd) const
  537. {
  538. // skip leading whitespace
  539. while(std::isspace(*from) && from != to) { ++from; }
  540. /* num_get.get() will always consume the first character if it
  541. * is a sign indicator (+/-). Special value strings may begin
  542. * with one of these signs so we'll need a copy of it
  543. * in case num_get.get() fails. */
  544. char_type c = '\0';
  545. // TODO Are these characters somewhere in the locale?
  546. if(*from == '-' || *from == '+') {
  547. c = *from;
  548. }
  549. typedef std::num_get<CharT, InItrT> num_get;
  550. typename duration_type::duration_rep_type val = 0;
  551. std::ios_base::iostate err = std::ios_base::goodbit;
  552. if (std::has_facet<num_get>(a_ios.getloc())) {
  553. from = std::use_facet<num_get>(a_ios.getloc()).get(from, to, a_ios, err, val);
  554. }
  555. else {
  556. num_get* ng = new num_get();
  557. std::locale l = std::locale(a_ios.getloc(), ng);
  558. a_ios.imbue(l);
  559. from = ng->get(from, to, a_ios, err, val);
  560. }
  561. if(err & std::ios_base::failbit){
  562. typedef typename special_values_parser_type::match_results match_results;
  563. match_results mr;
  564. if(c == '-' || c == '+') { // was the first character consumed?
  565. mr.cache += c;
  566. }
  567. m_sv_parser.match(from, to, mr);
  568. if(mr.current_match == match_results::PARSE_ERROR) {
  569. boost::throw_exception(std::ios_base::failure("Parse failed. No match found for '" + mr.cache + "'"));
  570. BOOST_DATE_TIME_UNREACHABLE_EXPRESSION(return from); // should never reach
  571. }
  572. dd = duration_type(static_cast<special_values>(mr.current_match));
  573. }
  574. else {
  575. dd = duration_type(val);
  576. }
  577. return from;
  578. }
  579. InItrT get(InItrT& from,
  580. InItrT& to,
  581. std::ios_base& a_ios,
  582. period_type& p) const
  583. {
  584. p = m_period_parser.get_period(from, to, a_ios, p, duration_type::unit(), *this);
  585. return from;
  586. }
  587. InItrT get(InItrT& from,
  588. InItrT& to,
  589. std::ios_base& a_ios,
  590. nth_kday_type& nkd) const
  591. {
  592. nkd = m_date_gen_parser.get_nth_kday_type(from, to, a_ios, *this);
  593. return from;
  594. }
  595. InItrT get(InItrT& from,
  596. InItrT& to,
  597. std::ios_base& a_ios,
  598. partial_date_type& pd) const
  599. {
  600. pd = m_date_gen_parser.get_partial_date_type(from, to, a_ios, *this);
  601. return from;
  602. }
  603. InItrT get(InItrT& from,
  604. InItrT& to,
  605. std::ios_base& a_ios,
  606. first_kday_type& fkd) const
  607. {
  608. fkd = m_date_gen_parser.get_first_kday_type(from, to, a_ios, *this);
  609. return from;
  610. }
  611. InItrT get(InItrT& from,
  612. InItrT& to,
  613. std::ios_base& a_ios,
  614. last_kday_type& lkd) const
  615. {
  616. lkd = m_date_gen_parser.get_last_kday_type(from, to, a_ios, *this);
  617. return from;
  618. }
  619. InItrT get(InItrT& from,
  620. InItrT& to,
  621. std::ios_base& a_ios,
  622. kday_before_type& fkb) const
  623. {
  624. fkb = m_date_gen_parser.get_kday_before_type(from, to, a_ios, *this);
  625. return from;
  626. }
  627. InItrT get(InItrT& from,
  628. InItrT& to,
  629. std::ios_base& a_ios,
  630. kday_after_type& fka) const
  631. {
  632. fka = m_date_gen_parser.get_kday_after_type(from, to, a_ios, *this);
  633. return from;
  634. }
  635. protected:
  636. string_type m_format;
  637. string_type m_month_format;
  638. string_type m_weekday_format;
  639. string_type m_year_format;
  640. format_date_parser_type m_parser;
  641. date_gen_parser_type m_date_gen_parser;
  642. period_parser_type m_period_parser;
  643. special_values_parser_type m_sv_parser;
  644. private:
  645. };
  646. template <class date_type, class CharT, class OutItrT>
  647. std::locale::id date_input_facet<date_type, CharT, OutItrT>::id;
  648. template <class date_type, class CharT, class OutItrT>
  649. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  650. date_input_facet<date_type, CharT, OutItrT>::long_weekday_format[3] = {'%','A'};
  651. template <class date_type, class CharT, class OutItrT>
  652. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  653. date_input_facet<date_type, CharT, OutItrT>::short_weekday_format[3] = {'%','a'};
  654. template <class date_type, class CharT, class OutItrT>
  655. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  656. date_input_facet<date_type, CharT, OutItrT>::long_month_format[3] = {'%','B'};
  657. template <class date_type, class CharT, class OutItrT>
  658. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  659. date_input_facet<date_type, CharT, OutItrT>::short_month_format[3] = {'%','b'};
  660. template <class date_type, class CharT, class OutItrT>
  661. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  662. date_input_facet<date_type, CharT, OutItrT>::four_digit_year_format[3] = {'%','Y'};
  663. template <class date_type, class CharT, class OutItrT>
  664. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  665. date_input_facet<date_type, CharT, OutItrT>::two_digit_year_format[3] = {'%','y'};
  666. template <class date_type, class CharT, class OutItrT>
  667. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  668. date_input_facet<date_type, CharT, OutItrT>::default_period_separator[4] = { ' ', '/', ' '};
  669. template <class date_type, class CharT, class OutItrT>
  670. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  671. date_input_facet<date_type, CharT, OutItrT>::standard_format_specifier[3] =
  672. {'%', 'x' };
  673. template <class date_type, class CharT, class OutItrT>
  674. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  675. date_input_facet<date_type, CharT, OutItrT>::iso_format_specifier[7] =
  676. {'%', 'Y', '%', 'm', '%', 'd' };
  677. template <class date_type, class CharT, class OutItrT>
  678. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  679. date_input_facet<date_type, CharT, OutItrT>::iso_format_extended_specifier[9] =
  680. {'%', 'Y', '-', '%', 'm', '-', '%', 'd' };
  681. template <class date_type, class CharT, class OutItrT>
  682. const typename date_input_facet<date_type, CharT, OutItrT>::char_type
  683. date_input_facet<date_type, CharT, OutItrT>::default_date_format[9] =
  684. {'%','Y','-','%','b','-','%','d'};
  685. } } // namespaces
  686. #endif