Back to home page

EIC code displayed by LXR

 
 

    


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

0001 #ifndef DATE_TIME_POSIX_TIME_IO_HPP__
0002 #define DATE_TIME_POSIX_TIME_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/time_facet.hpp>
0017 #include <boost/date_time/period_formatter.hpp>
0018 #include <boost/date_time/posix_time/ptime.hpp>
0019 #include <boost/date_time/posix_time/time_period.hpp>
0020 #include <boost/date_time/posix_time/posix_time_duration.hpp>
0021 #include <boost/date_time/posix_time/conversion.hpp> // to_tm will be needed in the facets
0022 
0023 namespace boost {
0024 namespace posix_time {
0025 
0026 
0027   //! wptime_facet is depricated and will be phased out. use wtime_facet instead
0028   //typedef boost::date_time::time_facet<ptime, wchar_t> wptime_facet;
0029   //! ptime_facet is depricated and will be phased out. use time_facet instead
0030   //typedef boost::date_time::time_facet<ptime, char>     ptime_facet;
0031 
0032   //! wptime_input_facet is depricated and will be phased out. use wtime_input_facet instead
0033   //typedef boost::date_time::time_input_facet<ptime,wchar_t> wptime_input_facet;
0034   //! ptime_input_facet is depricated and will be phased out. use time_input_facet instead
0035   //typedef boost::date_time::time_input_facet<ptime,char>     ptime_input_facet;
0036 
0037   typedef boost::date_time::time_facet<ptime, wchar_t>     wtime_facet;
0038   typedef boost::date_time::time_facet<ptime, char>         time_facet;
0039 
0040   typedef boost::date_time::time_input_facet<ptime, wchar_t>     wtime_input_facet;
0041   typedef boost::date_time::time_input_facet<ptime, char>         time_input_facet;
0042 
0043   template <class CharT, class TraitsT>
0044   inline
0045   std::basic_ostream<CharT, TraitsT>&
0046   operator<<(std::basic_ostream<CharT, TraitsT>& os,
0047              const ptime& p) {
0048     boost::io::ios_flags_saver iflags(os);
0049     typedef boost::date_time::time_facet<ptime, CharT> custom_ptime_facet;
0050     std::ostreambuf_iterator<CharT> oitr(os);
0051     if (std::has_facet<custom_ptime_facet>(os.getloc()))
0052       std::use_facet<custom_ptime_facet>(os.getloc()).put(oitr, os, os.fill(), p);
0053     else {
0054       //instantiate a custom facet for dealing with times since the user
0055       //has not put one in the stream so far.  This is for efficiency 
0056       //since we would always need to reconstruct for every time period
0057       //if the locale did not already exist.  Of course this will be overridden
0058       //if the user imbues as some later point.
0059       custom_ptime_facet* f = new custom_ptime_facet();
0060       std::locale l = std::locale(os.getloc(), f);
0061       os.imbue(l);
0062       f->put(oitr, os, os.fill(), p);
0063     }
0064     return os;
0065   }
0066 
0067   //! input operator for ptime
0068   template <class CharT, class Traits>
0069   inline
0070   std::basic_istream<CharT, Traits>&
0071   operator>>(std::basic_istream<CharT, Traits>& is, ptime& pt)
0072   {
0073     boost::io::ios_flags_saver iflags(is);
0074     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
0075     if (strm_sentry) {
0076       try {
0077         typedef typename date_time::time_input_facet<ptime, CharT> time_input_facet_local;
0078         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0079         if(std::has_facet<time_input_facet_local>(is.getloc())) {
0080           std::use_facet<time_input_facet_local>(is.getloc()).get(sit, str_end, is, pt);
0081         }
0082         else {
0083           time_input_facet_local* f = new time_input_facet_local();
0084           std::locale l = std::locale(is.getloc(), f);
0085           is.imbue(l);
0086           f->get(sit, str_end, is, pt);
0087         }
0088       }
0089       catch(...) {
0090         // mask tells us what exceptions are turned on
0091         std::ios_base::iostate exception_mask = is.exceptions();
0092         // if the user wants exceptions on failbit, we'll rethrow our 
0093         // date_time exception & set the failbit
0094         if(std::ios_base::failbit & exception_mask) {
0095           try { is.setstate(std::ios_base::failbit); }
0096           catch(std::ios_base::failure&) {} // ignore this one
0097           throw; // rethrow original exception
0098         }
0099         else {
0100           // if the user wants to fail quietly, we simply set the failbit
0101           is.setstate(std::ios_base::failbit);
0102         }
0103       }
0104     }
0105     return is;
0106   }
0107 
0108 
0109   template <class CharT, class TraitsT>
0110   inline 
0111   std::basic_ostream<CharT, TraitsT>&
0112   operator<<(std::basic_ostream<CharT, TraitsT>& os,
0113              const boost::posix_time::time_period& p) {
0114     boost::io::ios_flags_saver iflags(os);
0115     typedef boost::date_time::time_facet<ptime, CharT> custom_ptime_facet;
0116     std::ostreambuf_iterator<CharT> oitr(os);
0117     if (std::has_facet<custom_ptime_facet>(os.getloc())) {
0118       std::use_facet<custom_ptime_facet>(os.getloc()).put(oitr, os, os.fill(), p);
0119     }
0120     else {
0121       //instantiate a custom facet for dealing with periods since the user
0122       //has not put one in the stream so far.  This is for efficiency 
0123       //since we would always need to reconstruct for every time period
0124       //if the local did not already exist.  Of course this will be overridden
0125       //if the user imbues as some later point.
0126       custom_ptime_facet* f = new custom_ptime_facet();
0127       std::locale l = std::locale(os.getloc(), f);
0128       os.imbue(l);
0129       f->put(oitr, os, os.fill(), p);
0130     }
0131     return os;
0132   }
0133 
0134   //! input operator for time_period
0135   template <class CharT, class Traits>
0136   inline
0137   std::basic_istream<CharT, Traits>&
0138   operator>>(std::basic_istream<CharT, Traits>& is, time_period& tp)
0139   {
0140     boost::io::ios_flags_saver iflags(is);
0141     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
0142     if (strm_sentry) {
0143       try {
0144         typedef typename date_time::time_input_facet<ptime, CharT> time_input_facet_local;
0145         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0146         if(std::has_facet<time_input_facet_local>(is.getloc())) {
0147           std::use_facet<time_input_facet_local>(is.getloc()).get(sit, str_end, is, tp);
0148         }
0149         else {
0150           time_input_facet_local* f = new time_input_facet_local();
0151           std::locale l = std::locale(is.getloc(), f);
0152           is.imbue(l);
0153           f->get(sit, str_end, is, tp);
0154         }
0155       }
0156       catch(...) {
0157         std::ios_base::iostate exception_mask = is.exceptions();
0158         if(std::ios_base::failbit & exception_mask) {
0159           try { is.setstate(std::ios_base::failbit); }
0160           catch(std::ios_base::failure&) {}
0161           throw; // rethrow original exception
0162         }
0163         else {
0164           is.setstate(std::ios_base::failbit);
0165         }
0166       }
0167     }
0168     return is;
0169   }
0170 
0171 
0172   //! ostream operator for posix_time::time_duration 
0173   //  todo fix to use facet --  place holder for now...
0174   template <class CharT, class Traits>
0175   inline
0176   std::basic_ostream<CharT, Traits>&
0177   operator<<(std::basic_ostream<CharT, Traits>& os, const time_duration& td)
0178   {
0179     boost::io::ios_flags_saver iflags(os);
0180     typedef boost::date_time::time_facet<ptime, CharT> custom_ptime_facet;
0181     std::ostreambuf_iterator<CharT> oitr(os);
0182     if (std::has_facet<custom_ptime_facet>(os.getloc()))
0183       std::use_facet<custom_ptime_facet>(os.getloc()).put(oitr, os, os.fill(), td);
0184     else {
0185       //instantiate a custom facet for dealing with times since the user
0186       //has not put one in the stream so far.  This is for efficiency 
0187       //since we would always need to reconstruct for every time period
0188       //if the locale did not already exist.  Of course this will be overridden
0189       //if the user imbues as some later point.
0190       custom_ptime_facet* f = new custom_ptime_facet();
0191       std::locale l = std::locale(os.getloc(), f);
0192       os.imbue(l);
0193       f->put(oitr, os, os.fill(), td);
0194     }
0195     return os;
0196   }
0197 
0198   //! input operator for time_duration
0199   template <class CharT, class Traits>
0200   inline
0201   std::basic_istream<CharT, Traits>&
0202   operator>>(std::basic_istream<CharT, Traits>& is, time_duration& td)
0203   {
0204     boost::io::ios_flags_saver iflags(is);
0205     typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
0206     if (strm_sentry) {
0207       try {
0208         typedef typename date_time::time_input_facet<ptime, CharT> time_input_facet_local;
0209         std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0210         if(std::has_facet<time_input_facet_local>(is.getloc())) {
0211           std::use_facet<time_input_facet_local>(is.getloc()).get(sit, str_end, is, td);
0212         }
0213         else {
0214           time_input_facet_local* f = new time_input_facet_local();
0215           std::locale l = std::locale(is.getloc(), f);
0216           is.imbue(l);
0217           f->get(sit, str_end, is, td);
0218         }
0219       }
0220       catch(...) {
0221         std::ios_base::iostate exception_mask = is.exceptions();
0222         if(std::ios_base::failbit & exception_mask) {
0223           try { is.setstate(std::ios_base::failbit); }
0224           catch(std::ios_base::failure&) {}
0225           throw; // rethrow original exception
0226         }
0227         else {
0228           is.setstate(std::ios_base::failbit);
0229         }
0230       }
0231     }
0232     return is;
0233   }
0234 
0235 } } // namespaces
0236 #endif // DATE_TIME_POSIX_TIME_IO_HPP__