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