Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef DATE_TIME_GREGORIAN_IO_HPP__
0002 #define DATE_TIME_GREGORIAN_IO_HPP__
0003 
0004 /* Copyright (c) 2004-2005 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 <locale>
0013 #include <iostream>
0014 #include <iterator> // i/ostreambuf_iterator
0015 #include <boost/io/ios_state.hpp>
0016 #include <boost/date_time/date_facet.hpp>
0017 #include <boost/date_time/period_parser.hpp>
0018 #include <boost/date_time/period_formatter.hpp>
0019 #include <boost/date_time/special_values_parser.hpp>
0020 #include <boost/date_time/special_values_formatter.hpp>
0021 #include <boost/date_time/gregorian/gregorian_types.hpp>
0022 #include <boost/date_time/gregorian/conversion.hpp> // to_tm will be needed in the facets
0023 
0024 namespace boost {
0025 namespace gregorian {
0026 
0027 
0028   typedef boost::date_time::period_formatter<wchar_t> wperiod_formatter;
0029   typedef boost::date_time::period_formatter<char>    period_formatter;
0030   
0031   typedef boost::date_time::date_facet<date,wchar_t> wdate_facet;
0032   typedef boost::date_time::date_facet<date,char>    date_facet;
0033 
0034   typedef boost::date_time::period_parser<date,char>       period_parser;
0035   typedef boost::date_time::period_parser<date,wchar_t>    wperiod_parser;
0036     
0037   typedef boost::date_time::special_values_formatter<char> special_values_formatter; 
0038   typedef boost::date_time::special_values_formatter<wchar_t> wspecial_values_formatter; 
0039   
0040   typedef boost::date_time::special_values_parser<date,char> special_values_parser; 
0041   typedef boost::date_time::special_values_parser<date,wchar_t> wspecial_values_parser; 
0042   
0043   typedef boost::date_time::date_input_facet<date,char>    date_input_facet;
0044   typedef boost::date_time::date_input_facet<date,wchar_t> wdate_input_facet;
0045 
0046   template <class CharT, class TraitsT>
0047   inline std::basic_ostream<CharT, TraitsT>&
0048   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date& d) {
0049     boost::io::ios_flags_saver iflags(os);
0050     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0051     std::ostreambuf_iterator<CharT> output_itr(os);
0052     if (std::has_facet<custom_date_facet>(os.getloc()))
0053       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), d);
0054     else {
0055       //instantiate a custom facet for dealing with dates since the user
0056       //has not put one in the stream so far.  This is for efficiency 
0057       //since we would always need to reconstruct for every date
0058       //if the locale did not already exist.  Of course this will be overridden
0059       //if the user imbues at some later point.  With the default settings
0060       //for the facet the resulting format will be the same as the
0061       //std::time_facet settings.
0062       custom_date_facet* f = new custom_date_facet();
0063       std::locale l = std::locale(os.getloc(), f);
0064       os.imbue(l);
0065       f->put(output_itr, os, os.fill(), d);
0066     }
0067     return os;
0068   }
0069 
0070   //! input operator for date
0071   template <class CharT, class Traits>
0072   inline
0073   std::basic_istream<CharT, Traits>&
0074   operator>>(std::basic_istream<CharT, Traits>& is, date& d)
0075   {
0076     boost::io::ios_flags_saver iflags(is);
0077     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0078     if (strm_sentry) {
0079       try {
0080         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0081         
0082         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0083         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0084           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, d);
0085         }
0086         else {
0087           date_input_facet_local* f = new date_input_facet_local();
0088           std::locale l = std::locale(is.getloc(), f);
0089           is.imbue(l);
0090           f->get(sit, str_end, is, d);
0091         }
0092       }
0093       catch(...) { 
0094         // mask tells us what exceptions are turned on
0095         std::ios_base::iostate exception_mask = is.exceptions();
0096         // if the user wants exceptions on failbit, we'll rethrow our 
0097         // date_time exception & set the failbit
0098         if(std::ios_base::failbit & exception_mask) {
0099           try { is.setstate(std::ios_base::failbit); } 
0100           catch(std::ios_base::failure&) {} // ignore this one
0101           throw; // rethrow original exception
0102         }
0103         else {
0104           // if the user want's to fail quietly, we simply set the failbit
0105           is.setstate(std::ios_base::failbit); 
0106         } 
0107             
0108       }
0109     }    
0110     return is;
0111   }
0112 
0113   template <class CharT, class TraitsT>
0114   inline std::basic_ostream<CharT, TraitsT>&
0115   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date_duration& dd) {
0116     boost::io::ios_flags_saver iflags(os);
0117     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0118     std::ostreambuf_iterator<CharT> output_itr(os);
0119     if (std::has_facet<custom_date_facet>(os.getloc()))
0120       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), dd);
0121     else {
0122       custom_date_facet* f = new custom_date_facet();
0123       std::locale l = std::locale(os.getloc(), f);
0124       os.imbue(l);
0125       f->put(output_itr, os, os.fill(), dd);
0126 
0127     }
0128     return os;
0129   }
0130 
0131   //! input operator for date_duration
0132   template <class CharT, class Traits>
0133   inline
0134   std::basic_istream<CharT, Traits>&
0135   operator>>(std::basic_istream<CharT, Traits>& is, date_duration& dd)
0136   {
0137     boost::io::ios_flags_saver iflags(is);
0138     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0139     if (strm_sentry) {
0140       try {
0141         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0142         
0143         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0144         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0145           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, dd);
0146         }
0147         else {
0148           date_input_facet_local* f = new date_input_facet_local();
0149           std::locale l = std::locale(is.getloc(), f);
0150           is.imbue(l);
0151           f->get(sit, str_end, is, dd);
0152         }
0153       }
0154       catch(...) { 
0155         std::ios_base::iostate exception_mask = is.exceptions();
0156         if(std::ios_base::failbit & exception_mask) {
0157           try { is.setstate(std::ios_base::failbit); } 
0158           catch(std::ios_base::failure&) {}
0159           throw; // rethrow original exception
0160         }
0161         else {
0162           is.setstate(std::ios_base::failbit); 
0163         } 
0164             
0165       }
0166     }
0167     return is;
0168   }
0169 
0170   template <class CharT, class TraitsT>
0171   inline std::basic_ostream<CharT, TraitsT>&
0172   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::date_period& dp) {
0173     boost::io::ios_flags_saver iflags(os);
0174     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0175     std::ostreambuf_iterator<CharT> output_itr(os);
0176     if (std::has_facet<custom_date_facet>(os.getloc()))
0177       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), dp);
0178     else {
0179       //instantiate a custom facet for dealing with date periods since the user
0180       //has not put one in the stream so far.  This is for efficiency 
0181       //since we would always need to reconstruct for every time period
0182       //if the local did not already exist.  Of course this will be overridden
0183       //if the user imbues at some later point.  With the default settings
0184       //for the facet the resulting format will be the same as the
0185       //std::time_facet settings.
0186       custom_date_facet* f = new custom_date_facet();
0187       std::locale l = std::locale(os.getloc(), f);
0188       os.imbue(l);
0189       f->put(output_itr, os, os.fill(), dp);
0190 
0191     }
0192     return os;
0193   }
0194 
0195   //! input operator for date_period 
0196   template <class CharT, class Traits>
0197   inline
0198   std::basic_istream<CharT, Traits>&
0199   operator>>(std::basic_istream<CharT, Traits>& is, date_period& dp)
0200   {
0201     boost::io::ios_flags_saver iflags(is);
0202     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0203     if (strm_sentry) {
0204       try {
0205         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0206 
0207         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0208         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0209           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, dp);
0210         }
0211         else {
0212           date_input_facet_local* f = new date_input_facet_local();
0213           std::locale l = std::locale(is.getloc(), f);
0214           is.imbue(l);
0215           f->get(sit, str_end, is, dp);
0216         }
0217       }
0218       catch(...) { 
0219         std::ios_base::iostate exception_mask = is.exceptions();
0220         if(std::ios_base::failbit & exception_mask) {
0221           try { is.setstate(std::ios_base::failbit); } 
0222           catch(std::ios_base::failure&) {}
0223           throw; // rethrow original exception
0224         }
0225         else {
0226           is.setstate(std::ios_base::failbit); 
0227         } 
0228             
0229       }
0230     }
0231     return is;
0232   }
0233 
0234   /********** small gregorian types **********/
0235   
0236   template <class CharT, class TraitsT>
0237   inline std::basic_ostream<CharT, TraitsT>&
0238   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::greg_month& gm) {
0239     boost::io::ios_flags_saver iflags(os);
0240     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0241     std::ostreambuf_iterator<CharT> output_itr(os);
0242     if (std::has_facet<custom_date_facet>(os.getloc()))
0243       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), gm);
0244     else {
0245       custom_date_facet* f = new custom_date_facet();//-> 10/1074199752/32 because year & day not initialized in put(...)
0246       //custom_date_facet* f = new custom_date_facet("%B");
0247       std::locale l = std::locale(os.getloc(), f);
0248       os.imbue(l);
0249       f->put(output_itr, os, os.fill(), gm);
0250     }
0251     return os;
0252   }
0253 
0254   //! input operator for greg_month
0255   template <class CharT, class Traits>
0256   inline
0257   std::basic_istream<CharT, Traits>&
0258   operator>>(std::basic_istream<CharT, Traits>& is, greg_month& m)
0259   {
0260     boost::io::ios_flags_saver iflags(is);
0261     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0262     if (strm_sentry) {
0263       try {
0264         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0265 
0266         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0267         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0268           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, m);
0269         }
0270         else {
0271           date_input_facet_local* f = new date_input_facet_local();
0272           std::locale l = std::locale(is.getloc(), f);
0273           is.imbue(l);
0274           f->get(sit, str_end, is, m);
0275         }
0276       }
0277       catch(...) { 
0278         std::ios_base::iostate exception_mask = is.exceptions();
0279         if(std::ios_base::failbit & exception_mask) {
0280           try { is.setstate(std::ios_base::failbit); } 
0281           catch(std::ios_base::failure&) {}
0282           throw; // rethrow original exception
0283         }
0284         else {
0285           is.setstate(std::ios_base::failbit); 
0286         } 
0287             
0288       }
0289     }
0290     return is;
0291   }
0292 
0293 
0294   template <class CharT, class TraitsT>
0295   inline std::basic_ostream<CharT, TraitsT>&
0296   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::greg_weekday& gw) {
0297     boost::io::ios_flags_saver iflags(os);
0298     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0299     std::ostreambuf_iterator<CharT> output_itr(os);
0300     if (std::has_facet<custom_date_facet>(os.getloc()))
0301       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), gw);
0302     else {
0303       custom_date_facet* f = new custom_date_facet();
0304       std::locale l = std::locale(os.getloc(), f);
0305       os.imbue(l);
0306       f->put(output_itr, os, os.fill(), gw);
0307     }
0308     return os;
0309   }
0310  
0311   //! input operator for greg_weekday
0312   template <class CharT, class Traits>
0313   inline
0314   std::basic_istream<CharT, Traits>&
0315   operator>>(std::basic_istream<CharT, Traits>& is, greg_weekday& wd)
0316   {
0317     boost::io::ios_flags_saver iflags(is);
0318     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0319     if (strm_sentry) {
0320       try {
0321         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0322 
0323         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0324         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0325           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, wd);
0326         }
0327         else {
0328           date_input_facet_local* f = new date_input_facet_local();
0329           std::locale l = std::locale(is.getloc(), f);
0330           is.imbue(l);
0331           f->get(sit, str_end, is, wd);
0332         }
0333       }
0334       catch(...) { 
0335         std::ios_base::iostate exception_mask = is.exceptions();
0336         if(std::ios_base::failbit & exception_mask) {
0337           try { is.setstate(std::ios_base::failbit); } 
0338           catch(std::ios_base::failure&) {}
0339           throw; // rethrow original exception
0340         }
0341         else {
0342           is.setstate(std::ios_base::failbit); 
0343         } 
0344             
0345       }
0346     }
0347     return is;
0348   }
0349 
0350   //NOTE: output operator for greg_day was not necessary
0351 
0352   //! input operator for greg_day
0353   template <class CharT, class Traits>
0354   inline
0355   std::basic_istream<CharT, Traits>&
0356   operator>>(std::basic_istream<CharT, Traits>& is, greg_day& gd)
0357   {
0358     boost::io::ios_flags_saver iflags(is);
0359     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0360     if (strm_sentry) {
0361       try {
0362         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0363 
0364         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0365         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0366           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, gd);
0367         }
0368         else {
0369           date_input_facet_local* f = new date_input_facet_local();
0370           std::locale l = std::locale(is.getloc(), f);
0371           is.imbue(l);
0372           f->get(sit, str_end, is, gd);
0373         }
0374       }
0375       catch(...) { 
0376         std::ios_base::iostate exception_mask = is.exceptions();
0377         if(std::ios_base::failbit & exception_mask) {
0378           try { is.setstate(std::ios_base::failbit); } 
0379           catch(std::ios_base::failure&) {}
0380           throw; // rethrow original exception
0381         }
0382         else {
0383           is.setstate(std::ios_base::failbit); 
0384         } 
0385             
0386       }
0387     }
0388     return is;
0389   }
0390 
0391   //NOTE: output operator for greg_year was not necessary
0392 
0393   //! input operator for greg_year
0394   template <class CharT, class Traits>
0395   inline
0396   std::basic_istream<CharT, Traits>&
0397   operator>>(std::basic_istream<CharT, Traits>& is, greg_year& gy)
0398   {
0399     boost::io::ios_flags_saver iflags(is);
0400     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0401     if (strm_sentry) {
0402       try {
0403         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0404 
0405         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0406         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0407           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, gy);
0408         }
0409         else {
0410           date_input_facet_local* f = new date_input_facet_local();
0411           std::locale l = std::locale(is.getloc(), f);
0412           is.imbue(l);
0413           f->get(sit, str_end, is, gy);
0414         }
0415       }
0416       catch(...) { 
0417         std::ios_base::iostate exception_mask = is.exceptions();
0418         if(std::ios_base::failbit & exception_mask) {
0419           try { is.setstate(std::ios_base::failbit); } 
0420           catch(std::ios_base::failure&) {}
0421           throw; // rethrow original exception
0422         }
0423         else {
0424           is.setstate(std::ios_base::failbit); 
0425         } 
0426             
0427       }
0428     }
0429     return is;
0430   }
0431 
0432   /********** date generator types **********/
0433   
0434   template <class CharT, class TraitsT>
0435   inline std::basic_ostream<CharT, TraitsT>&
0436   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::partial_date& pd) {
0437     boost::io::ios_flags_saver iflags(os);
0438     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0439     std::ostreambuf_iterator<CharT> output_itr(os);
0440     if (std::has_facet<custom_date_facet>(os.getloc()))
0441       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), pd);
0442     else {
0443       custom_date_facet* f = new custom_date_facet();
0444       std::locale l = std::locale(os.getloc(), f);
0445       os.imbue(l);
0446       f->put(output_itr, os, os.fill(), pd);
0447     }
0448     return os;
0449   }
0450 
0451   //! input operator for partial_date
0452   template <class CharT, class Traits>
0453   inline
0454   std::basic_istream<CharT, Traits>&
0455   operator>>(std::basic_istream<CharT, Traits>& is, partial_date& pd)
0456   {
0457     boost::io::ios_flags_saver iflags(is);
0458     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0459     if (strm_sentry) {
0460       try {
0461         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0462 
0463         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0464         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0465           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, pd);
0466         }
0467         else {
0468           date_input_facet_local* f = new date_input_facet_local();
0469           std::locale l = std::locale(is.getloc(), f);
0470           is.imbue(l);
0471           f->get(sit, str_end, is, pd);
0472         }
0473       }
0474       catch(...) { 
0475         std::ios_base::iostate exception_mask = is.exceptions();
0476         if(std::ios_base::failbit & exception_mask) {
0477           try { is.setstate(std::ios_base::failbit); } 
0478           catch(std::ios_base::failure&) {}
0479           throw; // rethrow original exception
0480         }
0481         else {
0482           is.setstate(std::ios_base::failbit); 
0483         } 
0484             
0485       }
0486     }
0487     return is;
0488   }
0489 
0490   template <class CharT, class TraitsT>
0491   inline std::basic_ostream<CharT, TraitsT>&
0492   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::nth_day_of_the_week_in_month& nkd) {
0493     boost::io::ios_flags_saver iflags(os);
0494     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0495     std::ostreambuf_iterator<CharT> output_itr(os);
0496     if (std::has_facet<custom_date_facet>(os.getloc()))
0497       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), nkd);
0498     else {
0499       custom_date_facet* f = new custom_date_facet();
0500       std::locale l = std::locale(os.getloc(), f);
0501       os.imbue(l);
0502       f->put(output_itr, os, os.fill(), nkd);
0503     }
0504     return os;
0505   }
0506 
0507   //! input operator for nth_day_of_the_week_in_month
0508   template <class CharT, class Traits>
0509   inline
0510   std::basic_istream<CharT, Traits>&
0511   operator>>(std::basic_istream<CharT, Traits>& is, 
0512              nth_day_of_the_week_in_month& nday)
0513   {
0514     boost::io::ios_flags_saver iflags(is);
0515     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0516     if (strm_sentry) {
0517       try {
0518         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0519 
0520         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0521         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0522           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, nday);
0523         }
0524         else {
0525           date_input_facet_local* f = new date_input_facet_local();
0526           std::locale l = std::locale(is.getloc(), f);
0527           is.imbue(l);
0528           f->get(sit, str_end, is, nday);
0529         }
0530       }
0531       catch(...) { 
0532         std::ios_base::iostate exception_mask = is.exceptions();
0533         if(std::ios_base::failbit & exception_mask) {
0534           try { is.setstate(std::ios_base::failbit); } 
0535           catch(std::ios_base::failure&) {}
0536           throw; // rethrow original exception
0537         }
0538         else {
0539           is.setstate(std::ios_base::failbit); 
0540         } 
0541             
0542       }
0543     }
0544     return is;
0545   }
0546 
0547 
0548   template <class CharT, class TraitsT>
0549   inline std::basic_ostream<CharT, TraitsT>&
0550   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::first_day_of_the_week_in_month& fkd) {
0551     boost::io::ios_flags_saver iflags(os);
0552     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0553     std::ostreambuf_iterator<CharT> output_itr(os);
0554     if (std::has_facet<custom_date_facet>(os.getloc()))
0555       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), fkd);
0556     else {
0557       custom_date_facet* f = new custom_date_facet();
0558       std::locale l = std::locale(os.getloc(), f);
0559       os.imbue(l);
0560       f->put(output_itr, os, os.fill(), fkd);
0561     }
0562     return os;
0563   }
0564 
0565   //! input operator for first_day_of_the_week_in_month
0566   template <class CharT, class Traits>
0567   inline
0568   std::basic_istream<CharT, Traits>&
0569   operator>>(std::basic_istream<CharT, Traits>& is, 
0570              first_day_of_the_week_in_month& fkd)
0571   {
0572     boost::io::ios_flags_saver iflags(is);
0573     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0574     if (strm_sentry) {
0575       try {
0576         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0577 
0578         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0579         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0580           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, fkd);
0581         }
0582         else {
0583           date_input_facet_local* f = new date_input_facet_local();
0584           std::locale l = std::locale(is.getloc(), f);
0585           is.imbue(l);
0586           f->get(sit, str_end, is, fkd);
0587         }
0588       }
0589       catch(...) { 
0590         std::ios_base::iostate exception_mask = is.exceptions();
0591         if(std::ios_base::failbit & exception_mask) {
0592           try { is.setstate(std::ios_base::failbit); } 
0593           catch(std::ios_base::failure&) {}
0594           throw; // rethrow original exception
0595         }
0596         else {
0597           is.setstate(std::ios_base::failbit); 
0598         } 
0599             
0600       }
0601     }
0602     return is;
0603   }
0604 
0605 
0606   template <class CharT, class TraitsT>
0607   inline std::basic_ostream<CharT, TraitsT>&
0608   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::last_day_of_the_week_in_month& lkd) {
0609     boost::io::ios_flags_saver iflags(os);
0610     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0611     std::ostreambuf_iterator<CharT> output_itr(os);
0612     if (std::has_facet<custom_date_facet>(os.getloc()))
0613       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), lkd);
0614     else {
0615       custom_date_facet* f = new custom_date_facet();
0616       std::locale l = std::locale(os.getloc(), f);
0617       os.imbue(l);
0618       f->put(output_itr, os, os.fill(), lkd);
0619     }
0620     return os;
0621   }
0622 
0623   //! input operator for last_day_of_the_week_in_month
0624   template <class CharT, class Traits>
0625   inline
0626   std::basic_istream<CharT, Traits>&
0627   operator>>(std::basic_istream<CharT, Traits>& is, 
0628              last_day_of_the_week_in_month& lkd)
0629   {
0630     boost::io::ios_flags_saver iflags(is);
0631     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0632     if (strm_sentry) {
0633       try {
0634         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0635 
0636         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0637         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0638           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, lkd);
0639         }
0640         else {
0641           date_input_facet_local* f = new date_input_facet_local();
0642           std::locale l = std::locale(is.getloc(), f);
0643           is.imbue(l);
0644           f->get(sit, str_end, is, lkd);
0645         }
0646       }
0647       catch(...) { 
0648         std::ios_base::iostate exception_mask = is.exceptions();
0649         if(std::ios_base::failbit & exception_mask) {
0650           try { is.setstate(std::ios_base::failbit); } 
0651           catch(std::ios_base::failure&) {}
0652           throw; // rethrow original exception
0653         }
0654         else {
0655           is.setstate(std::ios_base::failbit); 
0656         } 
0657             
0658       }
0659     }
0660     return is;
0661   }
0662 
0663 
0664   template <class CharT, class TraitsT>
0665   inline std::basic_ostream<CharT, TraitsT>&
0666   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::first_day_of_the_week_after& fda) {
0667     boost::io::ios_flags_saver iflags(os);
0668     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0669     std::ostreambuf_iterator<CharT> output_itr(os);
0670     if (std::has_facet<custom_date_facet>(os.getloc())) {
0671       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), fda);
0672     } 
0673     else {
0674       custom_date_facet* f = new custom_date_facet();
0675       std::locale l = std::locale(os.getloc(), f);
0676       os.imbue(l);
0677       f->put(output_itr, os, os.fill(), fda);
0678     }
0679     return os;
0680   }
0681 
0682   //! input operator for first_day_of_the_week_after
0683   template <class CharT, class Traits>
0684   inline
0685   std::basic_istream<CharT, Traits>&
0686   operator>>(std::basic_istream<CharT, Traits>& is, 
0687              first_day_of_the_week_after& fka)
0688   {
0689     boost::io::ios_flags_saver iflags(is);
0690     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0691     if (strm_sentry) {
0692       try {
0693         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0694 
0695         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0696         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0697           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, fka);
0698         }
0699         else {
0700           date_input_facet_local* f = new date_input_facet_local();
0701           std::locale l = std::locale(is.getloc(), f);
0702           is.imbue(l);
0703           f->get(sit, str_end, is, fka);
0704         }
0705       }
0706       catch(...) { 
0707         std::ios_base::iostate exception_mask = is.exceptions();
0708         if(std::ios_base::failbit & exception_mask) {
0709           try { is.setstate(std::ios_base::failbit); } 
0710           catch(std::ios_base::failure&) {}
0711           throw; // rethrow original exception
0712         }
0713         else {
0714           is.setstate(std::ios_base::failbit); 
0715         } 
0716             
0717       }
0718     }
0719     return is;
0720   }
0721 
0722 
0723   template <class CharT, class TraitsT>
0724   inline std::basic_ostream<CharT, TraitsT>&
0725   operator<<(std::basic_ostream<CharT, TraitsT>& os, const boost::gregorian::first_day_of_the_week_before& fdb) {
0726     boost::io::ios_flags_saver iflags(os);
0727     typedef boost::date_time::date_facet<date, CharT> custom_date_facet;
0728     std::ostreambuf_iterator<CharT> output_itr(os);
0729     if (std::has_facet<custom_date_facet>(os.getloc())) {
0730       std::use_facet<custom_date_facet>(os.getloc()).put(output_itr, os, os.fill(), fdb);
0731     }
0732     else {
0733       custom_date_facet* f = new custom_date_facet();
0734       std::locale l = std::locale(os.getloc(), f);
0735       os.imbue(l);
0736       f->put(output_itr, os, os.fill(), fdb);
0737     }
0738     return os;
0739   }
0740 
0741   //! input operator for first_day_of_the_week_before
0742   template <class CharT, class Traits>
0743   inline
0744   std::basic_istream<CharT, Traits>&
0745   operator>>(std::basic_istream<CharT, Traits>& is, 
0746              first_day_of_the_week_before& fkb)
0747   {
0748     boost::io::ios_flags_saver iflags(is);
0749     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 
0750     if (strm_sentry) {
0751       try {
0752         typedef typename date_time::date_input_facet<date, CharT> date_input_facet_local;
0753 
0754         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0755         if(std::has_facet<date_input_facet_local>(is.getloc())) {
0756           std::use_facet<date_input_facet_local>(is.getloc()).get(sit, str_end, is, fkb);
0757         }
0758         else {
0759           date_input_facet_local* f = new date_input_facet_local();
0760           std::locale l = std::locale(is.getloc(), f);
0761           is.imbue(l);
0762           f->get(sit, str_end, is, fkb);
0763         }
0764       }
0765       catch(...) { 
0766         std::ios_base::iostate exception_mask = is.exceptions();
0767         if(std::ios_base::failbit & exception_mask) {
0768           try { is.setstate(std::ios_base::failbit); } 
0769           catch(std::ios_base::failure&) {}
0770           throw; // rethrow original exception
0771         }
0772         else {
0773           is.setstate(std::ios_base::failbit); 
0774         } 
0775             
0776       }
0777     }
0778     return is;
0779   }
0780 
0781   
0782 } } // namespaces
0783 
0784 #endif // DATE_TIME_GREGORIAN_IO_HPP__