• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef GREGORIAN_FACET_HPP___
2 #define GREGORIAN_FACET_HPP___
3 
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
9  * $Date$
10  */
11 
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>
16 #include <boost/io/ios_state.hpp>
17 
18 //This file is basically commented out if locales are not supported
19 #ifndef BOOST_DATE_TIME_NO_LOCALE
20 
21 #include <string>
22 #include <memory>
23 #include <locale>
24 #include <iostream>
25 #include <exception>
26 
27 namespace boost {
28 namespace gregorian {
29 
30   //! Configuration of the output facet template
31   struct BOOST_SYMBOL_VISIBLE greg_facet_config
32   {
33     typedef boost::gregorian::greg_month month_type;
34     typedef boost::date_time::special_values special_value_enum;
35     typedef boost::gregorian::months_of_year month_enum;
36     typedef boost::date_time::weekdays weekday_enum;
37   };
38 
39 #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
40   //! Create the base facet type for gregorian::date
41   typedef boost::date_time::date_names_put<greg_facet_config> greg_base_facet;
42 
43   //! ostream operator for gregorian::date
44   /*! Uses the date facet to determine various output parameters including:
45    *  - string values for the month (eg: Jan, Feb, Mar) (default: English)
46    *  - string values for special values (eg: not-a-date-time) (default: English)
47    *  - selection of long, short strings, or numerical month representation (default: short string)
48    *  - month day year order (default yyyy-mmm-dd)
49    */
50   template <class charT, class traits>
51   inline
52   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const date & d)53   operator<<(std::basic_ostream<charT, traits>& os, const date& d)
54   {
55     typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
56     typedef boost::date_time::ostream_date_formatter<date, facet_def, charT> greg_ostream_formatter;
57     greg_ostream_formatter::date_put(d, os);
58     return os;
59   }
60 
61   //! operator<< for gregorian::greg_month typically streaming: Jan, Feb, Mar...
62   /*! Uses the date facet to determine output string as well as selection of long or short strings.
63    *  Default if no facet is installed is to output a 2 wide numeric value for the month
64    *  eg: 01 == Jan, 02 == Feb, ... 12 == Dec.
65    */
66   template <class charT, class traits>
67   inline
68   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const greg_month & m)69   operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
70   {
71     typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
72     typedef boost::date_time::ostream_month_formatter<facet_def, charT> greg_month_formatter;
73     std::locale locale = os.getloc();
74     if (std::has_facet<facet_def>(locale)) {
75       const facet_def& f = std::use_facet<facet_def>(locale);
76       greg_month_formatter::format_month(m, os, f);
77 
78     }
79     else { // default to numeric
80       boost::io::basic_ios_fill_saver<charT> ifs(os);
81       os  << std::setw(2) << std::setfill(os.widen('0')) << m.as_number();
82     }
83 
84     return os;
85   }
86 
87   //! operator<< for gregorian::greg_weekday typically streaming: Sun, Mon, Tue, ...
88   /*! Uses the date facet to determine output string as well as selection of long or short string.
89    *  Default if no facet is installed is to output a 3 char english string for the
90    *  day of the week.
91    */
92   template <class charT, class traits>
93   inline
94   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const greg_weekday & wd)95   operator<<(std::basic_ostream<charT, traits>& os, const greg_weekday& wd)
96   {
97     typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
98     typedef boost::date_time::ostream_weekday_formatter<greg_weekday, facet_def, charT> greg_weekday_formatter;
99     std::locale locale = os.getloc();
100     if (std::has_facet<facet_def>(locale)) {
101       const facet_def& f = std::use_facet<facet_def>(locale);
102       greg_weekday_formatter::format_weekday(wd, os, f, true);
103     }
104     else { //default to short English string eg: Sun, Mon, Tue, Wed...
105       os  << wd.as_short_string();
106     }
107 
108     return os;
109   }
110 
111   //! operator<< for gregorian::date_period typical output: [2002-Jan-01/2002-Jan-31]
112   /*! Uses the date facet to determine output string as well as selection of long
113    *  or short string fr dates.
114    *  Default if no facet is installed is to output a 3 char english string for the
115    *  day of the week.
116    */
117   template <class charT, class traits>
118   inline
119   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const date_period & dp)120   operator<<(std::basic_ostream<charT, traits>& os, const date_period& dp)
121   {
122     os << '['; //TODO: facet or manipulator for periods?
123     os << dp.begin();
124     os << '/'; //TODO: facet or manipulator for periods?
125     os << dp.last();
126     os << ']';
127     return os;
128   }
129 
130   template <class charT, class traits>
131   inline
132   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const date_duration & dd)133   operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
134   {
135     //os << dd.days();
136     os << dd.get_rep();
137     return os;
138   }
139 
140   //! operator<< for gregorian::partial_date. Output: "Jan 1"
141   template <class charT, class traits>
142   inline
143   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const partial_date & pd)144   operator<<(std::basic_ostream<charT, traits>& os, const partial_date& pd)
145   {
146     boost::io::basic_ios_fill_saver<charT> ifs(os);
147     os << std::setw(2) << std::setfill(os.widen('0')) << pd.day() << ' '
148        << pd.month().as_short_string() ;
149     return os;
150   }
151 
152   //! operator<< for gregorian::nth_kday_of_month. Output: "first Mon of Jun"
153   template <class charT, class traits>
154   inline
155   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const nth_kday_of_month & nkd)156   operator<<(std::basic_ostream<charT, traits>& os,
157              const nth_kday_of_month& nkd)
158   {
159     os << nkd.nth_week_as_str() << ' '
160        << nkd.day_of_week() << " of "
161        << nkd.month().as_short_string() ;
162     return os;
163   }
164 
165   //! operator<< for gregorian::first_kday_of_month. Output: "first Mon of Jun"
166   template <class charT, class traits>
167   inline
168   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const first_kday_of_month & fkd)169   operator<<(std::basic_ostream<charT, traits>& os,
170              const first_kday_of_month& fkd)
171   {
172     os << "first " << fkd.day_of_week() << " of "
173        << fkd.month().as_short_string() ;
174     return os;
175   }
176 
177   //! operator<< for gregorian::last_kday_of_month. Output: "last Mon of Jun"
178   template <class charT, class traits>
179   inline
180   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const last_kday_of_month & lkd)181   operator<<(std::basic_ostream<charT, traits>& os,
182              const last_kday_of_month& lkd)
183   {
184     os << "last " << lkd.day_of_week() << " of "
185        << lkd.month().as_short_string() ;
186     return os;
187   }
188 
189   //! operator<< for gregorian::first_kday_after. Output: "first Mon after"
190   template <class charT, class traits>
191   inline
192   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const first_kday_after & fka)193   operator<<(std::basic_ostream<charT, traits>& os,
194              const first_kday_after& fka)
195   {
196     os << fka.day_of_week() << " after";
197     return os;
198   }
199 
200   //! operator<< for gregorian::first_kday_before. Output: "first Mon before"
201   template <class charT, class traits>
202   inline
203   std::basic_ostream<charT, traits>&
operator <<(std::basic_ostream<charT,traits> & os,const first_kday_before & fkb)204   operator<<(std::basic_ostream<charT, traits>& os,
205              const first_kday_before& fkb)
206   {
207     os << fkb.day_of_week() << " before";
208     return os;
209   }
210 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
211   /**************** Input Streaming ******************/
212 
213 #if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
214   //! operator>> for gregorian::date
215   template<class charT>
216   inline
operator >>(std::basic_istream<charT> & is,date & d)217   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, date& d)
218   {
219     std::istream_iterator<std::basic_string<charT>, charT> beg(is), eos;
220     d = from_stream(beg, eos);
221     return is;
222   }
223 #endif // BOOST_NO_STD_ITERATOR_TRAITS
224 
225   //! operator>> for gregorian::date_duration
226   template<class charT>
227   inline
operator >>(std::basic_istream<charT> & is,date_duration & dd)228   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
229                                         date_duration& dd)
230   {
231     long v;
232     is >> v;
233     dd = date_duration(v);
234     return is;
235   }
236 
237   //! operator>> for gregorian::date_period
238   template<class charT>
239   inline
operator >>(std::basic_istream<charT> & is,date_period & dp)240   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
241                                         date_period& dp)
242   {
243     std::basic_string<charT> s;
244     is >> s;
245     dp = date_time::from_simple_string_type<date>(s);
246     return is;
247   }
248 
249   //! generates a locale with the set of gregorian name-strings of type char*
250   BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, char type);
251 
252   //! Returns a pointer to a facet with a default set of names (English)
253   /* Necessary in the event an exception is thrown from op>> for
254    * weekday or month. See comments in those functions for more info */
255   BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, char>* create_facet_def(char type);
256 
257 #ifndef BOOST_NO_STD_WSTRING
258   //! generates a locale with the set of gregorian name-strings of type wchar_t*
259   BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, wchar_t type);
260   //! Returns a pointer to a facet with a default set of names (English)
261   /* Necessary in the event an exception is thrown from op>> for
262    * weekday or month. See comments in those functions for more info */
263   BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, wchar_t>* create_facet_def(wchar_t type);
264 #endif // BOOST_NO_STD_WSTRING
265 
266   //! operator>> for gregorian::greg_month - throws exception if invalid month given
267   template<class charT>
268   inline
operator >>(std::basic_istream<charT> & is,greg_month & m)269   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m)
270   {
271     typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
272 
273     std::basic_string<charT> s;
274     is >> s;
275 
276     if(!std::has_facet<facet_def>(is.getloc())) {
277       std::locale loc = is.getloc();
278       charT a = '\0';
279       is.imbue(generate_locale(loc, a));
280     }
281 
282     short num = 0;
283 
284     try{
285       const facet_def& f = std::use_facet<facet_def>(is.getloc());
286       num = date_time::find_match(f.get_short_month_names(),
287                                   f.get_long_month_names(),
288                                   (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
289                                                            // which is needed by find_match
290     }
291     /* bad_cast will be thrown if the desired facet is not accessible
292      * so we can generate the facet. This has the drawback of using english
293      * names as a default. */
294     catch(std::bad_cast&){
295       charT a = '\0';
296 
297 #if defined(BOOST_NO_CXX11_SMART_PTR)
298 
299       std::auto_ptr< const facet_def > f(create_facet_def(a));
300 
301 #else
302 
303       std::unique_ptr< const facet_def > f(create_facet_def(a));
304 
305 #endif
306 
307       num = date_time::find_match(f->get_short_month_names(),
308                                   f->get_long_month_names(),
309                                   (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
310                                                            // which is needed by find_match
311     }
312 
313     ++num; // months numbered 1-12
314     m = greg_month(num);
315 
316     return is;
317   }
318 
319   //! operator>> for gregorian::greg_weekday  - throws exception if invalid weekday given
320   template<class charT>
321   inline
operator >>(std::basic_istream<charT> & is,greg_weekday & wd)322   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd)
323   {
324     typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
325 
326     std::basic_string<charT> s;
327     is >> s;
328 
329     if(!std::has_facet<facet_def>(is.getloc())) {
330       std::locale loc = is.getloc();
331       charT a = '\0';
332       is.imbue(generate_locale(loc, a));
333     }
334 
335     short num = 0;
336     try{
337       const facet_def& f = std::use_facet<facet_def>(is.getloc());
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
342     }
343     /* bad_cast will be thrown if the desired facet is not accessible
344      * so we can generate the facet. This has the drawback of using english
345      * names as a default. */
346     catch(std::bad_cast&){
347       charT a = '\0';
348 
349 #if defined(BOOST_NO_CXX11_SMART_PTR)
350 
351       std::auto_ptr< const facet_def > f(create_facet_def(a));
352 
353 #else
354 
355       std::unique_ptr< const facet_def > f(create_facet_def(a));
356 
357 #endif
358 
359       num = date_time::find_match(f->get_short_weekday_names(),
360                                   f->get_long_weekday_names(),
361                                   (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
362                                                                  // to form the array size which is needed by find_match
363     }
364 
365     wd = greg_weekday(num); // weekdays numbered 0-6
366     return is;
367   }
368 
369 } } //namespace gregorian
370 
371 #endif
372 
373 #endif
374 
375