File indexing completed on 2025-12-16 09:45:13
0001 #ifndef POSIXTIME_FORMATTERS_HPP___
0002 #define POSIXTIME_FORMATTERS_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 #include <boost/date_time/time_parsing.hpp>
0020
0021
0022
0023
0024
0025
0026 namespace boost {
0027
0028 namespace posix_time {
0029
0030
0031
0032 template<class charT>
0033 inline std::basic_string<charT> to_simple_string_type(time_duration td) {
0034 std::basic_ostringstream<charT> ss;
0035 if(td.is_special()) {
0036
0037
0038
0039 switch(td.get_rep().as_special())
0040 {
0041 case not_a_date_time:
0042
0043 ss << "not-a-date-time";
0044 break;
0045 case pos_infin:
0046 ss << "+infinity";
0047 break;
0048 case neg_infin:
0049 ss << "-infinity";
0050 break;
0051 default:
0052 ss << "";
0053 }
0054 }
0055 else {
0056 charT fill_char = '0';
0057 if(td.is_negative()) {
0058 ss << '-';
0059 }
0060 ss << std::setw(2) << std::setfill(fill_char)
0061 << date_time::absolute_value(td.hours()) << ":";
0062 ss << std::setw(2) << std::setfill(fill_char)
0063 << date_time::absolute_value(td.minutes()) << ":";
0064 ss << std::setw(2) << std::setfill(fill_char)
0065 << date_time::absolute_value(td.seconds());
0066
0067 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0068 boost::int64_t frac_sec =
0069 date_time::absolute_value(td.fractional_seconds());
0070
0071 charT buff[32];
0072 _i64toa(frac_sec, buff, 10);
0073 #else
0074 time_duration::fractional_seconds_type frac_sec =
0075 date_time::absolute_value(td.fractional_seconds());
0076 #endif
0077 if (frac_sec != 0) {
0078 ss << "." << std::setw(time_duration::num_fractional_digits())
0079 << std::setfill(fill_char)
0080
0081
0082 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0083 << buff;
0084 #else
0085 << frac_sec;
0086 #endif
0087 }
0088 }
0089 return ss.str();
0090 }
0091
0092
0093
0094 inline std::string to_simple_string(time_duration td) {
0095 return to_simple_string_type<char>(td);
0096 }
0097
0098
0099
0100
0101 template<class charT>
0102 inline std::basic_string<charT> to_iso_string_type(time_duration td)
0103 {
0104 std::basic_ostringstream<charT> ss;
0105 if(td.is_special()) {
0106
0107
0108
0109 switch(td.get_rep().as_special()) {
0110 case not_a_date_time:
0111
0112 ss << "not-a-date-time";
0113 break;
0114 case pos_infin:
0115 ss << "+infinity";
0116 break;
0117 case neg_infin:
0118 ss << "-infinity";
0119 break;
0120 default:
0121 ss << "";
0122 }
0123 }
0124 else {
0125 charT fill_char = '0';
0126 if(td.is_negative()) {
0127 ss << '-';
0128 }
0129 ss << std::setw(2) << std::setfill(fill_char)
0130 << date_time::absolute_value(td.hours());
0131 ss << std::setw(2) << std::setfill(fill_char)
0132 << date_time::absolute_value(td.minutes());
0133 ss << std::setw(2) << std::setfill(fill_char)
0134 << date_time::absolute_value(td.seconds());
0135
0136 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0137 boost::int64_t frac_sec =
0138 date_time::absolute_value(td.fractional_seconds());
0139
0140 charT buff[32];
0141 _i64toa(frac_sec, buff, 10);
0142 #else
0143 time_duration::fractional_seconds_type frac_sec =
0144 date_time::absolute_value(td.fractional_seconds());
0145 #endif
0146 if (frac_sec != 0) {
0147 ss << "." << std::setw(time_duration::num_fractional_digits())
0148 << std::setfill(fill_char)
0149
0150
0151 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0152 << buff;
0153 #else
0154 << frac_sec;
0155 #endif
0156 }
0157 }
0158 return ss.str();
0159 }
0160
0161
0162
0163 inline std::string to_iso_string(time_duration td){
0164 return to_iso_string_type<char>(td);
0165 }
0166
0167
0168
0169
0170 template<class charT>
0171 inline std::basic_string<charT> to_simple_string_type(ptime t)
0172 {
0173
0174 std::basic_string<charT> ts = gregorian::to_simple_string_type<charT>(t.date());
0175 if(!t.time_of_day().is_special()) {
0176 charT space = ' ';
0177 return ts + space + to_simple_string_type<charT>(t.time_of_day());
0178 }
0179 else {
0180 return ts;
0181 }
0182 }
0183 inline std::string to_simple_string(ptime t){
0184 return to_simple_string_type<char>(t);
0185 }
0186
0187
0188
0189 template<class charT>
0190 inline std::basic_string<charT> to_simple_string_type(time_period tp)
0191 {
0192 charT beg = '[', mid = '/', end = ']';
0193 std::basic_string<charT> d1(to_simple_string_type<charT>(tp.begin()));
0194 std::basic_string<charT> d2(to_simple_string_type<charT>(tp.last()));
0195 return std::basic_string<charT>(beg + d1 + mid + d2 + end);
0196 }
0197
0198
0199
0200 inline std::string to_simple_string(time_period tp){
0201 return to_simple_string_type<char>(tp);
0202 }
0203
0204
0205
0206 template<class charT>
0207 inline std::basic_string<charT> to_iso_string_type(ptime t)
0208 {
0209 std::basic_string<charT> ts = gregorian::to_iso_string_type<charT>(t.date());
0210 if(!t.time_of_day().is_special()) {
0211 charT sep = 'T';
0212 return ts + sep + to_iso_string_type<charT>(t.time_of_day());
0213 }
0214 else {
0215 return ts;
0216 }
0217 }
0218
0219
0220
0221 inline std::string to_iso_string(ptime t){
0222 return to_iso_string_type<char>(t);
0223 }
0224
0225
0226
0227
0228 template<class charT>
0229 inline std::basic_string<charT> to_iso_extended_string_type(ptime t)
0230 {
0231 std::basic_string<charT> ts = gregorian::to_iso_extended_string_type<charT>(t.date());
0232 if(!t.time_of_day().is_special()) {
0233 charT sep = 'T';
0234 return ts + sep + to_simple_string_type<charT>(t.time_of_day());
0235 }
0236 else {
0237 return ts;
0238 }
0239 }
0240
0241
0242
0243 inline std::string to_iso_extended_string(ptime t){
0244 return to_iso_extended_string_type<char>(t);
0245 }
0246
0247 #if !defined(BOOST_NO_STD_WSTRING)
0248
0249
0250
0251 inline std::wstring to_simple_wstring(time_duration td) {
0252 return to_simple_string_type<wchar_t>(td);
0253 }
0254
0255
0256
0257 inline std::wstring to_iso_wstring(time_duration td){
0258 return to_iso_string_type<wchar_t>(td);
0259 }
0260 inline std::wstring to_simple_wstring(ptime t){
0261 return to_simple_string_type<wchar_t>(t);
0262 }
0263
0264
0265
0266 inline std::wstring to_simple_wstring(time_period tp){
0267 return to_simple_string_type<wchar_t>(tp);
0268 }
0269
0270
0271
0272 inline std::wstring to_iso_wstring(ptime t){
0273 return to_iso_string_type<wchar_t>(t);
0274 }
0275
0276
0277
0278 inline std::wstring to_iso_extended_wstring(ptime t){
0279 return to_iso_extended_string_type<wchar_t>(t);
0280 }
0281
0282 #endif
0283
0284
0285 } }
0286
0287
0288 #endif
0289