1 #ifndef DATE_TIME_DATE_FORMATTING_LOCALES_HPP___
2 #define DATE_TIME_DATE_FORMATTING_LOCALES_HPP___
4 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
5 * Use, modification and distribution is subject to the
6 * Boost Software License, Version 1.0. (See accompanying
7 * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
8 * Author: Jeff Garland, Bart Garst
13 #include "boost/date_time/locale_config.hpp" // set BOOST_DATE_TIME_NO_LOCALE
15 #ifndef BOOST_DATE_TIME_NO_LOCALE
17 #include "boost/date_time/iso_format.hpp"
18 #include "boost/date_time/date_names_put.hpp"
19 #include "boost/date_time/parse_format_base.hpp"
28 //! Formats a month as as string into an ostream
29 template<class facet_type,
31 class ostream_month_formatter
34 typedef typename facet_type::month_type month_type;
35 typedef std::basic_ostream<charT> ostream_type;
37 //! Formats a month as as string into an output iterator
38 static void format_month(const month_type& month,
43 switch (f.month_format())
45 case month_as_short_string:
47 std::ostreambuf_iterator<charT> oitr(os);
48 f.put_month_short(oitr, month.as_enum());
51 case month_as_long_string:
53 std::ostreambuf_iterator<charT> oitr(os);
54 f.put_month_long(oitr, month.as_enum());
57 case month_as_integer:
59 charT fill_char = '0';
60 os << std::setw(2) << std::setfill(fill_char) << month.as_number();
71 template<class weekday_type,
74 class ostream_weekday_formatter
77 typedef typename facet_type::month_type month_type;
78 typedef std::basic_ostream<charT> ostream_type;
80 //! Formats a month as as string into an output iterator
81 static void format_weekday(const weekday_type& wd,
87 std::ostreambuf_iterator<charT> oitr(os);
89 f.put_weekday_long(oitr, wd.as_enum());
92 f.put_weekday_short(oitr, wd.as_enum());
100 //! Convert ymd to a standard string formatting policies
101 template<class ymd_type,
104 class ostream_ymd_formatter
107 typedef typename ymd_type::month_type month_type;
108 typedef ostream_month_formatter<facet_type, charT> month_formatter_type;
109 typedef std::basic_ostream<charT> ostream_type;
110 typedef std::basic_string<charT> foo_type;
112 //! Convert ymd to a standard string formatting policies
113 /*! This is standard code for handling date formatting with
114 * year-month-day based date information. This function
115 * uses the format_type to control whether the string will
116 * contain separator characters, and if so what the character
117 * will be. In addtion, it can format the month as either
118 * an integer or a string as controled by the formatting
121 // static string_type ymd_to_string(ymd_type ymd)
123 // std::ostringstream ss;
125 // ymd_put(ymd, ss, dnp);
130 // Put ymd to ostream -- part of ostream refactor
131 static void ymd_put(ymd_type ymd,
135 std::ostreambuf_iterator<charT> oitr(os);
136 charT fill_char = '0';
137 switch (f.date_order()) {
138 case ymd_order_iso: {
140 if (f.has_date_sep_chars()) {
141 f.month_sep_char(oitr);
143 month_formatter_type::format_month(ymd.month, os, f);
144 if (f.has_date_sep_chars()) {
145 f.day_sep_char(oitr);
147 os << std::setw(2) << std::setfill(fill_char)
152 month_formatter_type::format_month(ymd.month, os, f);
153 if (f.has_date_sep_chars()) {
154 f.day_sep_char(oitr);
156 os << std::setw(2) << std::setfill(fill_char)
158 if (f.has_date_sep_chars()) {
159 f.month_sep_char(oitr);
164 case ymd_order_dmy: {
165 os << std::setw(2) << std::setfill(fill_char)
167 if (f.has_date_sep_chars()) {
168 f.day_sep_char(oitr);
170 month_formatter_type::format_month(ymd.month, os, f);
171 if (f.has_date_sep_chars()) {
172 f.month_sep_char(oitr);
182 //! Convert a date to string using format policies
183 template<class date_type,
186 class ostream_date_formatter
189 typedef std::basic_ostream<charT> ostream_type;
190 typedef typename date_type::ymd_type ymd_type;
192 //! Put date into an ostream
193 static void date_put(const date_type& d,
197 special_values sv = d.as_special();
198 if (sv == not_special) {
199 ymd_type ymd = d.year_month_day();
200 ostream_ymd_formatter<ymd_type, facet_type, charT>::ymd_put(ymd, os, f);
202 else { // output a special value
203 std::ostreambuf_iterator<charT> coi(os);
204 f.put_special_value(coi, sv);
209 //! Put date into an ostream
210 static void date_put(const date_type& d,
213 //retrieve the local from the ostream
214 std::locale locale = os.getloc();
215 if (std::has_facet<facet_type>(locale)) {
216 const facet_type& f = std::use_facet<facet_type>(locale);
220 //default to something sensible if no facet installed
221 facet_type default_facet;
222 date_put(d, os, default_facet);
225 }; //class date_formatter
228 } } //namespace date_time