date_formatting_locales.hpp 6.5 KB

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