File indexing completed on 2025-01-18 09:30:36
0001 #ifndef POSIXTIME_FORMATTERS_LIMITED_HPP___
0002 #define POSIXTIME_FORMATTERS_LIMITED_HPP___
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012 #include <boost/date_time/gregorian/gregorian.hpp>
0013 #include <boost/date_time/compiler_config.hpp>
0014 #include <boost/date_time/iso_format.hpp>
0015 #include <boost/date_time/date_format_simple.hpp>
0016 #include <boost/date_time/posix_time/posix_time_types.hpp>
0017 #include <boost/date_time/time_formatting_streams.hpp>
0018 #include <boost/date_time/time_resolution_traits.hpp> // absolute_value
0019
0020 namespace boost {
0021
0022 namespace posix_time {
0023
0024
0025
0026
0027 inline std::string to_simple_string(time_duration td) {
0028 std::ostringstream ss;
0029 if(td.is_special()) {
0030
0031
0032
0033 switch(td.get_rep().as_special())
0034 {
0035 case not_a_date_time:
0036
0037 ss << "not-a-date-time";
0038 break;
0039 case pos_infin:
0040 ss << "+infinity";
0041 break;
0042 case neg_infin:
0043 ss << "-infinity";
0044 break;
0045 default:
0046 ss << "";
0047 }
0048 }
0049 else {
0050 if(td.is_negative()) {
0051 ss << '-';
0052 }
0053 ss << std::setw(2) << std::setfill('0')
0054 << date_time::absolute_value(td.hours()) << ":";
0055 ss << std::setw(2) << std::setfill('0')
0056 << date_time::absolute_value(td.minutes()) << ":";
0057 ss << std::setw(2) << std::setfill('0')
0058 << date_time::absolute_value(td.seconds());
0059
0060 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0061 boost::int64_t frac_sec =
0062 date_time::absolute_value(td.fractional_seconds());
0063
0064 char buff[32];
0065 _i64toa(frac_sec, buff, 10);
0066 #else
0067 time_duration::fractional_seconds_type frac_sec =
0068 date_time::absolute_value(td.fractional_seconds());
0069 #endif
0070 if (frac_sec != 0) {
0071 ss << "." << std::setw(time_duration::num_fractional_digits())
0072 << std::setfill('0')
0073
0074
0075 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0076 << buff;
0077 #else
0078 << frac_sec;
0079 #endif
0080 }
0081 }
0082 return ss.str();
0083 }
0084
0085
0086
0087
0088 inline
0089 std::string
0090 to_iso_string(time_duration td)
0091 {
0092 std::ostringstream ss;
0093 if(td.is_special()) {
0094
0095
0096
0097 switch(td.get_rep().as_special()) {
0098 case not_a_date_time:
0099
0100 ss << "not-a-date-time";
0101 break;
0102 case pos_infin:
0103 ss << "+infinity";
0104 break;
0105 case neg_infin:
0106 ss << "-infinity";
0107 break;
0108 default:
0109 ss << "";
0110 }
0111 }
0112 else {
0113 if(td.is_negative()) {
0114 ss << '-';
0115 }
0116 ss << std::setw(2) << std::setfill('0')
0117 << date_time::absolute_value(td.hours());
0118 ss << std::setw(2) << std::setfill('0')
0119 << date_time::absolute_value(td.minutes());
0120 ss << std::setw(2) << std::setfill('0')
0121 << date_time::absolute_value(td.seconds());
0122
0123 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0124 boost::int64_t frac_sec =
0125 date_time::absolute_value(td.fractional_seconds());
0126
0127 char buff[32];
0128 _i64toa(frac_sec, buff, 10);
0129 #else
0130 time_duration::fractional_seconds_type frac_sec =
0131 date_time::absolute_value(td.fractional_seconds());
0132 #endif
0133 if (frac_sec != 0) {
0134 ss << "." << std::setw(time_duration::num_fractional_digits())
0135 << std::setfill('0')
0136
0137
0138 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0139 << buff;
0140 #else
0141 << frac_sec;
0142 #endif
0143 }
0144 }
0145 return ss.str();
0146 }
0147
0148
0149
0150
0151 inline
0152 std::string
0153 to_simple_string(ptime t)
0154 {
0155 std::string ts = gregorian::to_simple_string(t.date());
0156 if(!t.time_of_day().is_special()) {
0157 return ts + " " + to_simple_string(t.time_of_day());
0158 }
0159 else {
0160 return ts;
0161 }
0162 }
0163
0164
0165
0166
0167 inline
0168 std::string
0169 to_simple_string(time_period tp)
0170 {
0171 std::string d1(to_simple_string(tp.begin()));
0172 std::string d2(to_simple_string(tp.last()));
0173 return std::string("[" + d1 + "/" + d2 +"]");
0174 }
0175
0176
0177
0178
0179 inline
0180 std::string to_iso_string(ptime t)
0181 {
0182 std::string ts = gregorian::to_iso_string(t.date());
0183 if(!t.time_of_day().is_special()) {
0184 return ts + "T" + to_iso_string(t.time_of_day());
0185 }
0186 else {
0187 return ts;
0188 }
0189 }
0190
0191
0192
0193
0194 inline
0195 std::string
0196 to_iso_extended_string(ptime t)
0197 {
0198 std::string ts = gregorian::to_iso_extended_string(t.date());
0199 if(!t.time_of_day().is_special()) {
0200 return ts + "T" + to_simple_string(t.time_of_day());
0201 }
0202 else {
0203 return ts;
0204 }
0205 }
0206
0207
0208 } }
0209
0210
0211 #endif
0212