Chris@16
|
1 #ifndef DATE_TIME_DATE_NAMES_PUT_HPP___
|
Chris@16
|
2 #define DATE_TIME_DATE_NAMES_PUT_HPP___
|
Chris@16
|
3
|
Chris@16
|
4 /* Copyright (c) 2002-2005 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/special_defs.hpp"
|
Chris@16
|
18 #include "boost/date_time/date_defs.hpp"
|
Chris@16
|
19 #include "boost/date_time/parse_format_base.hpp"
|
Chris@16
|
20 #include "boost/lexical_cast.hpp"
|
Chris@16
|
21 #include <locale>
|
Chris@16
|
22
|
Chris@16
|
23
|
Chris@16
|
24 namespace boost {
|
Chris@16
|
25 namespace date_time {
|
Chris@16
|
26
|
Chris@16
|
27 //! Output facet base class for gregorian dates.
|
Chris@16
|
28 /*! This class is a base class for date facets used to localize the
|
Chris@16
|
29 * names of months and the names of days in the week.
|
Chris@16
|
30 *
|
Chris@16
|
31 * Requirements of Config
|
Chris@16
|
32 * - define an enumeration month_enum that enumerates the months.
|
Chris@16
|
33 * The enumeration should be '1' based eg: Jan==1
|
Chris@16
|
34 * - define as_short_string and as_long_string
|
Chris@16
|
35 *
|
Chris@16
|
36 * (see langer & kreft p334).
|
Chris@16
|
37 *
|
Chris@16
|
38 */
|
Chris@16
|
39 template<class Config,
|
Chris@16
|
40 class charT = char,
|
Chris@16
|
41 class OutputIterator = std::ostreambuf_iterator<charT> >
|
Chris@16
|
42 class date_names_put : public std::locale::facet
|
Chris@16
|
43 {
|
Chris@16
|
44 public:
|
Chris@16
|
45 date_names_put() {}
|
Chris@16
|
46 typedef OutputIterator iter_type;
|
Chris@16
|
47 typedef typename Config::month_type month_type;
|
Chris@16
|
48 typedef typename Config::month_enum month_enum;
|
Chris@16
|
49 typedef typename Config::weekday_enum weekday_enum;
|
Chris@16
|
50 typedef typename Config::special_value_enum special_value_enum;
|
Chris@16
|
51 //typedef typename Config::format_type format_type;
|
Chris@16
|
52 typedef std::basic_string<charT> string_type;
|
Chris@16
|
53 typedef charT char_type;
|
Chris@16
|
54 static const char_type default_special_value_names[3][17];
|
Chris@16
|
55 static const char_type separator[2];
|
Chris@16
|
56
|
Chris@16
|
57 static std::locale::id id;
|
Chris@16
|
58
|
Chris@16
|
59 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
|
Chris@16
|
60 std::locale::id& __get_id (void) const { return id; }
|
Chris@16
|
61 #endif
|
Chris@16
|
62
|
Chris@16
|
63 void put_special_value(iter_type& oitr, special_value_enum sv) const
|
Chris@16
|
64 {
|
Chris@16
|
65 do_put_special_value(oitr, sv);
|
Chris@16
|
66 }
|
Chris@16
|
67 void put_month_short(iter_type& oitr, month_enum moy) const
|
Chris@16
|
68 {
|
Chris@16
|
69 do_put_month_short(oitr, moy);
|
Chris@16
|
70 }
|
Chris@16
|
71 void put_month_long(iter_type& oitr, month_enum moy) const
|
Chris@16
|
72 {
|
Chris@16
|
73 do_put_month_long(oitr, moy);
|
Chris@16
|
74 }
|
Chris@16
|
75 void put_weekday_short(iter_type& oitr, weekday_enum wd) const
|
Chris@16
|
76 {
|
Chris@16
|
77 do_put_weekday_short(oitr, wd);
|
Chris@16
|
78 }
|
Chris@16
|
79 void put_weekday_long(iter_type& oitr, weekday_enum wd) const
|
Chris@16
|
80 {
|
Chris@16
|
81 do_put_weekday_long(oitr, wd);
|
Chris@16
|
82 }
|
Chris@16
|
83 bool has_date_sep_chars() const
|
Chris@16
|
84 {
|
Chris@16
|
85 return do_has_date_sep_chars();
|
Chris@16
|
86 }
|
Chris@16
|
87 void year_sep_char(iter_type& oitr) const
|
Chris@16
|
88 {
|
Chris@16
|
89 do_year_sep_char(oitr);
|
Chris@16
|
90 }
|
Chris@16
|
91 //! char between year-month
|
Chris@16
|
92 void month_sep_char(iter_type& oitr) const
|
Chris@16
|
93 {
|
Chris@16
|
94 do_month_sep_char(oitr);
|
Chris@16
|
95 }
|
Chris@16
|
96 //! Char to separate month-day
|
Chris@16
|
97 void day_sep_char(iter_type& oitr) const
|
Chris@16
|
98 {
|
Chris@16
|
99 do_day_sep_char(oitr);
|
Chris@16
|
100 }
|
Chris@16
|
101 //! Determines the order to put the date elements
|
Chris@16
|
102 ymd_order_spec date_order() const
|
Chris@16
|
103 {
|
Chris@16
|
104 return do_date_order();
|
Chris@16
|
105 }
|
Chris@16
|
106 //! Determines if month is displayed as integer, short or long string
|
Chris@16
|
107 month_format_spec month_format() const
|
Chris@16
|
108 {
|
Chris@16
|
109 return do_month_format();
|
Chris@16
|
110 }
|
Chris@16
|
111
|
Chris@16
|
112 protected:
|
Chris@16
|
113 //! Default facet implementation uses month_type defaults
|
Chris@16
|
114 virtual void do_put_month_short(iter_type& oitr, month_enum moy) const
|
Chris@16
|
115 {
|
Chris@16
|
116 month_type gm(moy);
|
Chris@16
|
117 charT c = '\0';
|
Chris@16
|
118 put_string(oitr, gm.as_short_string(c));
|
Chris@16
|
119 }
|
Chris@16
|
120 //! Default facet implementation uses month_type defaults
|
Chris@16
|
121 virtual void do_put_month_long(iter_type& oitr,
|
Chris@16
|
122 month_enum moy) const
|
Chris@16
|
123 {
|
Chris@16
|
124 month_type gm(moy);
|
Chris@16
|
125 charT c = '\0';
|
Chris@16
|
126 put_string(oitr, gm.as_long_string(c));
|
Chris@16
|
127 }
|
Chris@16
|
128 //! Default facet implementation for special value types
|
Chris@16
|
129 virtual void do_put_special_value(iter_type& oitr, special_value_enum sv) const
|
Chris@16
|
130 {
|
Chris@16
|
131 if(sv <= 2) { // only output not_a_date_time, neg_infin, or pos_infin
|
Chris@16
|
132 string_type s(default_special_value_names[sv]);
|
Chris@16
|
133 put_string(oitr, s);
|
Chris@16
|
134 }
|
Chris@16
|
135 }
|
Chris@16
|
136 virtual void do_put_weekday_short(iter_type&, weekday_enum) const
|
Chris@16
|
137 {
|
Chris@16
|
138 }
|
Chris@16
|
139 virtual void do_put_weekday_long(iter_type&, weekday_enum) const
|
Chris@16
|
140 {
|
Chris@16
|
141 }
|
Chris@16
|
142 virtual bool do_has_date_sep_chars() const
|
Chris@16
|
143 {
|
Chris@16
|
144 return true;
|
Chris@16
|
145 }
|
Chris@16
|
146 virtual void do_year_sep_char(iter_type& oitr) const
|
Chris@16
|
147 {
|
Chris@16
|
148 string_type s(separator);
|
Chris@16
|
149 put_string(oitr, s);
|
Chris@16
|
150 }
|
Chris@16
|
151 //! char between year-month
|
Chris@16
|
152 virtual void do_month_sep_char(iter_type& oitr) const
|
Chris@16
|
153 {
|
Chris@16
|
154 string_type s(separator);
|
Chris@16
|
155 put_string(oitr, s);
|
Chris@16
|
156 }
|
Chris@16
|
157 //! Char to separate month-day
|
Chris@16
|
158 virtual void do_day_sep_char(iter_type& oitr) const
|
Chris@16
|
159 {
|
Chris@16
|
160 string_type s(separator); //put in '-'
|
Chris@16
|
161 put_string(oitr, s);
|
Chris@16
|
162 }
|
Chris@16
|
163 //! Default for date order
|
Chris@16
|
164 virtual ymd_order_spec do_date_order() const
|
Chris@16
|
165 {
|
Chris@16
|
166 return ymd_order_iso;
|
Chris@16
|
167 }
|
Chris@16
|
168 //! Default month format
|
Chris@16
|
169 virtual month_format_spec do_month_format() const
|
Chris@16
|
170 {
|
Chris@16
|
171 return month_as_short_string;
|
Chris@16
|
172 }
|
Chris@16
|
173 void put_string(iter_type& oi, const charT* const s) const
|
Chris@16
|
174 {
|
Chris@16
|
175 string_type s1(boost::lexical_cast<string_type>(s));
|
Chris@16
|
176 typename string_type::iterator si,end;
|
Chris@16
|
177 for (si=s1.begin(), end=s1.end(); si!=end; si++, oi++) {
|
Chris@16
|
178 *oi = *si;
|
Chris@16
|
179 }
|
Chris@16
|
180 }
|
Chris@16
|
181 void put_string(iter_type& oi, const string_type& s1) const
|
Chris@16
|
182 {
|
Chris@16
|
183 typename string_type::const_iterator si,end;
|
Chris@16
|
184 for (si=s1.begin(), end=s1.end(); si!=end; si++, oi++) {
|
Chris@16
|
185 *oi = *si;
|
Chris@16
|
186 }
|
Chris@16
|
187 }
|
Chris@16
|
188 };
|
Chris@16
|
189
|
Chris@16
|
190 template<class Config, class charT, class OutputIterator>
|
Chris@16
|
191 const typename date_names_put<Config, charT, OutputIterator>::char_type
|
Chris@16
|
192 date_names_put<Config, charT, OutputIterator>::default_special_value_names[3][17] = {
|
Chris@16
|
193 {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'},
|
Chris@16
|
194 {'-','i','n','f','i','n','i','t','y'},
|
Chris@16
|
195 {'+','i','n','f','i','n','i','t','y'} };
|
Chris@16
|
196
|
Chris@16
|
197 template<class Config, class charT, class OutputIterator>
|
Chris@16
|
198 const typename date_names_put<Config, charT, OutputIterator>::char_type
|
Chris@16
|
199 date_names_put<Config, charT, OutputIterator>::separator[2] =
|
Chris@16
|
200 {'-', '\0'} ;
|
Chris@16
|
201
|
Chris@16
|
202
|
Chris@16
|
203 //! Generate storage location for a std::locale::id
|
Chris@16
|
204 template<class Config, class charT, class OutputIterator>
|
Chris@16
|
205 std::locale::id date_names_put<Config, charT, OutputIterator>::id;
|
Chris@16
|
206
|
Chris@16
|
207 //! A date name output facet that takes an array of char* to define strings
|
Chris@16
|
208 template<class Config,
|
Chris@16
|
209 class charT = char,
|
Chris@16
|
210 class OutputIterator = std::ostreambuf_iterator<charT> >
|
Chris@16
|
211 class all_date_names_put : public date_names_put<Config, charT, OutputIterator>
|
Chris@16
|
212 {
|
Chris@16
|
213 public:
|
Chris@16
|
214 all_date_names_put(const charT* const month_short_names[],
|
Chris@16
|
215 const charT* const month_long_names[],
|
Chris@16
|
216 const charT* const special_value_names[],
|
Chris@16
|
217 const charT* const weekday_short_names[],
|
Chris@16
|
218 const charT* const weekday_long_names[],
|
Chris@16
|
219 charT separator_char = '-',
|
Chris@16
|
220 ymd_order_spec order_spec = ymd_order_iso,
|
Chris@16
|
221 month_format_spec month_format = month_as_short_string) :
|
Chris@16
|
222 month_short_names_(month_short_names),
|
Chris@16
|
223 month_long_names_(month_long_names),
|
Chris@16
|
224 special_value_names_(special_value_names),
|
Chris@16
|
225 weekday_short_names_(weekday_short_names),
|
Chris@16
|
226 weekday_long_names_(weekday_long_names),
|
Chris@16
|
227 order_spec_(order_spec),
|
Chris@16
|
228 month_format_spec_(month_format)
|
Chris@16
|
229 {
|
Chris@16
|
230 separator_char_[0] = separator_char;
|
Chris@16
|
231 separator_char_[1] = '\0';
|
Chris@16
|
232
|
Chris@16
|
233 }
|
Chris@16
|
234 typedef OutputIterator iter_type;
|
Chris@16
|
235 typedef typename Config::month_enum month_enum;
|
Chris@16
|
236 typedef typename Config::weekday_enum weekday_enum;
|
Chris@16
|
237 typedef typename Config::special_value_enum special_value_enum;
|
Chris@16
|
238
|
Chris@16
|
239 const charT* const* get_short_month_names() const
|
Chris@16
|
240 {
|
Chris@16
|
241 return month_short_names_;
|
Chris@16
|
242 }
|
Chris@16
|
243 const charT* const* get_long_month_names() const
|
Chris@16
|
244 {
|
Chris@16
|
245 return month_long_names_;
|
Chris@16
|
246 }
|
Chris@16
|
247 const charT* const* get_special_value_names() const
|
Chris@16
|
248 {
|
Chris@16
|
249 return special_value_names_;
|
Chris@16
|
250 }
|
Chris@16
|
251 const charT* const* get_short_weekday_names()const
|
Chris@16
|
252 {
|
Chris@16
|
253 return weekday_short_names_;
|
Chris@16
|
254 }
|
Chris@16
|
255 const charT* const* get_long_weekday_names()const
|
Chris@16
|
256 {
|
Chris@16
|
257 return weekday_long_names_;
|
Chris@16
|
258 }
|
Chris@16
|
259
|
Chris@16
|
260 protected:
|
Chris@16
|
261 //! Generic facet that takes array of chars
|
Chris@16
|
262 virtual void do_put_month_short(iter_type& oitr, month_enum moy) const
|
Chris@16
|
263 {
|
Chris@16
|
264 this->put_string(oitr, month_short_names_[moy-1]);
|
Chris@16
|
265 }
|
Chris@16
|
266 //! Long month names
|
Chris@16
|
267 virtual void do_put_month_long(iter_type& oitr, month_enum moy) const
|
Chris@16
|
268 {
|
Chris@16
|
269 this->put_string(oitr, month_long_names_[moy-1]);
|
Chris@16
|
270 }
|
Chris@16
|
271 //! Special values names
|
Chris@16
|
272 virtual void do_put_special_value(iter_type& oitr, special_value_enum sv) const
|
Chris@16
|
273 {
|
Chris@16
|
274 this->put_string(oitr, special_value_names_[sv]);
|
Chris@16
|
275 }
|
Chris@16
|
276 virtual void do_put_weekday_short(iter_type& oitr, weekday_enum wd) const
|
Chris@16
|
277 {
|
Chris@16
|
278 this->put_string(oitr, weekday_short_names_[wd]);
|
Chris@16
|
279 }
|
Chris@16
|
280 virtual void do_put_weekday_long(iter_type& oitr, weekday_enum wd) const
|
Chris@16
|
281 {
|
Chris@16
|
282 this->put_string(oitr, weekday_long_names_[wd]);
|
Chris@16
|
283 }
|
Chris@16
|
284 //! char between year-month
|
Chris@16
|
285 virtual void do_month_sep_char(iter_type& oitr) const
|
Chris@16
|
286 {
|
Chris@16
|
287 this->put_string(oitr, separator_char_);
|
Chris@16
|
288 }
|
Chris@16
|
289 //! Char to separate month-day
|
Chris@16
|
290 virtual void do_day_sep_char(iter_type& oitr) const
|
Chris@16
|
291 {
|
Chris@16
|
292 this->put_string(oitr, separator_char_);
|
Chris@16
|
293 }
|
Chris@16
|
294 //! Set the date ordering
|
Chris@16
|
295 virtual ymd_order_spec do_date_order() const
|
Chris@16
|
296 {
|
Chris@16
|
297 return order_spec_;
|
Chris@16
|
298 }
|
Chris@16
|
299 //! Set the date ordering
|
Chris@16
|
300 virtual month_format_spec do_month_format() const
|
Chris@16
|
301 {
|
Chris@16
|
302 return month_format_spec_;
|
Chris@16
|
303 }
|
Chris@16
|
304
|
Chris@16
|
305 private:
|
Chris@16
|
306 const charT* const* month_short_names_;
|
Chris@16
|
307 const charT* const* month_long_names_;
|
Chris@16
|
308 const charT* const* special_value_names_;
|
Chris@16
|
309 const charT* const* weekday_short_names_;
|
Chris@16
|
310 const charT* const* weekday_long_names_;
|
Chris@16
|
311 charT separator_char_[2];
|
Chris@16
|
312 ymd_order_spec order_spec_;
|
Chris@16
|
313 month_format_spec month_format_spec_;
|
Chris@16
|
314 };
|
Chris@16
|
315
|
Chris@16
|
316 } } //namespace boost::date_time
|
Chris@16
|
317
|
Chris@16
|
318 #endif //BOOST_NO_STD_LOCALE
|
Chris@16
|
319
|
Chris@16
|
320 #endif
|