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

source code of boost/libs/date_time/include/boost/date_time/date_formatting_locales.hpp