PageRenderTime 36ms CodeModel.GetById 11ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/Src/Dependencies/Boost/boost/date_time/gregorian/greg_facet.hpp

http://hadesmem.googlecode.com/
C++ Header | 354 lines | 245 code | 45 blank | 64 comment | 6 complexity | 77d623bbac8fccc848ffc3890f1fc342 MD5 | raw file
  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: 2008-11-23 22:13:35 +1100 (Sun, 23 Nov 2008) $
 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
 16//This file is basically commented out if locales are not supported
 17#ifndef BOOST_DATE_TIME_NO_LOCALE
 18
 19#include <string>
 20#include <memory>
 21#include <locale>
 22#include <iostream>
 23#include <exception>
 24
 25namespace boost {
 26namespace gregorian {
 27  
 28  //! Configuration of the output facet template
 29  struct greg_facet_config
 30  {
 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;
 35  };
 36
 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;
 40
 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)
 47   */
 48  template <class charT, class traits>
 49  inline
 50  std::basic_ostream<charT, traits>&
 51  operator<<(std::basic_ostream<charT, traits>& os, const date& d)
 52  {
 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);
 56    return os;
 57  }
 58
 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.
 63   */
 64  template <class charT, class traits>
 65  inline
 66  std::basic_ostream<charT, traits>&
 67  operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
 68  {
 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);
 75
 76    }
 77    else { //default to numeric
 78      charT fill_char = '0';
 79      os  << std::setw(2) << std::setfill(fill_char) << m.as_number();
 80    }
 81
 82    return os;
 83  }
 84
 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
 88   *  day of the week.
 89   */
 90  template <class charT, class traits>
 91  inline
 92  std::basic_ostream<charT, traits>&
 93  operator<<(std::basic_ostream<charT, traits>& os, const greg_weekday& wd)
 94  {
 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);
101    }
102    else { //default to short English string eg: Sun, Mon, Tue, Wed...
103      os  << wd.as_short_string();
104    }
105
106    return os;
107  }
108
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
113   *  day of the week.
114   */
115  template <class charT, class traits>
116  inline
117  std::basic_ostream<charT, traits>&
118  operator<<(std::basic_ostream<charT, traits>& os, const date_period& dp)
119  {
120    os << '['; //TODO: facet or manipulator for periods?
121    os << dp.begin();
122    os << '/'; //TODO: facet or manipulator for periods?
123    os << dp.last();
124    os << ']'; 
125    return os;
126  }
127
128  template <class charT, class traits>
129  inline
130  std::basic_ostream<charT, traits>&
131  operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
132  {
133    //os << dd.days();
134    os << dd.get_rep();
135    return os;
136  }
137
138  //! operator<< for gregorian::partial_date. Output: "Jan 1"
139  template <class charT, class traits>
140  inline
141  std::basic_ostream<charT, traits>&
142  operator<<(std::basic_ostream<charT, traits>& os, const partial_date& pd)
143  {
144    os << std::setw(2) << std::setfill('0') << pd.day() << ' ' 
145       << pd.month().as_short_string() ; 
146    return os;
147  }
148
149  //! operator<< for gregorian::nth_kday_of_month. Output: "first Mon of Jun"
150  template <class charT, class traits>
151  inline
152  std::basic_ostream<charT, traits>&
153  operator<<(std::basic_ostream<charT, traits>& os, 
154             const nth_kday_of_month& nkd)
155  {
156    os << nkd.nth_week_as_str() << ' ' 
157       << nkd.day_of_week() << " of "
158       << nkd.month().as_short_string() ; 
159    return os;
160  }
161
162  //! operator<< for gregorian::first_kday_of_month. Output: "first Mon of Jun"
163  template <class charT, class traits>
164  inline
165  std::basic_ostream<charT, traits>&
166  operator<<(std::basic_ostream<charT, traits>& os, 
167             const first_kday_of_month& fkd)
168  {
169    os << "first " << fkd.day_of_week() << " of " 
170       << fkd.month().as_short_string() ; 
171    return os;
172  }
173
174  //! operator<< for gregorian::last_kday_of_month. Output: "last Mon of Jun"
175  template <class charT, class traits>
176  inline
177  std::basic_ostream<charT, traits>&
178  operator<<(std::basic_ostream<charT, traits>& os, 
179             const last_kday_of_month& lkd)
180  {
181    os << "last " << lkd.day_of_week() << " of " 
182       << lkd.month().as_short_string() ; 
183    return os;
184  }
185
186  //! operator<< for gregorian::first_kday_after. Output: "first Mon after"
187  template <class charT, class traits>
188  inline
189  std::basic_ostream<charT, traits>&
190  operator<<(std::basic_ostream<charT, traits>& os, 
191             const first_kday_after& fka)
192  {
193    os << fka.day_of_week() << " after"; 
194    return os;
195  }
196
197  //! operator<< for gregorian::first_kday_before. Output: "first Mon before"
198  template <class charT, class traits>
199  inline
200  std::basic_ostream<charT, traits>&
201  operator<<(std::basic_ostream<charT, traits>& os, 
202             const first_kday_before& fkb)
203  {
204    os << fkb.day_of_week() << " before"; 
205    return os;
206  }
207#endif // USE_DATE_TIME_PRE_1_33_FACET_IO
208  /**************** Input Streaming ******************/
209  
210#if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
211  //! operator>> for gregorian::date
212  template<class charT>
213  inline 
214  std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, date& d)
215  {
216    std::istream_iterator<std::basic_string<charT>, charT> beg(is), eos;
217    
218    typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
219    d = from_stream(beg, eos);
220    return is;
221  }
222#endif // BOOST_NO_STD_ITERATOR_TRAITS
223
224  //! operator>> for gregorian::date_duration
225  template<class charT>
226  inline
227  std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, 
228                                        date_duration& dd)
229  {
230    long v;
231    is >> v;
232    dd = date_duration(v);
233    return is;
234  }
235
236  //! operator>> for gregorian::date_period
237  template<class charT>
238  inline
239  std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
240                                        date_period& dp)
241  {
242    std::basic_string<charT> s;
243    is >> s;
244    dp = date_time::from_simple_string_type<date>(s);
245    return is;
246  }
247
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);
250
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);
255
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
264
265  //! operator>> for gregorian::greg_month - throws exception if invalid month given
266  template<class charT>
267  inline
268  std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m) 
269  {
270    typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
271
272    std::basic_string<charT> s;
273    is >> s;
274    
275    if(!std::has_facet<facet_def>(is.getloc())) {
276      std::locale loc = is.getloc();
277      charT a = '\0';
278      is.imbue(generate_locale(loc, a));
279    }
280
281    short num = 0;
282
283    try{
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
289    }
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&){
294      charT a = '\0';
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
300    }
301    
302    ++num; // months numbered 1-12
303    m = greg_month(num); 
304
305    return is;
306  }
307
308  //! operator>> for gregorian::greg_weekday  - throws exception if invalid weekday given
309  template<class charT>
310  inline
311  std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd) 
312  {
313    typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
314
315    std::basic_string<charT> s;
316    is >> s;
317
318    if(!std::has_facet<facet_def>(is.getloc())) {
319      std::locale loc = is.getloc();
320      charT a = '\0';
321      is.imbue(generate_locale(loc, a));
322    }
323
324    short num = 0;
325    try{
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
331    }
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&){
336      charT a = '\0';
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
342    }
343   
344    wd = greg_weekday(num); // weekdays numbered 0-6
345    return is;
346  }
347
348} } //namespace gregorian
349
350#endif  
351    
352    
353#endif
354