1 #ifndef BOOST_DATE_TIME_LOCAL_TIME_IO_HPP__ 2 #define BOOST_DATE_TIME_LOCAL_TIME_IO_HPP__ 3 4 /* Copyright (c) 2003-2004 CrystalClear Software, Inc. 5 * Subject to the Boost Software License, Version 1.0. 6 * (See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) 7 * Author: Jeff Garland, Bart Garst 8 * $Date$ 9 */ 10 11 #include <string> 12 #include <locale> 13 #include <iostream> 14 #include <iterator> // i/ostreambuf_iterator 15 #include <boost/io/ios_state.hpp> 16 #include <boost/date_time/special_defs.hpp> 17 #include <boost/date_time/time_facet.hpp> 18 #include <boost/date_time/string_convert.hpp> 19 #include <boost/date_time/local_time/local_time_types.hpp> 20 #include <boost/date_time/local_time/local_date_time.hpp> 21 #include <boost/date_time/local_time/posix_time_zone.hpp> 22 #include <boost/date_time/local_time/conversion.hpp> // to_tm will be needed in the facets 23 24 namespace boost { 25 namespace local_time { 26 27 typedef boost::date_time::time_facet<local_date_time, wchar_t> wlocal_time_facet; 28 typedef boost::date_time::time_facet<local_date_time, char> local_time_facet; 29 30 typedef boost::date_time::time_input_facet<local_date_time::utc_time_type,wchar_t> wlocal_time_input_facet; 31 typedef boost::date_time::time_input_facet<local_date_time::utc_time_type,char> local_time_input_facet; 32 33 //! operator<< for local_date_time - see local_time docs for formatting details 34 template<class CharT, class TraitsT> 35 inline 36 std::basic_ostream<CharT, TraitsT>& operator <<(std::basic_ostream<CharT,TraitsT> & os,const local_date_time & ldt)37 operator<<(std::basic_ostream<CharT, TraitsT>& os, const local_date_time& ldt) 38 { 39 boost::io::ios_flags_saver iflags(os); 40 typedef local_date_time time_type;//::utc_time_type typename 41 typedef date_time::time_facet<time_type, CharT> custom_time_facet; 42 std::ostreambuf_iterator<CharT> oitr(os); 43 44 if(std::has_facet<custom_time_facet>(os.getloc())) { 45 std::use_facet<custom_time_facet>(os.getloc()).put(oitr, 46 os, 47 os.fill(), 48 ldt); 49 } 50 else { 51 custom_time_facet* f = new custom_time_facet(); 52 std::locale l = std::locale(os.getloc(), f); 53 os.imbue(l); 54 f->put(oitr, os, os.fill(), ldt); 55 } 56 57 return os; 58 } 59 60 61 //! input operator for local_date_time 62 template <class CharT, class Traits> 63 inline 64 std::basic_istream<CharT, Traits>& operator >>(std::basic_istream<CharT,Traits> & is,local_date_time & ldt)65 operator>>(std::basic_istream<CharT, Traits>& is, local_date_time& ldt) 66 { 67 boost::io::ios_flags_saver iflags(is); 68 typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 69 if (strm_sentry) { 70 try { 71 typedef typename local_date_time::utc_time_type utc_time_type; 72 typedef typename date_time::time_input_facet<utc_time_type, CharT> time_input_facet; 73 74 // intermediate objects 75 std::basic_string<CharT> tz_str; 76 utc_time_type pt(not_a_date_time); 77 78 std::istreambuf_iterator<CharT,Traits> sit(is), str_end; 79 if(std::has_facet<time_input_facet>(is.getloc())) { 80 std::use_facet<time_input_facet>(is.getloc()).get_local_time(sit, str_end, is, pt, tz_str); 81 } 82 else { 83 time_input_facet* f = new time_input_facet(); 84 std::locale l = std::locale(is.getloc(), f); 85 is.imbue(l); 86 f->get_local_time(sit, str_end, is, pt, tz_str); 87 } 88 if(tz_str.empty()) { 89 time_zone_ptr null_ptr; 90 // a null time_zone_ptr creates a local_date_time that is UTC 91 ldt = local_date_time(pt, null_ptr); 92 } 93 else { 94 time_zone_ptr tz_ptr(new posix_time_zone(date_time::convert_string_type<CharT,char>(tz_str))); 95 // the "date & time" constructor expects the time label to *not* be utc. 96 // a posix_tz_string also expects the time label to *not* be utc. 97 ldt = local_date_time(pt.date(), pt.time_of_day(), tz_ptr, local_date_time::EXCEPTION_ON_ERROR); 98 } 99 } 100 catch(...) { 101 // mask tells us what exceptions are turned on 102 std::ios_base::iostate exception_mask = is.exceptions(); 103 // if the user wants exceptions on failbit, we'll rethrow our 104 // date_time exception & set the failbit 105 if(std::ios_base::failbit & exception_mask) { 106 try { is.setstate(std::ios_base::failbit); } 107 catch(std::ios_base::failure&) {} // ignore this one 108 throw; // rethrow original exception 109 } 110 else { 111 // if the user want's to fail quietly, we simply set the failbit 112 is.setstate(std::ios_base::failbit); 113 } 114 115 } 116 } 117 return is; 118 } 119 120 //! output operator for local_time_period 121 template <class CharT, class TraitsT> 122 inline 123 std::basic_ostream<CharT, TraitsT>& operator <<(std::basic_ostream<CharT,TraitsT> & os,const boost::local_time::local_time_period & p)124 operator<<(std::basic_ostream<CharT, TraitsT>& os, 125 const boost::local_time::local_time_period& p) { 126 boost::io::ios_flags_saver iflags(os); 127 typedef boost::date_time::time_facet<local_date_time, CharT> custom_facet; 128 std::ostreambuf_iterator<CharT> oitr(os); 129 if (std::has_facet<custom_facet>(os.getloc())) { 130 std::use_facet<custom_facet>(os.getloc()).put(oitr, os, os.fill(), p); 131 } 132 else { 133 //instantiate a custom facet for dealing with periods since the user 134 //has not put one in the stream so far. This is for efficiency 135 //since we would always need to reconstruct for every time period 136 //if the local did not already exist. Of course this will be overridden 137 //if the user imbues as some later point. 138 custom_facet* f = new custom_facet(); 139 std::locale l = std::locale(os.getloc(), f); 140 os.imbue(l); 141 f->put(oitr, os, os.fill(), p); 142 } 143 return os; 144 } 145 146 //! input operator for local_time_period 147 template <class CharT, class Traits> 148 inline 149 std::basic_istream<CharT, Traits>& operator >>(std::basic_istream<CharT,Traits> & is,boost::local_time::local_time_period & tp)150 operator>>(std::basic_istream<CharT, Traits>& is, boost::local_time::local_time_period& tp) 151 { 152 boost::io::ios_flags_saver iflags(is); 153 typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false); 154 if (strm_sentry) { 155 try { 156 typedef typename date_time::time_input_facet<local_date_time, CharT> time_input_facet; 157 158 std::istreambuf_iterator<CharT,Traits> sit(is), str_end; 159 if(std::has_facet<time_input_facet>(is.getloc())) { 160 std::use_facet<time_input_facet>(is.getloc()).get(sit, str_end, is, tp); 161 } 162 else { 163 time_input_facet* f = new time_input_facet(); 164 std::locale l = std::locale(is.getloc(), f); 165 is.imbue(l); 166 f->get(sit, str_end, is, tp); 167 } 168 } 169 catch(...) { 170 std::ios_base::iostate exception_mask = is.exceptions(); 171 if(std::ios_base::failbit & exception_mask) { 172 try { is.setstate(std::ios_base::failbit); } 173 catch(std::ios_base::failure&) {} 174 throw; // rethrow original exception 175 } 176 else { 177 is.setstate(std::ios_base::failbit); 178 } 179 180 } 181 } 182 return is; 183 } 184 185 } } // namespaces 186 187 #endif // BOOST_DATE_TIME_LOCAL_TIME_IO_HPP__ 188