1 #ifndef GREGORIAN_FACET_HPP___
2 #define GREGORIAN_FACET_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
12 #include "boost/date_time/gregorian/gregorian_types.hpp"
13 #include "boost/date_time/date_formatting_locales.hpp" // sets BOOST_DATE_TIME_NO_LOCALE
14 #include "boost/date_time/gregorian/parsers.hpp"
16 //This file is basically commented out if locales are not supported
17 #ifndef BOOST_DATE_TIME_NO_LOCALE
28 //! Configuration of the output facet template
29 struct greg_facet_config
31 typedef boost::gregorian::greg_month month_type;
32 typedef boost::date_time::special_values special_value_enum;
33 typedef boost::gregorian::months_of_year month_enum;
34 typedef boost::date_time::weekdays weekday_enum;
37 #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
38 //! Create the base facet type for gregorian::date
39 typedef boost::date_time::date_names_put<greg_facet_config> greg_base_facet;
41 //! ostream operator for gregorian::date
42 /*! Uses the date facet to determine various output parameters including:
43 * - string values for the month (eg: Jan, Feb, Mar) (default: English)
44 * - string values for special values (eg: not-a-date-time) (default: English)
45 * - selection of long, short strings, or numerical month representation (default: short string)
46 * - month day year order (default yyyy-mmm-dd)
48 template <class charT, class traits>
50 std::basic_ostream<charT, traits>&
51 operator<<(std::basic_ostream<charT, traits>& os, const date& d)
53 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
54 typedef boost::date_time::ostream_date_formatter<date, facet_def, charT> greg_ostream_formatter;
55 greg_ostream_formatter::date_put(d, os);
59 //! operator<< for gregorian::greg_month typically streaming: Jan, Feb, Mar...
60 /*! Uses the date facet to determine output string as well as selection of long or short strings.
61 * Default if no facet is installed is to output a 2 wide numeric value for the month
62 * eg: 01 == Jan, 02 == Feb, ... 12 == Dec.
64 template <class charT, class traits>
66 std::basic_ostream<charT, traits>&
67 operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
69 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
70 typedef boost::date_time::ostream_month_formatter<facet_def, charT> greg_month_formatter;
71 std::locale locale = os.getloc();
72 if (std::has_facet<facet_def>(locale)) {
73 const facet_def& f = std::use_facet<facet_def>(locale);
74 greg_month_formatter::format_month(m, os, f);
77 else { //default to numeric
78 charT fill_char = '0';
79 os << std::setw(2) << std::setfill(fill_char) << m.as_number();
85 //! operator<< for gregorian::greg_weekday typically streaming: Sun, Mon, Tue, ...
86 /*! Uses the date facet to determine output string as well as selection of long or short string.
87 * Default if no facet is installed is to output a 3 char english string for the
90 template <class charT, class traits>
92 std::basic_ostream<charT, traits>&
93 operator<<(std::basic_ostream<charT, traits>& os, const greg_weekday& wd)
95 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
96 typedef boost::date_time::ostream_weekday_formatter<greg_weekday, facet_def, charT> greg_weekday_formatter;
97 std::locale locale = os.getloc();
98 if (std::has_facet<facet_def>(locale)) {
99 const facet_def& f = std::use_facet<facet_def>(locale);
100 greg_weekday_formatter::format_weekday(wd, os, f, true);
102 else { //default to short English string eg: Sun, Mon, Tue, Wed...
103 os << wd.as_short_string();
109 //! operator<< for gregorian::date_period typical output: [2002-Jan-01/2002-Jan-31]
110 /*! Uses the date facet to determine output string as well as selection of long
111 * or short string fr dates.
112 * Default if no facet is installed is to output a 3 char english string for the
115 template <class charT, class traits>
117 std::basic_ostream<charT, traits>&
118 operator<<(std::basic_ostream<charT, traits>& os, const date_period& dp)
120 os << '['; //TODO: facet or manipulator for periods?
122 os << '/'; //TODO: facet or manipulator for periods?
128 template <class charT, class traits>
130 std::basic_ostream<charT, traits>&
131 operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
138 //! operator<< for gregorian::partial_date. Output: "Jan 1"
139 template <class charT, class traits>
141 std::basic_ostream<charT, traits>&
142 operator<<(std::basic_ostream<charT, traits>& os, const partial_date& pd)
144 os << std::setw(2) << std::setfill('0') << pd.day() << ' '
145 << pd.month().as_short_string() ;
149 //! operator<< for gregorian::nth_kday_of_month. Output: "first Mon of Jun"
150 template <class charT, class traits>
152 std::basic_ostream<charT, traits>&
153 operator<<(std::basic_ostream<charT, traits>& os,
154 const nth_kday_of_month& nkd)
156 os << nkd.nth_week_as_str() << ' '
157 << nkd.day_of_week() << " of "
158 << nkd.month().as_short_string() ;
162 //! operator<< for gregorian::first_kday_of_month. Output: "first Mon of Jun"
163 template <class charT, class traits>
165 std::basic_ostream<charT, traits>&
166 operator<<(std::basic_ostream<charT, traits>& os,
167 const first_kday_of_month& fkd)
169 os << "first " << fkd.day_of_week() << " of "
170 << fkd.month().as_short_string() ;
174 //! operator<< for gregorian::last_kday_of_month. Output: "last Mon of Jun"
175 template <class charT, class traits>
177 std::basic_ostream<charT, traits>&
178 operator<<(std::basic_ostream<charT, traits>& os,
179 const last_kday_of_month& lkd)
181 os << "last " << lkd.day_of_week() << " of "
182 << lkd.month().as_short_string() ;
186 //! operator<< for gregorian::first_kday_after. Output: "first Mon after"
187 template <class charT, class traits>
189 std::basic_ostream<charT, traits>&
190 operator<<(std::basic_ostream<charT, traits>& os,
191 const first_kday_after& fka)
193 os << fka.day_of_week() << " after";
197 //! operator<< for gregorian::first_kday_before. Output: "first Mon before"
198 template <class charT, class traits>
200 std::basic_ostream<charT, traits>&
201 operator<<(std::basic_ostream<charT, traits>& os,
202 const first_kday_before& fkb)
204 os << fkb.day_of_week() << " before";
207 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
208 /**************** Input Streaming ******************/
210 #if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
211 //! operator>> for gregorian::date
212 template<class charT>
214 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, date& d)
216 std::istream_iterator<std::basic_string<charT>, charT> beg(is), eos;
217 d = from_stream(beg, eos);
220 #endif // BOOST_NO_STD_ITERATOR_TRAITS
222 //! operator>> for gregorian::date_duration
223 template<class charT>
225 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
230 dd = date_duration(v);
234 //! operator>> for gregorian::date_period
235 template<class charT>
237 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
240 std::basic_string<charT> s;
242 dp = date_time::from_simple_string_type<date>(s);
246 //! generates a locale with the set of gregorian name-strings of type char*
247 BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, char type);
249 //! Returns a pointer to a facet with a default set of names (English)
250 /* Necessary in the event an exception is thrown from op>> for
251 * weekday or month. See comments in those functions for more info */
252 BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, char>* create_facet_def(char type);
254 #ifndef BOOST_NO_STD_WSTRING
255 //! generates a locale with the set of gregorian name-strings of type wchar_t*
256 BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, wchar_t type);
257 //! Returns a pointer to a facet with a default set of names (English)
258 /* Necessary in the event an exception is thrown from op>> for
259 * weekday or month. See comments in those functions for more info */
260 BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, wchar_t>* create_facet_def(wchar_t type);
261 #endif // BOOST_NO_STD_WSTRING
263 //! operator>> for gregorian::greg_month - throws exception if invalid month given
264 template<class charT>
266 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m)
268 typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
270 std::basic_string<charT> s;
273 if(!std::has_facet<facet_def>(is.getloc())) {
274 std::locale loc = is.getloc();
276 is.imbue(generate_locale(loc, a));
282 const facet_def& f = std::use_facet<facet_def>(is.getloc());
283 num = date_time::find_match(f.get_short_month_names(),
284 f.get_long_month_names(),
285 (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
286 // which is needed by find_match
288 /* bad_cast will be thrown if the desired facet is not accessible
289 * so we can generate the facet. This has the drawback of using english
290 * names as a default. */
291 catch(std::bad_cast&){
293 std::auto_ptr< const facet_def > f(create_facet_def(a));
294 num = date_time::find_match(f->get_short_month_names(),
295 f->get_long_month_names(),
296 (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
297 // which is needed by find_match
300 ++num; // months numbered 1-12
306 //! operator>> for gregorian::greg_weekday - throws exception if invalid weekday given
307 template<class charT>
309 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd)
311 typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
313 std::basic_string<charT> s;
316 if(!std::has_facet<facet_def>(is.getloc())) {
317 std::locale loc = is.getloc();
319 is.imbue(generate_locale(loc, a));
324 const facet_def& f = std::use_facet<facet_def>(is.getloc());
325 num = date_time::find_match(f.get_short_weekday_names(),
326 f.get_long_weekday_names(),
327 (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
328 // to form the array size which is needed by find_match
330 /* bad_cast will be thrown if the desired facet is not accessible
331 * so we can generate the facet. This has the drawback of using english
332 * names as a default. */
333 catch(std::bad_cast&){
335 std::auto_ptr< const facet_def > f(create_facet_def(a));
336 num = date_time::find_match(f->get_short_weekday_names(),
337 f->get_long_weekday_names(),
338 (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
339 // to form the array size which is needed by find_match
342 wd = greg_weekday(num); // weekdays numbered 0-6
346 } } //namespace gregorian