source: NonGTP/Boost/boost/date_time/gregorian/greg_facet.hpp @ 857

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