gregorian_io.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. #ifndef DATE_TIME_GREGORIAN_IO_HPP__
  2. #define DATE_TIME_GREGORIAN_IO_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 <locale>
  11. #include <iostream>
  12. #include <iterator> // i/ostreambuf_iterator
  13. #include <boost/io/ios_state.hpp>
  14. #include <boost/date_time/date_facet.hpp>
  15. #include <boost/date_time/period_parser.hpp>
  16. #include <boost/date_time/period_formatter.hpp>
  17. #include <boost/date_time/special_values_parser.hpp>
  18. #include <boost/date_time/special_values_formatter.hpp>
  19. #include <boost/date_time/gregorian/gregorian_types.hpp>
  20. #include <boost/date_time/gregorian/conversion.hpp> // to_tm will be needed in the facets
  21. namespace boost {
  22. namespace gregorian {
  23. typedef boost::date_time::period_formatter<wchar_t> wperiod_formatter;
  24. typedef boost::date_time::period_formatter<char> period_formatter;
  25. typedef boost::date_time::date_facet<date,wchar_t> wdate_facet;
  26. typedef boost::date_time::date_facet<date,char> date_facet;
  27. typedef boost::date_time::period_parser<date,char> period_parser;
  28. typedef boost::date_time::period_parser<date,wchar_t> wperiod_parser;
  29. typedef boost::date_time::special_values_formatter<char> special_values_formatter;
  30. typedef boost::date_time::special_values_formatter<wchar_t> wspecial_values_formatter;
  31. typedef boost::date_time::special_values_parser<date,char> special_values_parser;
  32. typedef boost::date_time::special_values_parser<date,wchar_t> wspecial_values_parser;
  33. typedef boost::date_time::date_input_facet<date,char> date_input_facet;
  34. typedef boost::date_time::date_input_facet<date,wchar_t> wdate_input_facet;
  35. template <class CharT, class TraitsT>
  36. inline std::basic_ostream<CharT, TraitsT>&
  37. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date& d) {
  38. boost::io::ios_flags_saver iflags(os);
  39. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  40. std::ostreambuf_iterator<CharT> output_itr(os);
  41. if (std::has_facet<custom_date_facet>(os.getloc()))
  42. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), d);
  43. else {
  44. //instantiate a custom facet for dealing with dates since the user
  45. //has not put one in the stream so far. This is for efficiency
  46. //since we would always need to reconstruct for every date
  47. //if the locale did not already exist. Of course this will be overridden
  48. //if the user imbues at some later point. With the default settings
  49. //for the facet the resulting format will be the same as the
  50. //std::time_facet settings.
  51. custom_date_facet* f = new custom_date_facet();
  52. std::locale l = std::locale(os.getloc(), f);
  53. os.imbue(l);
  54. f->put(output_itr, os, os.fill(), d);
  55. }
  56. return os;
  57. }
  58. //! input operator for date
  59. template <class CharT, class Traits>
  60. inline
  61. std::basic_istream<CharT, Traits>&
  62. operator>>(std::basic_istream<CharT, Traits>& is, date& d)
  63. {
  64. boost::io::ios_flags_saver iflags(is);
  65. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  66. if (strm_sentry) {
  67. try {
  68. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  69. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  70. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  71. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, d);
  72. }
  73. else {
  74. date_input_facet_local* f = new date_input_facet_local();
  75. std::locale l = std::locale(is.getloc(), f);
  76. is.imbue(l);
  77. f->get(sit, str_end, is, d);
  78. }
  79. }
  80. catch(...) {
  81. // mask tells us what exceptions are turned on
  82. std::ios_base::iostate exception_mask = is.exceptions();
  83. // if the user wants exceptions on failbit, we'll rethrow our
  84. // date_time exception & set the failbit
  85. if(std::ios_base::failbit & exception_mask) {
  86. try { is.setstate(std::ios_base::failbit); }
  87. catch(std::ios_base::failure&) {} // ignore this one
  88. throw; // rethrow original exception
  89. }
  90. else {
  91. // if the user want's to fail quietly, we simply set the failbit
  92. is.setstate(std::ios_base::failbit);
  93. }
  94. }
  95. }
  96. return is;
  97. }
  98. template <class CharT, class TraitsT>
  99. inline std::basic_ostream<CharT, TraitsT>&
  100. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date_duration& dd) {
  101. boost::io::ios_flags_saver iflags(os);
  102. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  103. std::ostreambuf_iterator<CharT> output_itr(os);
  104. if (std::has_facet<custom_date_facet>(os.getloc()))
  105. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), dd);
  106. else {
  107. custom_date_facet* f = new custom_date_facet();
  108. std::locale l = std::locale(os.getloc(), f);
  109. os.imbue(l);
  110. f->put(output_itr, os, os.fill(), dd);
  111. }
  112. return os;
  113. }
  114. //! input operator for date_duration
  115. template <class CharT, class Traits>
  116. inline
  117. std::basic_istream<CharT, Traits>&
  118. operator>>(std::basic_istream<CharT, Traits>& is, date_duration& dd)
  119. {
  120. boost::io::ios_flags_saver iflags(is);
  121. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  122. if (strm_sentry) {
  123. try {
  124. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  125. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  126. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  127. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, dd);
  128. }
  129. else {
  130. date_input_facet_local* f = new date_input_facet_local();
  131. std::locale l = std::locale(is.getloc(), f);
  132. is.imbue(l);
  133. f->get(sit, str_end, is, dd);
  134. }
  135. }
  136. catch(...) {
  137. std::ios_base::iostate exception_mask = is.exceptions();
  138. if(std::ios_base::failbit & exception_mask) {
  139. try { is.setstate(std::ios_base::failbit); }
  140. catch(std::ios_base::failure&) {}
  141. throw; // rethrow original exception
  142. }
  143. else {
  144. is.setstate(std::ios_base::failbit);
  145. }
  146. }
  147. }
  148. return is;
  149. }
  150. template <class CharT, class TraitsT>
  151. inline std::basic_ostream<CharT, TraitsT>&
  152. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date_period& dp) {
  153. boost::io::ios_flags_saver iflags(os);
  154. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  155. std::ostreambuf_iterator<CharT> output_itr(os);
  156. if (std::has_facet<custom_date_facet>(os.getloc()))
  157. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), dp);
  158. else {
  159. //instantiate a custom facet for dealing with date periods since the user
  160. //has not put one in the stream so far. This is for efficiency
  161. //since we would always need to reconstruct for every time period
  162. //if the local did not already exist. Of course this will be overridden
  163. //if the user imbues at some later point. With the default settings
  164. //for the facet the resulting format will be the same as the
  165. //std::time_facet settings.
  166. custom_date_facet* f = new custom_date_facet();
  167. std::locale l = std::locale(os.getloc(), f);
  168. os.imbue(l);
  169. f->put(output_itr, os, os.fill(), dp);
  170. }
  171. return os;
  172. }
  173. //! input operator for date_period
  174. template <class CharT, class Traits>
  175. inline
  176. std::basic_istream<CharT, Traits>&
  177. operator>>(std::basic_istream<CharT, Traits>& is, date_period& dp)
  178. {
  179. boost::io::ios_flags_saver iflags(is);
  180. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  181. if (strm_sentry) {
  182. try {
  183. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  184. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  185. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  186. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, dp);
  187. }
  188. else {
  189. date_input_facet_local* f = new date_input_facet_local();
  190. std::locale l = std::locale(is.getloc(), f);
  191. is.imbue(l);
  192. f->get(sit, str_end, is, dp);
  193. }
  194. }
  195. catch(...) {
  196. std::ios_base::iostate exception_mask = is.exceptions();
  197. if(std::ios_base::failbit & exception_mask) {
  198. try { is.setstate(std::ios_base::failbit); }
  199. catch(std::ios_base::failure&) {}
  200. throw; // rethrow original exception
  201. }
  202. else {
  203. is.setstate(std::ios_base::failbit);
  204. }
  205. }
  206. }
  207. return is;
  208. }
  209. /********** small gregorian types **********/
  210. template <class CharT, class TraitsT>
  211. inline std::basic_ostream<CharT, TraitsT>&
  212. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::greg_month& gm) {
  213. boost::io::ios_flags_saver iflags(os);
  214. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  215. std::ostreambuf_iterator<CharT> output_itr(os);
  216. if (std::has_facet<custom_date_facet>(os.getloc()))
  217. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), gm);
  218. else {
  219. custom_date_facet* f = new custom_date_facet();//-> 10/1074199752/32 because year & day not initialized in put(...)
  220. //custom_date_facet* f = new custom_date_facet("%B");
  221. std::locale l = std::locale(os.getloc(), f);
  222. os.imbue(l);
  223. f->put(output_itr, os, os.fill(), gm);
  224. }
  225. return os;
  226. }
  227. //! input operator for greg_month
  228. template <class CharT, class Traits>
  229. inline
  230. std::basic_istream<CharT, Traits>&
  231. operator>>(std::basic_istream<CharT, Traits>& is, greg_month& m)
  232. {
  233. boost::io::ios_flags_saver iflags(is);
  234. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  235. if (strm_sentry) {
  236. try {
  237. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  238. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  239. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  240. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, m);
  241. }
  242. else {
  243. date_input_facet_local* f = new date_input_facet_local();
  244. std::locale l = std::locale(is.getloc(), f);
  245. is.imbue(l);
  246. f->get(sit, str_end, is, m);
  247. }
  248. }
  249. catch(...) {
  250. std::ios_base::iostate exception_mask = is.exceptions();
  251. if(std::ios_base::failbit & exception_mask) {
  252. try { is.setstate(std::ios_base::failbit); }
  253. catch(std::ios_base::failure&) {}
  254. throw; // rethrow original exception
  255. }
  256. else {
  257. is.setstate(std::ios_base::failbit);
  258. }
  259. }
  260. }
  261. return is;
  262. }
  263. template <class CharT, class TraitsT>
  264. inline std::basic_ostream<CharT, TraitsT>&
  265. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::greg_weekday& gw) {
  266. boost::io::ios_flags_saver iflags(os);
  267. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  268. std::ostreambuf_iterator<CharT> output_itr(os);
  269. if (std::has_facet<custom_date_facet>(os.getloc()))
  270. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), gw);
  271. else {
  272. custom_date_facet* f = new custom_date_facet();
  273. std::locale l = std::locale(os.getloc(), f);
  274. os.imbue(l);
  275. f->put(output_itr, os, os.fill(), gw);
  276. }
  277. return os;
  278. }
  279. //! input operator for greg_weekday
  280. template <class CharT, class Traits>
  281. inline
  282. std::basic_istream<CharT, Traits>&
  283. operator>>(std::basic_istream<CharT, Traits>& is, greg_weekday& wd)
  284. {
  285. boost::io::ios_flags_saver iflags(is);
  286. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  287. if (strm_sentry) {
  288. try {
  289. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  290. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  291. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  292. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, wd);
  293. }
  294. else {
  295. date_input_facet_local* f = new date_input_facet_local();
  296. std::locale l = std::locale(is.getloc(), f);
  297. is.imbue(l);
  298. f->get(sit, str_end, is, wd);
  299. }
  300. }
  301. catch(...) {
  302. std::ios_base::iostate exception_mask = is.exceptions();
  303. if(std::ios_base::failbit & exception_mask) {
  304. try { is.setstate(std::ios_base::failbit); }
  305. catch(std::ios_base::failure&) {}
  306. throw; // rethrow original exception
  307. }
  308. else {
  309. is.setstate(std::ios_base::failbit);
  310. }
  311. }
  312. }
  313. return is;
  314. }
  315. //NOTE: output operator for greg_day was not necessary
  316. //! input operator for greg_day
  317. template <class CharT, class Traits>
  318. inline
  319. std::basic_istream<CharT, Traits>&
  320. operator>>(std::basic_istream<CharT, Traits>& is, greg_day& gd)
  321. {
  322. boost::io::ios_flags_saver iflags(is);
  323. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  324. if (strm_sentry) {
  325. try {
  326. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  327. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  328. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  329. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, gd);
  330. }
  331. else {
  332. date_input_facet_local* f = new date_input_facet_local();
  333. std::locale l = std::locale(is.getloc(), f);
  334. is.imbue(l);
  335. f->get(sit, str_end, is, gd);
  336. }
  337. }
  338. catch(...) {
  339. std::ios_base::iostate exception_mask = is.exceptions();
  340. if(std::ios_base::failbit & exception_mask) {
  341. try { is.setstate(std::ios_base::failbit); }
  342. catch(std::ios_base::failure&) {}
  343. throw; // rethrow original exception
  344. }
  345. else {
  346. is.setstate(std::ios_base::failbit);
  347. }
  348. }
  349. }
  350. return is;
  351. }
  352. //NOTE: output operator for greg_year was not necessary
  353. //! input operator for greg_year
  354. template <class CharT, class Traits>
  355. inline
  356. std::basic_istream<CharT, Traits>&
  357. operator>>(std::basic_istream<CharT, Traits>& is, greg_year& gy)
  358. {
  359. boost::io::ios_flags_saver iflags(is);
  360. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  361. if (strm_sentry) {
  362. try {
  363. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  364. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  365. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  366. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, gy);
  367. }
  368. else {
  369. date_input_facet_local* f = new date_input_facet_local();
  370. std::locale l = std::locale(is.getloc(), f);
  371. is.imbue(l);
  372. f->get(sit, str_end, is, gy);
  373. }
  374. }
  375. catch(...) {
  376. std::ios_base::iostate exception_mask = is.exceptions();
  377. if(std::ios_base::failbit & exception_mask) {
  378. try { is.setstate(std::ios_base::failbit); }
  379. catch(std::ios_base::failure&) {}
  380. throw; // rethrow original exception
  381. }
  382. else {
  383. is.setstate(std::ios_base::failbit);
  384. }
  385. }
  386. }
  387. return is;
  388. }
  389. /********** date generator types **********/
  390. template <class CharT, class TraitsT>
  391. inline std::basic_ostream<CharT, TraitsT>&
  392. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::partial_date& pd) {
  393. boost::io::ios_flags_saver iflags(os);
  394. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  395. std::ostreambuf_iterator<CharT> output_itr(os);
  396. if (std::has_facet<custom_date_facet>(os.getloc()))
  397. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), pd);
  398. else {
  399. custom_date_facet* f = new custom_date_facet();
  400. std::locale l = std::locale(os.getloc(), f);
  401. os.imbue(l);
  402. f->put(output_itr, os, os.fill(), pd);
  403. }
  404. return os;
  405. }
  406. //! input operator for partial_date
  407. template <class CharT, class Traits>
  408. inline
  409. std::basic_istream<CharT, Traits>&
  410. operator>>(std::basic_istream<CharT, Traits>& is, partial_date& pd)
  411. {
  412. boost::io::ios_flags_saver iflags(is);
  413. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  414. if (strm_sentry) {
  415. try {
  416. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  417. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  418. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  419. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, pd);
  420. }
  421. else {
  422. date_input_facet_local* f = new date_input_facet_local();
  423. std::locale l = std::locale(is.getloc(), f);
  424. is.imbue(l);
  425. f->get(sit, str_end, is, pd);
  426. }
  427. }
  428. catch(...) {
  429. std::ios_base::iostate exception_mask = is.exceptions();
  430. if(std::ios_base::failbit & exception_mask) {
  431. try { is.setstate(std::ios_base::failbit); }
  432. catch(std::ios_base::failure&) {}
  433. throw; // rethrow original exception
  434. }
  435. else {
  436. is.setstate(std::ios_base::failbit);
  437. }
  438. }
  439. }
  440. return is;
  441. }
  442. template <class CharT, class TraitsT>
  443. inline std::basic_ostream<CharT, TraitsT>&
  444. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::nth_day_of_the_week_in_month& nkd) {
  445. boost::io::ios_flags_saver iflags(os);
  446. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  447. std::ostreambuf_iterator<CharT> output_itr(os);
  448. if (std::has_facet<custom_date_facet>(os.getloc()))
  449. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), nkd);
  450. else {
  451. custom_date_facet* f = new custom_date_facet();
  452. std::locale l = std::locale(os.getloc(), f);
  453. os.imbue(l);
  454. f->put(output_itr, os, os.fill(), nkd);
  455. }
  456. return os;
  457. }
  458. //! input operator for nth_day_of_the_week_in_month
  459. template <class CharT, class Traits>
  460. inline
  461. std::basic_istream<CharT, Traits>&
  462. operator>>(std::basic_istream<CharT, Traits>& is,
  463. nth_day_of_the_week_in_month& nday)
  464. {
  465. boost::io::ios_flags_saver iflags(is);
  466. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  467. if (strm_sentry) {
  468. try {
  469. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  470. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  471. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  472. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, nday);
  473. }
  474. else {
  475. date_input_facet_local* f = new date_input_facet_local();
  476. std::locale l = std::locale(is.getloc(), f);
  477. is.imbue(l);
  478. f->get(sit, str_end, is, nday);
  479. }
  480. }
  481. catch(...) {
  482. std::ios_base::iostate exception_mask = is.exceptions();
  483. if(std::ios_base::failbit & exception_mask) {
  484. try { is.setstate(std::ios_base::failbit); }
  485. catch(std::ios_base::failure&) {}
  486. throw; // rethrow original exception
  487. }
  488. else {
  489. is.setstate(std::ios_base::failbit);
  490. }
  491. }
  492. }
  493. return is;
  494. }
  495. template <class CharT, class TraitsT>
  496. inline std::basic_ostream<CharT, TraitsT>&
  497. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::first_day_of_the_week_in_month& fkd) {
  498. boost::io::ios_flags_saver iflags(os);
  499. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  500. std::ostreambuf_iterator<CharT> output_itr(os);
  501. if (std::has_facet<custom_date_facet>(os.getloc()))
  502. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), fkd);
  503. else {
  504. custom_date_facet* f = new custom_date_facet();
  505. std::locale l = std::locale(os.getloc(), f);
  506. os.imbue(l);
  507. f->put(output_itr, os, os.fill(), fkd);
  508. }
  509. return os;
  510. }
  511. //! input operator for first_day_of_the_week_in_month
  512. template <class CharT, class Traits>
  513. inline
  514. std::basic_istream<CharT, Traits>&
  515. operator>>(std::basic_istream<CharT, Traits>& is,
  516. first_day_of_the_week_in_month& fkd)
  517. {
  518. boost::io::ios_flags_saver iflags(is);
  519. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  520. if (strm_sentry) {
  521. try {
  522. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  523. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  524. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  525. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, fkd);
  526. }
  527. else {
  528. date_input_facet_local* f = new date_input_facet_local();
  529. std::locale l = std::locale(is.getloc(), f);
  530. is.imbue(l);
  531. f->get(sit, str_end, is, fkd);
  532. }
  533. }
  534. catch(...) {
  535. std::ios_base::iostate exception_mask = is.exceptions();
  536. if(std::ios_base::failbit & exception_mask) {
  537. try { is.setstate(std::ios_base::failbit); }
  538. catch(std::ios_base::failure&) {}
  539. throw; // rethrow original exception
  540. }
  541. else {
  542. is.setstate(std::ios_base::failbit);
  543. }
  544. }
  545. }
  546. return is;
  547. }
  548. template <class CharT, class TraitsT>
  549. inline std::basic_ostream<CharT, TraitsT>&
  550. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::last_day_of_the_week_in_month& lkd) {
  551. boost::io::ios_flags_saver iflags(os);
  552. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  553. std::ostreambuf_iterator<CharT> output_itr(os);
  554. if (std::has_facet<custom_date_facet>(os.getloc()))
  555. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), lkd);
  556. else {
  557. custom_date_facet* f = new custom_date_facet();
  558. std::locale l = std::locale(os.getloc(), f);
  559. os.imbue(l);
  560. f->put(output_itr, os, os.fill(), lkd);
  561. }
  562. return os;
  563. }
  564. //! input operator for last_day_of_the_week_in_month
  565. template <class CharT, class Traits>
  566. inline
  567. std::basic_istream<CharT, Traits>&
  568. operator>>(std::basic_istream<CharT, Traits>& is,
  569. last_day_of_the_week_in_month& lkd)
  570. {
  571. boost::io::ios_flags_saver iflags(is);
  572. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  573. if (strm_sentry) {
  574. try {
  575. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  576. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  577. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  578. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, lkd);
  579. }
  580. else {
  581. date_input_facet_local* f = new date_input_facet_local();
  582. std::locale l = std::locale(is.getloc(), f);
  583. is.imbue(l);
  584. f->get(sit, str_end, is, lkd);
  585. }
  586. }
  587. catch(...) {
  588. std::ios_base::iostate exception_mask = is.exceptions();
  589. if(std::ios_base::failbit & exception_mask) {
  590. try { is.setstate(std::ios_base::failbit); }
  591. catch(std::ios_base::failure&) {}
  592. throw; // rethrow original exception
  593. }
  594. else {
  595. is.setstate(std::ios_base::failbit);
  596. }
  597. }
  598. }
  599. return is;
  600. }
  601. template <class CharT, class TraitsT>
  602. inline std::basic_ostream<CharT, TraitsT>&
  603. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::first_day_of_the_week_after& fda) {
  604. boost::io::ios_flags_saver iflags(os);
  605. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  606. std::ostreambuf_iterator<CharT> output_itr(os);
  607. if (std::has_facet<custom_date_facet>(os.getloc())) {
  608. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), fda);
  609. }
  610. else {
  611. custom_date_facet* f = new custom_date_facet();
  612. std::locale l = std::locale(os.getloc(), f);
  613. os.imbue(l);
  614. f->put(output_itr, os, os.fill(), fda);
  615. }
  616. return os;
  617. }
  618. //! input operator for first_day_of_the_week_after
  619. template <class CharT, class Traits>
  620. inline
  621. std::basic_istream<CharT, Traits>&
  622. operator>>(std::basic_istream<CharT, Traits>& is,
  623. first_day_of_the_week_after& fka)
  624. {
  625. boost::io::ios_flags_saver iflags(is);
  626. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  627. if (strm_sentry) {
  628. try {
  629. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  630. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  631. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  632. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, fka);
  633. }
  634. else {
  635. date_input_facet_local* f = new date_input_facet_local();
  636. std::locale l = std::locale(is.getloc(), f);
  637. is.imbue(l);
  638. f->get(sit, str_end, is, fka);
  639. }
  640. }
  641. catch(...) {
  642. std::ios_base::iostate exception_mask = is.exceptions();
  643. if(std::ios_base::failbit & exception_mask) {
  644. try { is.setstate(std::ios_base::failbit); }
  645. catch(std::ios_base::failure&) {}
  646. throw; // rethrow original exception
  647. }
  648. else {
  649. is.setstate(std::ios_base::failbit);
  650. }
  651. }
  652. }
  653. return is;
  654. }
  655. template <class CharT, class TraitsT>
  656. inline std::basic_ostream<CharT, TraitsT>&
  657. operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::first_day_of_the_week_before& fdb) {
  658. boost::io::ios_flags_saver iflags(os);
  659. typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
  660. std::ostreambuf_iterator<CharT> output_itr(os);
  661. if (std::has_facet<custom_date_facet>(os.getloc())) {
  662. std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), fdb);
  663. }
  664. else {
  665. custom_date_facet* f = new custom_date_facet();
  666. std::locale l = std::locale(os.getloc(), f);
  667. os.imbue(l);
  668. f->put(output_itr, os, os.fill(), fdb);
  669. }
  670. return os;
  671. }
  672. //! input operator for first_day_of_the_week_before
  673. template <class CharT, class Traits>
  674. inline
  675. std::basic_istream<CharT, Traits>&
  676. operator>>(std::basic_istream<CharT, Traits>& is,
  677. first_day_of_the_week_before& fkb)
  678. {
  679. boost::io::ios_flags_saver iflags(is);
  680. typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
  681. if (strm_sentry) {
  682. try {
  683. typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
  684. std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
  685. if(std::has_facet<date_input_facet_local>(is.getloc())) {
  686. std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, fkb);
  687. }
  688. else {
  689. date_input_facet_local* f = new date_input_facet_local();
  690. std::locale l = std::locale(is.getloc(), f);
  691. is.imbue(l);
  692. f->get(sit, str_end, is, fkb);
  693. }
  694. }
  695. catch(...) {
  696. std::ios_base::iostate exception_mask = is.exceptions();
  697. if(std::ios_base::failbit & exception_mask) {
  698. try { is.setstate(std::ios_base::failbit); }
  699. catch(std::ios_base::failure&) {}
  700. throw; // rethrow original exception
  701. }
  702. else {
  703. is.setstate(std::ios_base::failbit);
  704. }
  705. }
  706. }
  707. return is;
  708. }
  709. } } // namespaces
  710. #endif // DATE_TIME_GREGORIAN_IO_HPP__