greg_facet.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #ifndef GREGORIAN_FACET_HPP___
  2. #define GREGORIAN_FACET_HPP___
  3. /* Copyright (c) 2002,2003 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: 2008-11-23 06:13:35 -0500 (Sun, 23 Nov 2008) $
  9. */
  10. #include "boost/date_time/gregorian/gregorian_types.hpp"
  11. #include "boost/date_time/date_formatting_locales.hpp" // sets BOOST_DATE_TIME_NO_LOCALE
  12. #include "boost/date_time/gregorian/parsers.hpp"
  13. //This file is basically commented out if locales are not supported
  14. #ifndef BOOST_DATE_TIME_NO_LOCALE
  15. #include <string>
  16. #include <memory>
  17. #include <locale>
  18. #include <iostream>
  19. #include <exception>
  20. namespace boost {
  21. namespace gregorian {
  22. //! Configuration of the output facet template
  23. struct greg_facet_config
  24. {
  25. typedef boost::gregorian::greg_month month_type;
  26. typedef boost::date_time::special_values special_value_enum;
  27. typedef boost::gregorian::months_of_year month_enum;
  28. typedef boost::date_time::weekdays weekday_enum;
  29. };
  30. #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
  31. //! Create the base facet type for gregorian::date
  32. typedef boost::date_time::date_names_put<greg_facet_config> greg_base_facet;
  33. //! ostream operator for gregorian::date
  34. /*! Uses the date facet to determine various output parameters including:
  35. * - string values for the month (eg: Jan, Feb, Mar) (default: English)
  36. * - string values for special values (eg: not-a-date-time) (default: English)
  37. * - selection of long, short strings, or numerical month representation (default: short string)
  38. * - month day year order (default yyyy-mmm-dd)
  39. */
  40. template <class charT, class traits>
  41. inline
  42. std::basic_ostream<charT, traits>&
  43. operator<<(std::basic_ostream<charT, traits>& os, const date& d)
  44. {
  45. typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
  46. typedef boost::date_time::ostream_date_formatter<date, facet_def, charT> greg_ostream_formatter;
  47. greg_ostream_formatter::date_put(d, os);
  48. return os;
  49. }
  50. //! operator<< for gregorian::greg_month typically streaming: Jan, Feb, Mar...
  51. /*! Uses the date facet to determine output string as well as selection of long or short strings.
  52. * Default if no facet is installed is to output a 2 wide numeric value for the month
  53. * eg: 01 == Jan, 02 == Feb, ... 12 == Dec.
  54. */
  55. template <class charT, class traits>
  56. inline
  57. std::basic_ostream<charT, traits>&
  58. operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
  59. {
  60. typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
  61. typedef boost::date_time::ostream_month_formatter<facet_def, charT> greg_month_formatter;
  62. std::locale locale = os.getloc();
  63. if (std::has_facet<facet_def>(locale)) {
  64. const facet_def& f = std::use_facet<facet_def>(locale);
  65. greg_month_formatter::format_month(m, os, f);
  66. }
  67. else { //default to numeric
  68. charT fill_char = '0';
  69. os << std::setw(2) << std::setfill(fill_char) << m.as_number();
  70. }
  71. return os;
  72. }
  73. //! operator<< for gregorian::greg_weekday typically streaming: Sun, Mon, Tue, ...
  74. /*! Uses the date facet to determine output string as well as selection of long or short string.
  75. * Default if no facet is installed is to output a 3 char english string for the
  76. * day of the week.
  77. */
  78. template <class charT, class traits>
  79. inline
  80. std::basic_ostream<charT, traits>&
  81. operator<<(std::basic_ostream<charT, traits>& os, const greg_weekday& wd)
  82. {
  83. typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
  84. typedef boost::date_time::ostream_weekday_formatter<greg_weekday, facet_def, charT> greg_weekday_formatter;
  85. std::locale locale = os.getloc();
  86. if (std::has_facet<facet_def>(locale)) {
  87. const facet_def& f = std::use_facet<facet_def>(locale);
  88. greg_weekday_formatter::format_weekday(wd.as_enum(), os, f, true);
  89. }
  90. else { //default to short English string eg: Sun, Mon, Tue, Wed...
  91. os << wd.as_short_string();
  92. }
  93. return os;
  94. }
  95. //! operator<< for gregorian::date_period typical output: [2002-Jan-01/2002-Jan-31]
  96. /*! Uses the date facet to determine output string as well as selection of long
  97. * or short string fr dates.
  98. * Default if no facet is installed is to output a 3 char english string for the
  99. * day of the week.
  100. */
  101. template <class charT, class traits>
  102. inline
  103. std::basic_ostream<charT, traits>&
  104. operator<<(std::basic_ostream<charT, traits>& os, const date_period& dp)
  105. {
  106. os << '['; //TODO: facet or manipulator for periods?
  107. os << dp.begin();
  108. os << '/'; //TODO: facet or manipulator for periods?
  109. os << dp.last();
  110. os << ']';
  111. return os;
  112. }
  113. template <class charT, class traits>
  114. inline
  115. std::basic_ostream<charT, traits>&
  116. operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
  117. {
  118. //os << dd.days();
  119. os << dd.get_rep();
  120. return os;
  121. }
  122. //! operator<< for gregorian::partial_date. Output: "Jan 1"
  123. template <class charT, class traits>
  124. inline
  125. std::basic_ostream<charT, traits>&
  126. operator<<(std::basic_ostream<charT, traits>& os, const partial_date& pd)
  127. {
  128. os << std::setw(2) << std::setfill('0') << pd.day() << ' '
  129. << pd.month().as_short_string() ;
  130. return os;
  131. }
  132. //! operator<< for gregorian::nth_kday_of_month. Output: "first Mon of Jun"
  133. template <class charT, class traits>
  134. inline
  135. std::basic_ostream<charT, traits>&
  136. operator<<(std::basic_ostream<charT, traits>& os,
  137. const nth_kday_of_month& nkd)
  138. {
  139. os << nkd.nth_week_as_str() << ' '
  140. << nkd.day_of_week() << " of "
  141. << nkd.month().as_short_string() ;
  142. return os;
  143. }
  144. //! operator<< for gregorian::first_kday_of_month. Output: "first Mon of Jun"
  145. template <class charT, class traits>
  146. inline
  147. std::basic_ostream<charT, traits>&
  148. operator<<(std::basic_ostream<charT, traits>& os,
  149. const first_kday_of_month& fkd)
  150. {
  151. os << "first " << fkd.day_of_week() << " of "
  152. << fkd.month().as_short_string() ;
  153. return os;
  154. }
  155. //! operator<< for gregorian::last_kday_of_month. Output: "last Mon of Jun"
  156. template <class charT, class traits>
  157. inline
  158. std::basic_ostream<charT, traits>&
  159. operator<<(std::basic_ostream<charT, traits>& os,
  160. const last_kday_of_month& lkd)
  161. {
  162. os << "last " << lkd.day_of_week() << " of "
  163. << lkd.month().as_short_string() ;
  164. return os;
  165. }
  166. //! operator<< for gregorian::first_kday_after. Output: "first Mon after"
  167. template <class charT, class traits>
  168. inline
  169. std::basic_ostream<charT, traits>&
  170. operator<<(std::basic_ostream<charT, traits>& os,
  171. const first_kday_after& fka)
  172. {
  173. os << fka.day_of_week() << " after";
  174. return os;
  175. }
  176. //! operator<< for gregorian::first_kday_before. Output: "first Mon before"
  177. template <class charT, class traits>
  178. inline
  179. std::basic_ostream<charT, traits>&
  180. operator<<(std::basic_ostream<charT, traits>& os,
  181. const first_kday_before& fkb)
  182. {
  183. os << fkb.day_of_week() << " before";
  184. return os;
  185. }
  186. #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
  187. /**************** Input Streaming ******************/
  188. #if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
  189. //! operator>> for gregorian::date
  190. template<class charT>
  191. inline
  192. std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, date& d)
  193. {
  194. std::istream_iterator<std::basic_string<charT>, charT> beg(is), eos;
  195. typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
  196. d = from_stream(beg, eos);
  197. return is;
  198. }
  199. #endif // BOOST_NO_STD_ITERATOR_TRAITS
  200. //! operator>> for gregorian::date_duration
  201. template<class charT>
  202. inline
  203. std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
  204. date_duration& dd)
  205. {
  206. long v;
  207. is >> v;
  208. dd = date_duration(v);
  209. return is;
  210. }
  211. //! operator>> for gregorian::date_period
  212. template<class charT>
  213. inline
  214. std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
  215. date_period& dp)
  216. {
  217. std::basic_string<charT> s;
  218. is >> s;
  219. dp = date_time::from_simple_string_type<date>(s);
  220. return is;
  221. }
  222. //! generates a locale with the set of gregorian name-strings of type char*
  223. BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, char type);
  224. //! Returns a pointer to a facet with a default set of names (English)
  225. /* Necessary in the event an exception is thrown from op>> for
  226. * weekday or month. See comments in those functions for more info */
  227. BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, char>* create_facet_def(char type);
  228. #ifndef BOOST_NO_STD_WSTRING
  229. //! generates a locale with the set of gregorian name-strings of type wchar_t*
  230. BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, wchar_t type);
  231. //! Returns a pointer to a facet with a default set of names (English)
  232. /* Necessary in the event an exception is thrown from op>> for
  233. * weekday or month. See comments in those functions for more info */
  234. BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, wchar_t>* create_facet_def(wchar_t type);
  235. #endif // BOOST_NO_STD_WSTRING
  236. //! operator>> for gregorian::greg_month - throws exception if invalid month given
  237. template<class charT>
  238. inline
  239. std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m)
  240. {
  241. typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
  242. std::basic_string<charT> s;
  243. is >> s;
  244. if(!std::has_facet<facet_def>(is.getloc())) {
  245. std::locale loc = is.getloc();
  246. charT a = '\0';
  247. is.imbue(generate_locale(loc, a));
  248. }
  249. short num = 0;
  250. try{
  251. const facet_def& f = std::use_facet<facet_def>(is.getloc());
  252. num = date_time::find_match(f.get_short_month_names(),
  253. f.get_long_month_names(),
  254. (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
  255. // which is needed by find_match
  256. }
  257. /* bad_cast will be thrown if the desired facet is not accessible
  258. * so we can generate the facet. This has the drawback of using english
  259. * names as a default. */
  260. catch(std::bad_cast&){
  261. charT a = '\0';
  262. std::auto_ptr< const facet_def > f(create_facet_def(a));
  263. num = date_time::find_match(f->get_short_month_names(),
  264. f->get_long_month_names(),
  265. (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
  266. // which is needed by find_match
  267. }
  268. ++num; // months numbered 1-12
  269. m = greg_month(num);
  270. return is;
  271. }
  272. //! operator>> for gregorian::greg_weekday - throws exception if invalid weekday given
  273. template<class charT>
  274. inline
  275. std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd)
  276. {
  277. typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
  278. std::basic_string<charT> s;
  279. is >> s;
  280. if(!std::has_facet<facet_def>(is.getloc())) {
  281. std::locale loc = is.getloc();
  282. charT a = '\0';
  283. is.imbue(generate_locale(loc, a));
  284. }
  285. short num = 0;
  286. try{
  287. const facet_def& f = std::use_facet<facet_def>(is.getloc());
  288. num = date_time::find_match(f.get_short_weekday_names(),
  289. f.get_long_weekday_names(),
  290. (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
  291. // to form the array size which is needed by find_match
  292. }
  293. /* bad_cast will be thrown if the desired facet is not accessible
  294. * so we can generate the facet. This has the drawback of using english
  295. * names as a default. */
  296. catch(std::bad_cast&){
  297. charT a = '\0';
  298. std::auto_ptr< const facet_def > f(create_facet_def(a));
  299. num = date_time::find_match(f->get_short_weekday_names(),
  300. f->get_long_weekday_names(),
  301. (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
  302. // to form the array size which is needed by find_match
  303. }
  304. wd = greg_weekday(num); // weekdays numbered 0-6
  305. return is;
  306. }
  307. } } //namespace gregorian
  308. #endif
  309. #endif