date_formatting_locales.hpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #ifndef DATE_TIME_DATE_FORMATTING_LOCALES_HPP___
  2. #define DATE_TIME_DATE_FORMATTING_LOCALES_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$
  9. */
  10. #include "boost/date_time/locale_config.hpp" // set BOOST_DATE_TIME_NO_LOCALE
  11. #ifndef BOOST_DATE_TIME_NO_LOCALE
  12. #include "boost/date_time/iso_format.hpp"
  13. #include "boost/date_time/date_names_put.hpp"
  14. #include "boost/date_time/parse_format_base.hpp"
  15. #include <boost/io/ios_state.hpp>
  16. //#include <string>
  17. #include <sstream>
  18. #include <iomanip>
  19. namespace boost {
  20. namespace date_time {
  21. //! Formats a month as as string into an ostream
  22. template<class facet_type,
  23. class charT = char>
  24. class ostream_month_formatter
  25. {
  26. public:
  27. typedef typename facet_type::month_type month_type;
  28. typedef std::basic_ostream<charT> ostream_type;
  29. //! Formats a month as as string into an output iterator
  30. static void format_month(const month_type& month,
  31. ostream_type& os,
  32. const facet_type& f)
  33. {
  34. switch (f.month_format())
  35. {
  36. case month_as_short_string:
  37. {
  38. std::ostreambuf_iterator<charT> oitr(os);
  39. f.put_month_short(oitr, month.as_enum());
  40. break;
  41. }
  42. case month_as_long_string:
  43. {
  44. std::ostreambuf_iterator<charT> oitr(os);
  45. f.put_month_long(oitr, month.as_enum());
  46. break;
  47. }
  48. case month_as_integer:
  49. {
  50. boost::io::basic_ios_fill_saver<charT> ifs(os);
  51. os << std::setw(2) << std::setfill(os.widen('0')) << month.as_number();
  52. break;
  53. }
  54. }
  55. } // format_month
  56. };
  57. //! Formats a weekday
  58. template<class weekday_type,
  59. class facet_type,
  60. class charT = char>
  61. class ostream_weekday_formatter
  62. {
  63. public:
  64. typedef typename facet_type::month_type month_type;
  65. typedef std::basic_ostream<charT> ostream_type;
  66. //! Formats a month as as string into an output iterator
  67. static void format_weekday(const weekday_type& wd,
  68. ostream_type& os,
  69. const facet_type& f,
  70. bool as_long_string)
  71. {
  72. std::ostreambuf_iterator<charT> oitr(os);
  73. if (as_long_string) {
  74. f.put_weekday_long(oitr, wd.as_enum());
  75. }
  76. else {
  77. f.put_weekday_short(oitr, wd.as_enum());
  78. }
  79. } // format_weekday
  80. };
  81. //! Convert ymd to a standard string formatting policies
  82. template<class ymd_type,
  83. class facet_type,
  84. class charT = char>
  85. class ostream_ymd_formatter
  86. {
  87. public:
  88. typedef typename ymd_type::month_type month_type;
  89. typedef ostream_month_formatter<facet_type, charT> month_formatter_type;
  90. typedef std::basic_ostream<charT> ostream_type;
  91. typedef std::basic_string<charT> foo_type;
  92. //! Convert ymd to a standard string formatting policies
  93. /*! This is standard code for handling date formatting with
  94. * year-month-day based date information. This function
  95. * uses the format_type to control whether the string will
  96. * contain separator characters, and if so what the character
  97. * will be. In addtion, it can format the month as either
  98. * an integer or a string as controled by the formatting
  99. * policy
  100. */
  101. // static string_type ymd_to_string(ymd_type ymd)
  102. // {
  103. // std::ostringstream ss;
  104. // facet_type dnp;
  105. // ymd_put(ymd, ss, dnp);
  106. // return ss.str();
  107. // }
  108. // Put ymd to ostream -- part of ostream refactor
  109. static void ymd_put(ymd_type ymd,
  110. ostream_type& os,
  111. const facet_type& f)
  112. {
  113. boost::io::basic_ios_fill_saver<charT> ifs(os);
  114. std::ostreambuf_iterator<charT> oitr(os);
  115. switch (f.date_order()) {
  116. case ymd_order_iso: {
  117. os << ymd.year;
  118. if (f.has_date_sep_chars()) {
  119. f.month_sep_char(oitr);
  120. }
  121. month_formatter_type::format_month(ymd.month, os, f);
  122. if (f.has_date_sep_chars()) {
  123. f.day_sep_char(oitr);
  124. }
  125. os << std::setw(2) << std::setfill(os.widen('0'))
  126. << ymd.day;
  127. break;
  128. }
  129. case ymd_order_us: {
  130. month_formatter_type::format_month(ymd.month, os, f);
  131. if (f.has_date_sep_chars()) {
  132. f.day_sep_char(oitr);
  133. }
  134. os << std::setw(2) << std::setfill(os.widen('0'))
  135. << ymd.day;
  136. if (f.has_date_sep_chars()) {
  137. f.month_sep_char(oitr);
  138. }
  139. os << ymd.year;
  140. break;
  141. }
  142. case ymd_order_dmy: {
  143. os << std::setw(2) << std::setfill(os.widen('0'))
  144. << ymd.day;
  145. if (f.has_date_sep_chars()) {
  146. f.day_sep_char(oitr);
  147. }
  148. month_formatter_type::format_month(ymd.month, os, f);
  149. if (f.has_date_sep_chars()) {
  150. f.month_sep_char(oitr);
  151. }
  152. os << ymd.year;
  153. break;
  154. }
  155. }
  156. }
  157. };
  158. //! Convert a date to string using format policies
  159. template<class date_type,
  160. class facet_type,
  161. class charT = char>
  162. class ostream_date_formatter
  163. {
  164. public:
  165. typedef std::basic_ostream<charT> ostream_type;
  166. typedef typename date_type::ymd_type ymd_type;
  167. //! Put date into an ostream
  168. static void date_put(const date_type& d,
  169. ostream_type& os,
  170. const facet_type& f)
  171. {
  172. special_values sv = d.as_special();
  173. if (sv == not_special) {
  174. ymd_type ymd = d.year_month_day();
  175. ostream_ymd_formatter<ymd_type, facet_type, charT>::ymd_put(ymd, os, f);
  176. }
  177. else { // output a special value
  178. std::ostreambuf_iterator<charT> coi(os);
  179. f.put_special_value(coi, sv);
  180. }
  181. }
  182. //! Put date into an ostream
  183. static void date_put(const date_type& d,
  184. ostream_type& os)
  185. {
  186. //retrieve the local from the ostream
  187. std::locale locale = os.getloc();
  188. if (std::has_facet<facet_type>(locale)) {
  189. const facet_type& f = std::use_facet<facet_type>(locale);
  190. date_put(d, os, f);
  191. }
  192. else {
  193. //default to something sensible if no facet installed
  194. facet_type default_facet;
  195. date_put(d, os, default_facet);
  196. }
  197. } // date_to_ostream
  198. }; //class date_formatter
  199. } } //namespace date_time
  200. #endif
  201. #endif