File indexing completed on 2025-01-18 09:30:35
0001 #ifndef BOOST_DATE_TIME_LOCAL_TIME_IO_HPP__
0002 #define BOOST_DATE_TIME_LOCAL_TIME_IO_HPP__
0003
0004
0005
0006
0007
0008
0009
0010
0011 #include <string>
0012 #include <locale>
0013 #include <iostream>
0014 #include <iterator> // i/ostreambuf_iterator
0015 #include <boost/io/ios_state.hpp>
0016 #include <boost/date_time/special_defs.hpp>
0017 #include <boost/date_time/time_facet.hpp>
0018 #include <boost/date_time/string_convert.hpp>
0019 #include <boost/date_time/local_time/local_time_types.hpp>
0020 #include <boost/date_time/local_time/local_date_time.hpp>
0021 #include <boost/date_time/local_time/posix_time_zone.hpp>
0022 #include <boost/date_time/local_time/conversion.hpp> // to_tm will be needed in the facets
0023
0024 namespace boost {
0025 namespace local_time {
0026
0027 typedef boost::date_time::time_facet<local_date_time, wchar_t> wlocal_time_facet;
0028 typedef boost::date_time::time_facet<local_date_time, char> local_time_facet;
0029
0030 typedef boost::date_time::time_input_facet<local_date_time::utc_time_type,wchar_t> wlocal_time_input_facet;
0031 typedef boost::date_time::time_input_facet<local_date_time::utc_time_type,char> local_time_input_facet;
0032
0033
0034 template<class CharT, class TraitsT>
0035 inline
0036 std::basic_ostream<CharT, TraitsT>&
0037 operator<<(std::basic_ostream<CharT, TraitsT>& os, const local_date_time& ldt)
0038 {
0039 boost::io::ios_flags_saver iflags(os);
0040 typedef local_date_time time_type;
0041 typedef date_time::time_facet<time_type, CharT> custom_time_facet;
0042 std::ostreambuf_iterator<CharT> oitr(os);
0043
0044 if(std::has_facet<custom_time_facet>(os.getloc())) {
0045 std::use_facet<custom_time_facet>(os.getloc()).put(oitr,
0046 os,
0047 os.fill(),
0048 ldt);
0049 }
0050 else {
0051 custom_time_facet* f = new custom_time_facet();
0052 std::locale l = std::locale(os.getloc(), f);
0053 os.imbue(l);
0054 f->put(oitr, os, os.fill(), ldt);
0055 }
0056
0057 return os;
0058 }
0059
0060
0061
0062 template <class CharT, class Traits>
0063 inline
0064 std::basic_istream<CharT, Traits>&
0065 operator>>(std::basic_istream<CharT, Traits>& is, local_date_time& ldt)
0066 {
0067 boost::io::ios_flags_saver iflags(is);
0068 typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
0069 if (strm_sentry) {
0070 try {
0071 typedef typename local_date_time::utc_time_type utc_time_type;
0072 typedef typename date_time::time_input_facet<utc_time_type, CharT> time_input_facet;
0073
0074
0075 std::basic_string<CharT> tz_str;
0076 utc_time_type pt(not_a_date_time);
0077
0078 std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0079 if(std::has_facet<time_input_facet>(is.getloc())) {
0080 std::use_facet<time_input_facet>(is.getloc()).get_local_time(sit, str_end, is, pt, tz_str);
0081 }
0082 else {
0083 time_input_facet* f = new time_input_facet();
0084 std::locale l = std::locale(is.getloc(), f);
0085 is.imbue(l);
0086 f->get_local_time(sit, str_end, is, pt, tz_str);
0087 }
0088 if(tz_str.empty()) {
0089 time_zone_ptr null_ptr;
0090
0091 ldt = local_date_time(pt, null_ptr);
0092 }
0093 else {
0094 time_zone_ptr tz_ptr(new posix_time_zone(date_time::convert_string_type<CharT,char>(tz_str)));
0095
0096
0097 ldt = local_date_time(pt.date(), pt.time_of_day(), tz_ptr, local_date_time::EXCEPTION_ON_ERROR);
0098 }
0099 }
0100 catch(...) {
0101
0102 std::ios_base::iostate exception_mask = is.exceptions();
0103
0104
0105 if(std::ios_base::failbit & exception_mask) {
0106 try { is.setstate(std::ios_base::failbit); }
0107 catch(std::ios_base::failure&) {}
0108 throw;
0109 }
0110 else {
0111
0112 is.setstate(std::ios_base::failbit);
0113 }
0114
0115 }
0116 }
0117 return is;
0118 }
0119
0120
0121 template <class CharT, class TraitsT>
0122 inline
0123 std::basic_ostream<CharT, TraitsT>&
0124 operator<<(std::basic_ostream<CharT, TraitsT>& os,
0125 const boost::local_time::local_time_period& p) {
0126 boost::io::ios_flags_saver iflags(os);
0127 typedef boost::date_time::time_facet<local_date_time, CharT> custom_facet;
0128 std::ostreambuf_iterator<CharT> oitr(os);
0129 if (std::has_facet<custom_facet>(os.getloc())) {
0130 std::use_facet<custom_facet>(os.getloc()).put(oitr, os, os.fill(), p);
0131 }
0132 else {
0133
0134
0135
0136
0137
0138 custom_facet* f = new custom_facet();
0139 std::locale l = std::locale(os.getloc(), f);
0140 os.imbue(l);
0141 f->put(oitr, os, os.fill(), p);
0142 }
0143 return os;
0144 }
0145
0146
0147 template <class CharT, class Traits>
0148 inline
0149 std::basic_istream<CharT, Traits>&
0150 operator>>(std::basic_istream<CharT, Traits>& is, boost::local_time::local_time_period& tp)
0151 {
0152 boost::io::ios_flags_saver iflags(is);
0153 typename std::basic_istream<CharT, Traits>::sentry strm_sentry(is, false);
0154 if (strm_sentry) {
0155 try {
0156 typedef typename date_time::time_input_facet<local_date_time, CharT> time_input_facet;
0157
0158 std::istreambuf_iterator<CharT,Traits> sit(is), str_end;
0159 if(std::has_facet<time_input_facet>(is.getloc())) {
0160 std::use_facet<time_input_facet>(is.getloc()).get(sit, str_end, is, tp);
0161 }
0162 else {
0163 time_input_facet* f = new time_input_facet();
0164 std::locale l = std::locale(is.getloc(), f);
0165 is.imbue(l);
0166 f->get(sit, str_end, is, tp);
0167 }
0168 }
0169 catch(...) {
0170 std::ios_base::iostate exception_mask = is.exceptions();
0171 if(std::ios_base::failbit & exception_mask) {
0172 try { is.setstate(std::ios_base::failbit); }
0173 catch(std::ios_base::failure&) {}
0174 throw;
0175 }
0176 else {
0177 is.setstate(std::ios_base::failbit);
0178 }
0179
0180 }
0181 }
0182 return is;
0183 }
0184
0185 } }
0186
0187 #endif