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/compiler_config.hpp>
13 #include <boost/date_time/gregorian/gregorian_types.hpp>
14 #include <boost/date_time/date_formatting_locales.hpp> // sets BOOST_DATE_TIME_NO_LOCALE
15 #include <boost/date_time/gregorian/parsers.hpp>
17 //This file is basically commented out if locales are not supported
18 #ifndef BOOST_DATE_TIME_NO_LOCALE
29 //! Configuration of the output facet template
30 struct BOOST_SYMBOL_VISIBLE greg_facet_config
32 typedef boost::gregorian::greg_month month_type;
33 typedef boost::date_time::special_values special_value_enum;
34 typedef boost::gregorian::months_of_year month_enum;
35 typedef boost::date_time::weekdays weekday_enum;
38 #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
39 //! Create the base facet type for gregorian::date
40 typedef boost::date_time::date_names_put<greg_facet_config> greg_base_facet;
42 //! ostream operator for gregorian::date
43 /*! Uses the date facet to determine various output parameters including:
44 * - string values for the month (eg: Jan, Feb, Mar) (default: English)
45 * - string values for special values (eg: not-a-date-time) (default: English)
46 * - selection of long, short strings, or numerical month representation (default: short string)
47 * - month day year order (default yyyy-mmm-dd)
49 template <class charT, class traits>
51 std::basic_ostream<charT, traits>&
52 operator<<(std::basic_ostream<charT, traits>& os, const date& d)
54 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
55 typedef boost::date_time::ostream_date_formatter<date, facet_def, charT> greg_ostream_formatter;
56 greg_ostream_formatter::date_put(d, os);
60 //! operator<< for gregorian::greg_month typically streaming: Jan, Feb, Mar...
61 /*! Uses the date facet to determine output string as well as selection of long or short strings.
62 * Default if no facet is installed is to output a 2 wide numeric value for the month
63 * eg: 01 == Jan, 02 == Feb, ... 12 == Dec.
65 template <class charT, class traits>
67 std::basic_ostream<charT, traits>&
68 operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
70 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
71 typedef boost::date_time::ostream_month_formatter<facet_def, charT> greg_month_formatter;
72 std::locale locale = os.getloc();
73 if (std::has_facet<facet_def>(locale)) {
74 const facet_def& f = std::use_facet<facet_def>(locale);
75 greg_month_formatter::format_month(m, os, f);
78 else { //default to numeric
79 charT fill_char = '0';
80 os << std::setw(2) << std::setfill(fill_char) << m.as_number();
86 //! operator<< for gregorian::greg_weekday typically streaming: Sun, Mon, Tue, ...
87 /*! Uses the date facet to determine output string as well as selection of long or short string.
88 * Default if no facet is installed is to output a 3 char english string for the
91 template <class charT, class traits>
93 std::basic_ostream<charT, traits>&
94 operator<<(std::basic_ostream<charT, traits>& os, const greg_weekday& wd)
96 typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
97 typedef boost::date_time::ostream_weekday_formatter<greg_weekday, facet_def, charT> greg_weekday_formatter;
98 std::locale locale = os.getloc();
99 if (std::has_facet<facet_def>(locale)) {
100 const facet_def& f = std::use_facet<facet_def>(locale);
101 greg_weekday_formatter::format_weekday(wd, os, f, true);
103 else { //default to short English string eg: Sun, Mon, Tue, Wed...
104 os << wd.as_short_string();
110 //! operator<< for gregorian::date_period typical output: [2002-Jan-01/2002-Jan-31]
111 /*! Uses the date facet to determine output string as well as selection of long
112 * or short string fr dates.
113 * Default if no facet is installed is to output a 3 char english string for the
116 template <class charT, class traits>
118 std::basic_ostream<charT, traits>&
119 operator<<(std::basic_ostream<charT, traits>& os, const date_period& dp)
121 os << '['; //TODO: facet or manipulator for periods?
123 os << '/'; //TODO: facet or manipulator for periods?
129 template <class charT, class traits>
131 std::basic_ostream<charT, traits>&
132 operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
139 //! operator<< for gregorian::partial_date. Output: "Jan 1"
140 template <class charT, class traits>
142 std::basic_ostream<charT, traits>&
143 operator<<(std::basic_ostream<charT, traits>& os, const partial_date& pd)
145 os << std::setw(2) << std::setfill('0') << pd.day() << ' '
146 << pd.month().as_short_string() ;
150 //! operator<< for gregorian::nth_kday_of_month. Output: "first Mon of Jun"
151 template <class charT, class traits>
153 std::basic_ostream<charT, traits>&
154 operator<<(std::basic_ostream<charT, traits>& os,
155 const nth_kday_of_month& nkd)
157 os << nkd.nth_week_as_str() << ' '
158 << nkd.day_of_week() << " of "
159 << nkd.month().as_short_string() ;
163 //! operator<< for gregorian::first_kday_of_month. Output: "first Mon of Jun"
164 template <class charT, class traits>
166 std::basic_ostream<charT, traits>&
167 operator<<(std::basic_ostream<charT, traits>& os,
168 const first_kday_of_month& fkd)
170 os << "first " << fkd.day_of_week() << " of "
171 << fkd.month().as_short_string() ;
175 //! operator<< for gregorian::last_kday_of_month. Output: "last Mon of Jun"
176 template <class charT, class traits>
178 std::basic_ostream<charT, traits>&
179 operator<<(std::basic_ostream<charT, traits>& os,
180 const last_kday_of_month& lkd)
182 os << "last " << lkd.day_of_week() << " of "
183 << lkd.month().as_short_string() ;
187 //! operator<< for gregorian::first_kday_after. Output: "first Mon after"
188 template <class charT, class traits>
190 std::basic_ostream<charT, traits>&
191 operator<<(std::basic_ostream<charT, traits>& os,
192 const first_kday_after& fka)
194 os << fka.day_of_week() << " after";
198 //! operator<< for gregorian::first_kday_before. Output: "first Mon before"
199 template <class charT, class traits>
201 std::basic_ostream<charT, traits>&
202 operator<<(std::basic_ostream<charT, traits>& os,
203 const first_kday_before& fkb)
205 os << fkb.day_of_week() << " before";
208 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
209 /**************** Input Streaming ******************/
211 #if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
212 //! operator>> for gregorian::date
213 template<class charT>
215 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, date& d)
217 std::istream_iterator<std::basic_string<charT>, charT> beg(is), eos;
218 d = from_stream(beg, eos);
221 #endif // BOOST_NO_STD_ITERATOR_TRAITS
223 //! operator>> for gregorian::date_duration
224 template<class charT>
226 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
231 dd = date_duration(v);
235 //! operator>> for gregorian::date_period
236 template<class charT>
238 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
241 std::basic_string<charT> s;
243 dp = date_time::from_simple_string_type<date>(s);
247 //! generates a locale with the set of gregorian name-strings of type char*
248 BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, char type);
250 //! Returns a pointer to a facet with a default set of names (English)
251 /* Necessary in the event an exception is thrown from op>> for
252 * weekday or month. See comments in those functions for more info */
253 BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, char>* create_facet_def(char type);
255 #ifndef BOOST_NO_STD_WSTRING
256 //! generates a locale with the set of gregorian name-strings of type wchar_t*
257 BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, wchar_t type);
258 //! Returns a pointer to a facet with a default set of names (English)
259 /* Necessary in the event an exception is thrown from op>> for
260 * weekday or month. See comments in those functions for more info */
261 BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, wchar_t>* create_facet_def(wchar_t type);
262 #endif // BOOST_NO_STD_WSTRING
264 //! operator>> for gregorian::greg_month - throws exception if invalid month given
265 template<class charT>
267 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m)
269 typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
271 std::basic_string<charT> s;
274 if(!std::has_facet<facet_def>(is.getloc())) {
275 std::locale loc = is.getloc();
277 is.imbue(generate_locale(loc, a));
283 const facet_def& f = std::use_facet<facet_def>(is.getloc());
284 num = date_time::find_match(f.get_short_month_names(),
285 f.get_long_month_names(),
286 (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
287 // which is needed by find_match
289 /* bad_cast will be thrown if the desired facet is not accessible
290 * so we can generate the facet. This has the drawback of using english
291 * names as a default. */
292 catch(std::bad_cast&){
295 #if defined(BOOST_NO_CXX11_SMART_PTR)
297 std::auto_ptr< const facet_def > f(create_facet_def(a));
301 std::unique_ptr< const facet_def > f(create_facet_def(a));
305 num = date_time::find_match(f->get_short_month_names(),
306 f->get_long_month_names(),
307 (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
308 // which is needed by find_match
311 ++num; // months numbered 1-12
317 //! operator>> for gregorian::greg_weekday - throws exception if invalid weekday given
318 template<class charT>
320 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd)
322 typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
324 std::basic_string<charT> s;
327 if(!std::has_facet<facet_def>(is.getloc())) {
328 std::locale loc = is.getloc();
330 is.imbue(generate_locale(loc, a));
335 const facet_def& f = std::use_facet<facet_def>(is.getloc());
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
341 /* bad_cast will be thrown if the desired facet is not accessible
342 * so we can generate the facet. This has the drawback of using english
343 * names as a default. */
344 catch(std::bad_cast&){
347 #if defined(BOOST_NO_CXX11_SMART_PTR)
349 std::auto_ptr< const facet_def > f(create_facet_def(a));
353 std::unique_ptr< const facet_def > f(create_facet_def(a));
357 num = date_time::find_match(f->get_short_weekday_names(),
358 f->get_long_weekday_names(),
359 (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
360 // to form the array size which is needed by find_match
363 wd = greg_weekday(num); // weekdays numbered 0-6
367 } } //namespace gregorian