1 #ifndef POSIX_TIME_PRE133_OPERATORS_HPP___ 2 #define POSIX_TIME_PRE133_OPERATORS_HPP___ 3 4 /* Copyright (c) 2002-2004 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$ 10 */ 11 12 /*! @file posix_time_pre133_operators.hpp 13 * These input and output operators are for use with the 14 * pre 1.33 version of the date_time libraries io facet code. 15 * The operators used in version 1.33 and later can be found 16 * in posix_time_io.hpp */ 17 18 #include <iostream> 19 #include <string> 20 #include <sstream> 21 #include "boost/date_time/compiler_config.hpp" 22 #include "boost/date_time/gregorian/gregorian.hpp" 23 #include "boost/date_time/posix_time/posix_time_duration.hpp" 24 #include "boost/date_time/posix_time/ptime.hpp" 25 #include "boost/date_time/posix_time/time_period.hpp" 26 #include "boost/date_time/time_parsing.hpp" 27 28 namespace boost { 29 namespace posix_time { 30 31 32 //The following code is removed for configurations with poor std::locale support (eg: MSVC6, gcc 2.9x) 33 #ifndef BOOST_DATE_TIME_NO_LOCALE 34 #if defined(USE_DATE_TIME_PRE_1_33_FACET_IO) 35 //! ostream operator for posix_time::time_duration 36 template <class charT, class traits> 37 inline 38 std::basic_ostream<charT, traits>& operator <<(std::basic_ostream<charT,traits> & os,const time_duration & td)39 operator<<(std::basic_ostream<charT, traits>& os, const time_duration& td) 40 { 41 typedef boost::date_time::ostream_time_duration_formatter<time_duration, charT> duration_formatter; 42 duration_formatter::duration_put(td, os); 43 return os; 44 } 45 46 //! ostream operator for posix_time::ptime 47 template <class charT, class traits> 48 inline 49 std::basic_ostream<charT, traits>& operator <<(std::basic_ostream<charT,traits> & os,const ptime & t)50 operator<<(std::basic_ostream<charT, traits>& os, const ptime& t) 51 { 52 typedef boost::date_time::ostream_time_formatter<ptime, charT> time_formatter; 53 time_formatter::time_put(t, os); 54 return os; 55 } 56 57 //! ostream operator for posix_time::time_period 58 template <class charT, class traits> 59 inline 60 std::basic_ostream<charT, traits>& operator <<(std::basic_ostream<charT,traits> & os,const time_period & tp)61 operator<<(std::basic_ostream<charT, traits>& os, const time_period& tp) 62 { 63 typedef boost::date_time::ostream_time_period_formatter<time_period, charT> period_formatter; 64 period_formatter::period_put(tp, os); 65 return os; 66 } 67 #endif // USE_DATE_TIME_PRE_1_33_FACET_IO 68 /******** input streaming ********/ 69 template<class charT> 70 inline operator >>(std::basic_istream<charT> & is,time_duration & td)71 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, time_duration& td) 72 { 73 // need to create a std::string and parse it 74 std::basic_string<charT> inp_s; 75 std::stringstream out_ss; 76 is >> inp_s; 77 typename std::basic_string<charT>::iterator b = inp_s.begin(); 78 // need to use both iterators because there is no requirement 79 // for the data held by a std::basic_string<> be terminated with 80 // any marker (such as '\0'). 81 typename std::basic_string<charT>::iterator e = inp_s.end(); 82 while(b != e){ 83 out_ss << is.narrow(*b, 0); 84 ++b; 85 } 86 87 td = date_time::parse_delimited_time_duration<time_duration>(out_ss.str()); 88 return is; 89 } 90 91 template<class charT> 92 inline operator >>(std::basic_istream<charT> & is,ptime & pt)93 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, ptime& pt) 94 { 95 gregorian::date d(not_a_date_time); 96 time_duration td(0,0,0); 97 is >> d >> td; 98 pt = ptime(d, td); 99 100 return is; 101 } 102 103 /** operator>> for time_period. time_period must be in 104 * "[date time_duration/date time_duration]" format. */ 105 template<class charT> 106 inline operator >>(std::basic_istream<charT> & is,time_period & tp)107 std::basic_istream<charT>& operator>>(std::basic_istream<charT>& is, time_period& tp) 108 { 109 gregorian::date d(not_a_date_time); 110 time_duration td(0,0,0); 111 ptime beg(d, td); 112 ptime end(beg); 113 std::basic_string<charT> s; 114 // get first date string and remove leading '[' 115 is >> s; 116 { 117 std::basic_stringstream<charT> ss; 118 ss << s.substr(s.find('[')+1); 119 ss >> d; 120 } 121 // get first time_duration & second date string, remove the '/' 122 // and split into 2 strings 123 is >> s; 124 { 125 std::basic_stringstream<charT> ss; 126 ss << s.substr(0, s.find('/')); 127 ss >> td; 128 } 129 beg = ptime(d, td); 130 { 131 std::basic_stringstream<charT> ss; 132 ss << s.substr(s.find('/')+1); 133 ss >> d; 134 } 135 // get last time_duration and remove the trailing ']' 136 is >> s; 137 { 138 std::basic_stringstream<charT> ss; 139 ss << s.substr(0, s.find(']')); 140 ss >> td; 141 } 142 end = ptime(d, td); 143 144 tp = time_period(beg,end); 145 return is; 146 } 147 148 149 #endif //BOOST_DATE_TIME_NO_LOCALE 150 151 } } // namespaces 152 153 #endif // POSIX_TIME_PRE133_OPERATORS_HPP___ 154