fix doc example typo
[boost.git] / boost / date_time / date_names_put.hpp
blobd6215bc4b6ca11b3b58be7d2538bad9027e2cfd3
1 #ifndef DATE_TIME_DATE_NAMES_PUT_HPP___
2 #define DATE_TIME_DATE_NAMES_PUT_HPP___
4 /* Copyright (c) 2002-2005 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$
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/special_defs.hpp"
18 #include "boost/date_time/date_defs.hpp"
19 #include "boost/date_time/parse_format_base.hpp"
20 #include "boost/lexical_cast.hpp"
21 #include <locale>
24 namespace boost {
25 namespace date_time {
27 //! Output facet base class for gregorian dates.
28 /*! This class is a base class for date facets used to localize the
29 * names of months and the names of days in the week.
31 * Requirements of Config
32 * - define an enumeration month_enum that enumerates the months.
33 * The enumeration should be '1' based eg: Jan==1
34 * - define as_short_string and as_long_string
36 * (see langer & kreft p334).
39 template<class Config,
40 class charT = char,
41 class OutputIterator = std::ostreambuf_iterator<charT> >
42 class date_names_put : public std::locale::facet
44 public:
45 date_names_put() {};
46 typedef OutputIterator iter_type;
47 typedef typename Config::month_type month_type;
48 typedef typename Config::month_enum month_enum;
49 typedef typename Config::weekday_enum weekday_enum;
50 typedef typename Config::special_value_enum special_value_enum;
51 //typedef typename Config::format_type format_type;
52 typedef std::basic_string<charT> string_type;
53 typedef charT char_type;
54 static const char_type default_special_value_names[3][17];
55 static const char_type separator[2];
57 static std::locale::id id;
59 #if defined (__SUNPRO_CC) && defined (_RWSTD_VER)
60 std::locale::id& __get_id (void) const { return id; }
61 #endif
63 void put_special_value(iter_type& oitr, special_value_enum sv) const
65 do_put_special_value(oitr, sv);
67 void put_month_short(iter_type& oitr, month_enum moy) const
69 do_put_month_short(oitr, moy);
71 void put_month_long(iter_type& oitr, month_enum moy) const
73 do_put_month_long(oitr, moy);
75 void put_weekday_short(iter_type& oitr, weekday_enum wd) const
77 do_put_weekday_short(oitr, wd);
79 void put_weekday_long(iter_type& oitr, weekday_enum wd) const
81 do_put_weekday_long(oitr, wd);
83 bool has_date_sep_chars() const
85 return do_has_date_sep_chars();
87 void year_sep_char(iter_type& oitr) const
89 do_year_sep_char(oitr);
91 //! char between year-month
92 void month_sep_char(iter_type& oitr) const
94 do_month_sep_char(oitr);
96 //! Char to separate month-day
97 void day_sep_char(iter_type& oitr) const
99 do_day_sep_char(oitr);
101 //! Determines the order to put the date elements
102 ymd_order_spec date_order() const
104 return do_date_order();
106 //! Determines if month is displayed as integer, short or long string
107 month_format_spec month_format() const
109 return do_month_format();
112 protected:
113 //! Default facet implementation uses month_type defaults
114 virtual void do_put_month_short(iter_type& oitr, month_enum moy) const
116 month_type gm(moy);
117 charT c = '\0';
118 put_string(oitr, gm.as_short_string(c));
120 //! Default facet implementation uses month_type defaults
121 virtual void do_put_month_long(iter_type& oitr,
122 month_enum moy) const
124 month_type gm(moy);
125 charT c = '\0';
126 put_string(oitr, gm.as_long_string(c));
128 //! Default facet implementation for special value types
129 virtual void do_put_special_value(iter_type& oitr, special_value_enum sv) const
131 if(sv <= 2) { // only output not_a_date_time, neg_infin, or pos_infin
132 string_type s(default_special_value_names[sv]);
133 put_string(oitr, s);
136 virtual void do_put_weekday_short(iter_type&, weekday_enum) const
139 virtual void do_put_weekday_long(iter_type&, weekday_enum) const
142 virtual bool do_has_date_sep_chars() const
144 return true;
146 virtual void do_year_sep_char(iter_type& oitr) const
148 string_type s(separator);
149 put_string(oitr, s);
151 //! char between year-month
152 virtual void do_month_sep_char(iter_type& oitr) const
154 string_type s(separator);
155 put_string(oitr, s);
157 //! Char to separate month-day
158 virtual void do_day_sep_char(iter_type& oitr) const
160 string_type s(separator); //put in '-'
161 put_string(oitr, s);
163 //! Default for date order
164 virtual ymd_order_spec do_date_order() const
166 return ymd_order_iso;
168 //! Default month format
169 virtual month_format_spec do_month_format() const
171 return month_as_short_string;
173 void put_string(iter_type& oi, const charT* const s) const
175 string_type s1(boost::lexical_cast<string_type>(s));
176 typename string_type::iterator si,end;
177 for (si=s1.begin(), end=s1.end(); si!=end; si++, oi++) {
178 *oi = *si;
181 void put_string(iter_type& oi, const string_type& s1) const
183 typename string_type::const_iterator si,end;
184 for (si=s1.begin(), end=s1.end(); si!=end; si++, oi++) {
185 *oi = *si;
190 template<class Config, class charT, class OutputIterator>
191 const typename date_names_put<Config, charT, OutputIterator>::char_type
192 date_names_put<Config, charT, OutputIterator>::default_special_value_names[3][17] = {
193 {'n','o','t','-','a','-','d','a','t','e','-','t','i','m','e'},
194 {'-','i','n','f','i','n','i','t','y'},
195 {'+','i','n','f','i','n','i','t','y'} };
197 template<class Config, class charT, class OutputIterator>
198 const typename date_names_put<Config, charT, OutputIterator>::char_type
199 date_names_put<Config, charT, OutputIterator>::separator[2] =
200 {'-', '\0'} ;
203 //! Generate storage location for a std::locale::id
204 template<class Config, class charT, class OutputIterator>
205 std::locale::id date_names_put<Config, charT, OutputIterator>::id;
207 //! A date name output facet that takes an array of char* to define strings
208 template<class Config,
209 class charT = char,
210 class OutputIterator = std::ostreambuf_iterator<charT> >
211 class all_date_names_put : public date_names_put<Config, charT, OutputIterator>
213 public:
214 all_date_names_put(const charT* const month_short_names[],
215 const charT* const month_long_names[],
216 const charT* const special_value_names[],
217 const charT* const weekday_short_names[],
218 const charT* const weekday_long_names[],
219 charT separator_char = '-',
220 ymd_order_spec order_spec = ymd_order_iso,
221 month_format_spec month_format = month_as_short_string) :
222 month_short_names_(month_short_names),
223 month_long_names_(month_long_names),
224 special_value_names_(special_value_names),
225 weekday_short_names_(weekday_short_names),
226 weekday_long_names_(weekday_long_names),
227 order_spec_(order_spec),
228 month_format_spec_(month_format)
230 separator_char_[0] = separator_char;
231 separator_char_[1] = '\0';
234 typedef OutputIterator iter_type;
235 typedef typename Config::month_enum month_enum;
236 typedef typename Config::weekday_enum weekday_enum;
237 typedef typename Config::special_value_enum special_value_enum;
239 const charT* const* get_short_month_names() const
241 return month_short_names_;
243 const charT* const* get_long_month_names() const
245 return month_long_names_;
247 const charT* const* get_special_value_names() const
249 return special_value_names_;
251 const charT* const* get_short_weekday_names()const
253 return weekday_short_names_;
255 const charT* const* get_long_weekday_names()const
257 return weekday_long_names_;
260 protected:
261 //! Generic facet that takes array of chars
262 virtual void do_put_month_short(iter_type& oitr, month_enum moy) const
264 this->put_string(oitr, month_short_names_[moy-1]);
266 //! Long month names
267 virtual void do_put_month_long(iter_type& oitr, month_enum moy) const
269 this->put_string(oitr, month_long_names_[moy-1]);
271 //! Special values names
272 virtual void do_put_special_value(iter_type& oitr, special_value_enum sv) const
274 this->put_string(oitr, special_value_names_[sv]);
276 virtual void do_put_weekday_short(iter_type& oitr, weekday_enum wd) const
278 this->put_string(oitr, weekday_short_names_[wd]);
280 virtual void do_put_weekday_long(iter_type& oitr, weekday_enum wd) const
282 this->put_string(oitr, weekday_long_names_[wd]);
284 //! char between year-month
285 virtual void do_month_sep_char(iter_type& oitr) const
287 this->put_string(oitr, separator_char_);
289 //! Char to separate month-day
290 virtual void do_day_sep_char(iter_type& oitr) const
292 this->put_string(oitr, separator_char_);
294 //! Set the date ordering
295 virtual ymd_order_spec do_date_order() const
297 return order_spec_;
299 //! Set the date ordering
300 virtual month_format_spec do_month_format() const
302 return month_format_spec_;
305 private:
306 const charT* const* month_short_names_;
307 const charT* const* month_long_names_;
308 const charT* const* special_value_names_;
309 const charT* const* weekday_short_names_;
310 const charT* const* weekday_long_names_;
311 charT separator_char_[2];
312 ymd_order_spec order_spec_;
313 month_format_spec month_format_spec_;
316 } } //namespace boost::date_time
318 #endif //BOOST_NO_STD_LOCALE
320 #endif