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
.as_enum(), 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
;
218 typedef boost::date_time::all_date_names_put
<greg_facet_config
, charT
> facet_def
;
219 d
= from_stream(beg
, eos
);
222 #endif // BOOST_NO_STD_ITERATOR_TRAITS
224 //! operator>> for gregorian::date_duration
225 template<class charT
>
227 std::basic_istream
<charT
>& operator>>(std::basic_istream
<charT
>& is
,
232 dd
= date_duration(v
);
236 //! operator>> for gregorian::date_period
237 template<class charT
>
239 std::basic_istream
<charT
>& operator>>(std::basic_istream
<charT
>& is
,
242 std::basic_string
<charT
> s
;
244 dp
= date_time::from_simple_string_type
<date
>(s
);
248 //! generates a locale with the set of gregorian name-strings of type char*
249 BOOST_DATE_TIME_DECL
std::locale
generate_locale(std::locale
& loc
, char type
);
251 //! Returns a pointer to a facet with a default set of names (English)
252 /* Necessary in the event an exception is thrown from op>> for
253 * weekday or month. See comments in those functions for more info */
254 BOOST_DATE_TIME_DECL
boost::date_time::all_date_names_put
<greg_facet_config
, char>* create_facet_def(char type
);
256 #ifndef BOOST_NO_STD_WSTRING
257 //! generates a locale with the set of gregorian name-strings of type wchar_t*
258 BOOST_DATE_TIME_DECL
std::locale
generate_locale(std::locale
& loc
, wchar_t type
);
259 //! Returns a pointer to a facet with a default set of names (English)
260 /* Necessary in the event an exception is thrown from op>> for
261 * weekday or month. See comments in those functions for more info */
262 BOOST_DATE_TIME_DECL
boost::date_time::all_date_names_put
<greg_facet_config
, wchar_t>* create_facet_def(wchar_t type
);
263 #endif // BOOST_NO_STD_WSTRING
265 //! operator>> for gregorian::greg_month - throws exception if invalid month given
266 template<class charT
>
268 std::basic_istream
<charT
>& operator>>(std::basic_istream
<charT
>& is
,greg_month
& m
)
270 typedef boost::date_time::all_date_names_put
<greg_facet_config
, charT
> facet_def
;
272 std::basic_string
<charT
> s
;
275 if(!std::has_facet
<facet_def
>(is
.getloc())) {
276 std::locale loc
= is
.getloc();
278 is
.imbue(generate_locale(loc
, a
));
284 const facet_def
& f
= std::use_facet
<facet_def
>(is
.getloc());
285 num
= date_time::find_match(f
.get_short_month_names(),
286 f
.get_long_month_names(),
287 (greg_month::max
)(), s
); // greg_month spans 1..12, so max returns the array size,
288 // which is needed by find_match
290 /* bad_cast will be thrown if the desired facet is not accessible
291 * so we can generate the facet. This has the drawback of using english
292 * names as a default. */
293 catch(std::bad_cast
&){
295 std::auto_ptr
< const facet_def
> f(create_facet_def(a
));
296 num
= date_time::find_match(f
->get_short_month_names(),
297 f
->get_long_month_names(),
298 (greg_month::max
)(), s
); // greg_month spans 1..12, so max returns the array size,
299 // which is needed by find_match
302 ++num
; // months numbered 1-12
308 //! operator>> for gregorian::greg_weekday - throws exception if invalid weekday given
309 template<class charT
>
311 std::basic_istream
<charT
>& operator>>(std::basic_istream
<charT
>& is
,greg_weekday
& wd
)
313 typedef boost::date_time::all_date_names_put
<greg_facet_config
, charT
> facet_def
;
315 std::basic_string
<charT
> s
;
318 if(!std::has_facet
<facet_def
>(is
.getloc())) {
319 std::locale loc
= is
.getloc();
321 is
.imbue(generate_locale(loc
, a
));
326 const facet_def
& f
= std::use_facet
<facet_def
>(is
.getloc());
327 num
= date_time::find_match(f
.get_short_weekday_names(),
328 f
.get_long_weekday_names(),
329 (greg_weekday::max
)() + 1, s
); // greg_weekday spans 0..6, so increment is needed
330 // to form the array size which is needed by find_match
332 /* bad_cast will be thrown if the desired facet is not accessible
333 * so we can generate the facet. This has the drawback of using english
334 * names as a default. */
335 catch(std::bad_cast
&){
337 std::auto_ptr
< const facet_def
> f(create_facet_def(a
));
338 num
= date_time::find_match(f
->get_short_weekday_names(),
339 f
->get_long_weekday_names(),
340 (greg_weekday::max
)() + 1, s
); // greg_weekday spans 0..6, so increment is needed
341 // to form the array size which is needed by find_match
344 wd
= greg_weekday(num
); // weekdays numbered 0-6
348 } } //namespace gregorian