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
0005
0006
0007
0008
0009
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
0028
0029
0030
0031
0032
0033
0034
0035
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
0055
0056
0057
0058
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
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
0091 std::ios_base::iostate exception_mask = is.exceptions();
0092
0093
0094 if(std::ios_base::failbit & exception_mask) {
0095 try { is.setstate(std::ios_base::failbit); }
0096 catch(std::ios_base::failure&) {}
0097 throw;
0098 }
0099 else {
0100
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
0122
0123
0124
0125
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
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;
0162 }
0163 else {
0164 is.setstate(std::ios_base::failbit);
0165 }
0166 }
0167 }
0168 return is;
0169 }
0170
0171
0172
0173
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
0186
0187
0188
0189
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
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;
0226 }
0227 else {
0228 is.setstate(std::ios_base::failbit);
0229 }
0230 }
0231 }
0232 return is;
0233 }
0234
0235 } }
0236 #endif