Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:34

0001 #ifndef GREGORIAN_FACET_HPP___
0002 #define GREGORIAN_FACET_HPP___
0003 
0004 /* Copyright (c) 2002,2003 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the 
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
0010  */
0011 
0012 #include <boost/date_time/compiler_config.hpp>
0013 #include <boost/date_time/gregorian/gregorian_types.hpp>
0014 #include <boost/date_time/date_formatting_locales.hpp> // sets BOOST_DATE_TIME_NO_LOCALE
0015 #include <boost/date_time/gregorian/parsers.hpp>
0016 #include <boost/io/ios_state.hpp>
0017 
0018 //This file is basically commented out if locales are not supported
0019 #ifndef BOOST_DATE_TIME_NO_LOCALE
0020 
0021 #include <string>
0022 #include <memory>
0023 #include <locale>
0024 #include <iostream>
0025 #include <exception>
0026 
0027 namespace boost {
0028 namespace gregorian {
0029   
0030   //! Configuration of the output facet template
0031   struct BOOST_SYMBOL_VISIBLE greg_facet_config
0032   {
0033     typedef boost::gregorian::greg_month month_type;
0034     typedef boost::date_time::special_values special_value_enum;
0035     typedef boost::gregorian::months_of_year month_enum;
0036     typedef boost::date_time::weekdays weekday_enum;
0037   };
0038 
0039 #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO)
0040   //! Create the base facet type for gregorian::date
0041   typedef boost::date_time::date_names_put<greg_facet_config> greg_base_facet;
0042 
0043   //! ostream operator for gregorian::date
0044   /*! Uses the date facet to determine various output parameters including:
0045    *  - string values for the month (eg: Jan, Feb, Mar) (default: English)
0046    *  - string values for special values (eg: not-a-date-time) (default: English)
0047    *  - selection of long, short strings, or numerical month representation (default: short string)
0048    *  - month day year order (default yyyy-mmm-dd)
0049    */
0050   template <class charT, class traits>
0051   inline
0052   std::basic_ostream<charT, traits>&
0053   operator<<(std::basic_ostream<charT, traits>& os, const date& d)
0054   {
0055     typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
0056     typedef boost::date_time::ostream_date_formatter<date, facet_def, charT> greg_ostream_formatter;
0057     greg_ostream_formatter::date_put(d, os);
0058     return os;
0059   }
0060 
0061   //! operator<< for gregorian::greg_month typically streaming: Jan, Feb, Mar...
0062   /*! Uses the date facet to determine output string as well as selection of long or short strings.
0063    *  Default if no facet is installed is to output a 2 wide numeric value for the month
0064    *  eg: 01 == Jan, 02 == Feb, ... 12 == Dec.
0065    */
0066   template <class charT, class traits>
0067   inline
0068   std::basic_ostream<charT, traits>&
0069   operator<<(std::basic_ostream<charT, traits>& os, const greg_month& m)
0070   {
0071     typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
0072     typedef boost::date_time::ostream_month_formatter<facet_def, charT> greg_month_formatter;
0073     std::locale locale = os.getloc();
0074     if (std::has_facet<facet_def>(locale)) {
0075       const facet_def& f = std::use_facet<facet_def>(locale);
0076       greg_month_formatter::format_month(m, os, f);
0077 
0078     }
0079     else { // default to numeric
0080       boost::io::basic_ios_fill_saver<charT> ifs(os);
0081       os  << std::setw(2) << std::setfill(os.widen('0')) << m.as_number();
0082     }
0083 
0084     return os;
0085   }
0086 
0087   //! operator<< for gregorian::greg_weekday typically streaming: Sun, Mon, Tue, ...
0088   /*! Uses the date facet to determine output string as well as selection of long or short string.
0089    *  Default if no facet is installed is to output a 3 char english string for the
0090    *  day of the week.
0091    */
0092   template <class charT, class traits>
0093   inline
0094   std::basic_ostream<charT, traits>&
0095   operator<<(std::basic_ostream<charT, traits>& os, const greg_weekday& wd)
0096   {
0097     typedef boost::date_time::date_names_put<greg_facet_config, charT> facet_def;
0098     typedef boost::date_time::ostream_weekday_formatter<greg_weekday, facet_def, charT> greg_weekday_formatter;
0099     std::locale locale = os.getloc();
0100     if (std::has_facet<facet_def>(locale)) {
0101       const facet_def& f = std::use_facet<facet_def>(locale);
0102       greg_weekday_formatter::format_weekday(wd, os, f, true);
0103     }
0104     else { //default to short English string eg: Sun, Mon, Tue, Wed...
0105       os  << wd.as_short_string();
0106     }
0107 
0108     return os;
0109   }
0110 
0111   //! operator<< for gregorian::date_period typical output: [2002-Jan-01/2002-Jan-31]
0112   /*! Uses the date facet to determine output string as well as selection of long 
0113    *  or short string fr dates.
0114    *  Default if no facet is installed is to output a 3 char english string for the
0115    *  day of the week.
0116    */
0117   template <class charT, class traits>
0118   inline
0119   std::basic_ostream<charT, traits>&
0120   operator<<(std::basic_ostream<charT, traits>& os, const date_period& dp)
0121   {
0122     os << '['; //TODO: facet or manipulator for periods?
0123     os << dp.begin();
0124     os << '/'; //TODO: facet or manipulator for periods?
0125     os << dp.last();
0126     os << ']'; 
0127     return os;
0128   }
0129 
0130   template <class charT, class traits>
0131   inline
0132   std::basic_ostream<charT, traits>&
0133   operator<<(std::basic_ostream<charT, traits>& os, const date_duration& dd)
0134   {
0135     //os << dd.days();
0136     os << dd.get_rep();
0137     return os;
0138   }
0139 
0140   //! operator<< for gregorian::partial_date. Output: "Jan 1"
0141   template <class charT, class traits>
0142   inline
0143   std::basic_ostream<charT, traits>&
0144   operator<<(std::basic_ostream<charT, traits>& os, const partial_date& pd)
0145   {
0146     boost::io::basic_ios_fill_saver<charT> ifs(os);
0147     os << std::setw(2) << std::setfill(os.widen('0')) << pd.day() << ' ' 
0148        << pd.month().as_short_string() ; 
0149     return os;
0150   }
0151 
0152   //! operator<< for gregorian::nth_kday_of_month. Output: "first Mon of Jun"
0153   template <class charT, class traits>
0154   inline
0155   std::basic_ostream<charT, traits>&
0156   operator<<(std::basic_ostream<charT, traits>& os, 
0157              const nth_kday_of_month& nkd)
0158   {
0159     os << nkd.nth_week_as_str() << ' ' 
0160        << nkd.day_of_week() << " of "
0161        << nkd.month().as_short_string() ; 
0162     return os;
0163   }
0164 
0165   //! operator<< for gregorian::first_kday_of_month. Output: "first Mon of Jun"
0166   template <class charT, class traits>
0167   inline
0168   std::basic_ostream<charT, traits>&
0169   operator<<(std::basic_ostream<charT, traits>& os, 
0170              const first_kday_of_month& fkd)
0171   {
0172     os << "first " << fkd.day_of_week() << " of " 
0173        << fkd.month().as_short_string() ; 
0174     return os;
0175   }
0176 
0177   //! operator<< for gregorian::last_kday_of_month. Output: "last Mon of Jun"
0178   template <class charT, class traits>
0179   inline
0180   std::basic_ostream<charT, traits>&
0181   operator<<(std::basic_ostream<charT, traits>& os, 
0182              const last_kday_of_month& lkd)
0183   {
0184     os << "last " << lkd.day_of_week() << " of " 
0185        << lkd.month().as_short_string() ; 
0186     return os;
0187   }
0188 
0189   //! operator<< for gregorian::first_kday_after. Output: "first Mon after"
0190   template <class charT, class traits>
0191   inline
0192   std::basic_ostream<charT, traits>&
0193   operator<<(std::basic_ostream<charT, traits>& os, 
0194              const first_kday_after& fka)
0195   {
0196     os << fka.day_of_week() << " after"; 
0197     return os;
0198   }
0199 
0200   //! operator<< for gregorian::first_kday_before. Output: "first Mon before"
0201   template <class charT, class traits>
0202   inline
0203   std::basic_ostream<charT, traits>&
0204   operator<<(std::basic_ostream<charT, traits>& os, 
0205              const first_kday_before& fkb)
0206   {
0207     os << fkb.day_of_week() << " before"; 
0208     return os;
0209   }
0210 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO
0211   /**************** Input Streaming ******************/
0212   
0213 #if !defined(BOOST_NO_STD_ITERATOR_TRAITS)
0214   //! operator>> for gregorian::date
0215   template<class charT>
0216   inline 
0217   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, date& d)
0218   {
0219     std::istream_iterator<std::basic_string<charT>, charT> beg(is), eos;
0220     d = from_stream(beg, eos);
0221     return is;
0222   }
0223 #endif // BOOST_NO_STD_ITERATOR_TRAITS
0224 
0225   //! operator>> for gregorian::date_duration
0226   template<class charT>
0227   inline
0228   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, 
0229                                         date_duration& dd)
0230   {
0231     long v;
0232     is >> v;
0233     dd = date_duration(v);
0234     return is;
0235   }
0236 
0237   //! operator>> for gregorian::date_period
0238   template<class charT>
0239   inline
0240   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,
0241                                         date_period& dp)
0242   {
0243     std::basic_string<charT> s;
0244     is >> s;
0245     dp = date_time::from_simple_string_type<date>(s);
0246     return is;
0247   }
0248 
0249   //! generates a locale with the set of gregorian name-strings of type char*
0250   BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, char type);
0251 
0252   //! Returns a pointer to a facet with a default set of names (English)
0253   /* Necessary in the event an exception is thrown from op>> for 
0254    * weekday or month. See comments in those functions for more info */
0255   BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, char>* create_facet_def(char type);
0256 
0257 #ifndef BOOST_NO_STD_WSTRING
0258   //! generates a locale with the set of gregorian name-strings of type wchar_t*
0259   BOOST_DATE_TIME_DECL std::locale generate_locale(std::locale& loc, wchar_t type);
0260   //! Returns a pointer to a facet with a default set of names (English)
0261   /* Necessary in the event an exception is thrown from op>> for 
0262    * weekday or month. See comments in those functions for more info */
0263   BOOST_DATE_TIME_DECL boost::date_time::all_date_names_put<greg_facet_config, wchar_t>* create_facet_def(wchar_t type);
0264 #endif // BOOST_NO_STD_WSTRING
0265 
0266   //! operator>> for gregorian::greg_month - throws exception if invalid month given
0267   template<class charT>
0268   inline
0269   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_month& m) 
0270   {
0271     typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
0272 
0273     std::basic_string<charT> s;
0274     is >> s;
0275     
0276     if(!std::has_facet<facet_def>(is.getloc())) {
0277       std::locale loc = is.getloc();
0278       charT a = '\0';
0279       is.imbue(generate_locale(loc, a));
0280     }
0281 
0282     short num = 0;
0283 
0284     try{
0285       const facet_def& f = std::use_facet<facet_def>(is.getloc());
0286       num = date_time::find_match(f.get_short_month_names(), 
0287                                   f.get_long_month_names(), 
0288                                   (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
0289                                                            // which is needed by find_match
0290     }
0291     /* bad_cast will be thrown if the desired facet is not accessible
0292      * so we can generate the facet. This has the drawback of using english
0293      * names as a default. */
0294     catch(std::bad_cast&){
0295       charT a = '\0';
0296       
0297 #if defined(BOOST_NO_CXX11_SMART_PTR)
0298       
0299       std::auto_ptr< const facet_def > f(create_facet_def(a));
0300       
0301 #else
0302 
0303       std::unique_ptr< const facet_def > f(create_facet_def(a));
0304       
0305 #endif
0306       
0307       num = date_time::find_match(f->get_short_month_names(), 
0308                                   f->get_long_month_names(), 
0309                                   (greg_month::max)(), s); // greg_month spans 1..12, so max returns the array size,
0310                                                            // which is needed by find_match
0311     }
0312     
0313     ++num; // months numbered 1-12
0314     m = greg_month(num); 
0315 
0316     return is;
0317   }
0318 
0319   //! operator>> for gregorian::greg_weekday  - throws exception if invalid weekday given
0320   template<class charT>
0321   inline
0322   std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is,greg_weekday& wd) 
0323   {
0324     typedef boost::date_time::all_date_names_put<greg_facet_config, charT> facet_def;
0325 
0326     std::basic_string<charT> s;
0327     is >> s;
0328 
0329     if(!std::has_facet<facet_def>(is.getloc())) {
0330       std::locale loc = is.getloc();
0331       charT a = '\0';
0332       is.imbue(generate_locale(loc, a));
0333     }
0334 
0335     short num = 0;
0336     try{
0337       const facet_def& f = std::use_facet<facet_def>(is.getloc());
0338       num = date_time::find_match(f.get_short_weekday_names(), 
0339                                   f.get_long_weekday_names(), 
0340                                   (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
0341                                                                  // to form the array size which is needed by find_match
0342     }
0343     /* bad_cast will be thrown if the desired facet is not accessible
0344      * so we can generate the facet. This has the drawback of using english
0345      * names as a default. */
0346     catch(std::bad_cast&){
0347       charT a = '\0';
0348       
0349 #if defined(BOOST_NO_CXX11_SMART_PTR)
0350 
0351       std::auto_ptr< const facet_def > f(create_facet_def(a));
0352       
0353 #else 
0354 
0355       std::unique_ptr< const facet_def > f(create_facet_def(a));
0356       
0357 #endif
0358       
0359       num = date_time::find_match(f->get_short_weekday_names(), 
0360                                   f->get_long_weekday_names(), 
0361                                   (greg_weekday::max)() + 1, s); // greg_weekday spans 0..6, so increment is needed
0362                                                                  // to form the array size which is needed by find_match
0363     }
0364    
0365     wd = greg_weekday(num); // weekdays numbered 0-6
0366     return is;
0367   }
0368 
0369 } } //namespace gregorian
0370 
0371 #endif
0372 
0373 #endif
0374