[857] | 1 | #ifndef POSIXTIME_FORMATTERS_HPP___
|
---|
| 2 | #define POSIXTIME_FORMATTERS_HPP___
|
---|
| 3 |
|
---|
| 4 | /* Copyright (c) 2002-2004 CrystalClear Software, Inc.
|
---|
| 5 | * Use, modification and distribution is subject to the
|
---|
| 6 | * Boost Software License, Version 1.0. (See accompanying
|
---|
| 7 | * file LICENSE-1.0 or http://www.boost.org/LICENSE-1.0)
|
---|
| 8 | * Author: Jeff Garland, Bart Garst
|
---|
| 9 | * $Date: 2005/01/30 20:58:52 $
|
---|
| 10 | */
|
---|
| 11 |
|
---|
| 12 | #include "boost/date_time/gregorian/gregorian.hpp"
|
---|
| 13 | #include "boost/date_time/compiler_config.hpp"
|
---|
| 14 | #include "boost/date_time/iso_format.hpp"
|
---|
| 15 | #include "boost/date_time/date_format_simple.hpp"
|
---|
| 16 | #include "boost/date_time/posix_time/posix_time_types.hpp"
|
---|
| 17 | #include "boost/date_time/time_formatting_streams.hpp"
|
---|
| 18 |
|
---|
| 19 | #include "boost/date_time/time_parsing.hpp"
|
---|
| 20 |
|
---|
| 21 | /* NOTE: The "to_*_string" code for older compilers, ones that define
|
---|
| 22 | * BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS, is located in
|
---|
| 23 | * formatters_limited.hpp
|
---|
| 24 | */
|
---|
| 25 |
|
---|
| 26 | namespace boost {
|
---|
| 27 |
|
---|
| 28 | namespace posix_time {
|
---|
| 29 |
|
---|
| 30 | // template function called by wrapper functions:
|
---|
| 31 | // to_*_string(time_duration) & to_*_wstring(time_duration)
|
---|
| 32 | template<class charT>
|
---|
| 33 | inline std::basic_string<charT> to_simple_string_type(time_duration td) {
|
---|
| 34 | std::basic_ostringstream<charT> ss;
|
---|
| 35 | if(td.is_special()) {
|
---|
| 36 | /* simply using 'ss << td.get_rep()' won't work on compilers
|
---|
| 37 | * that don't support locales. This way does. */
|
---|
| 38 | // switch copied from date_names_put.hpp
|
---|
| 39 | switch(td.get_rep().as_special())
|
---|
| 40 | {
|
---|
| 41 | case not_a_date_time:
|
---|
| 42 | //ss << "not-a-number";
|
---|
| 43 | ss << "not-a-date-time";
|
---|
| 44 | break;
|
---|
| 45 | case pos_infin:
|
---|
| 46 | ss << "+infinity";
|
---|
| 47 | break;
|
---|
| 48 | case neg_infin:
|
---|
| 49 | ss << "-infinity";
|
---|
| 50 | break;
|
---|
| 51 | default:
|
---|
| 52 | ss << "";
|
---|
| 53 | }
|
---|
| 54 | }
|
---|
| 55 | else {
|
---|
| 56 | charT fill_char = '0';
|
---|
| 57 | if(td.is_negative()) {
|
---|
| 58 | ss << '-';
|
---|
| 59 | }
|
---|
| 60 | ss << std::setw(2) << std::setfill(fill_char)
|
---|
| 61 | << date_time::absolute_value(td.hours()) << ":";
|
---|
| 62 | ss << std::setw(2) << std::setfill(fill_char)
|
---|
| 63 | << date_time::absolute_value(td.minutes()) << ":";
|
---|
| 64 | ss << std::setw(2) << std::setfill(fill_char)
|
---|
| 65 | << date_time::absolute_value(td.seconds());
|
---|
| 66 | //TODO the following is totally non-generic, yelling FIXME
|
---|
| 67 | #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0
|
---|
| 68 | boost::int64_t frac_sec =
|
---|
| 69 | date_time::absolute_value(td.fractional_seconds());
|
---|
| 70 | // JDG [7/6/02 VC++ compatibility]
|
---|
| 71 | charT buff[32];
|
---|
| 72 | _i64toa(frac_sec, buff, 10);
|
---|
| 73 | #else
|
---|
| 74 | time_duration::fractional_seconds_type frac_sec =
|
---|
| 75 | date_time::absolute_value(td.fractional_seconds());
|
---|
| 76 | #endif
|
---|
| 77 | if (frac_sec != 0) {
|
---|
| 78 | ss << "." << std::setw(time_duration::num_fractional_digits())
|
---|
| 79 | << std::setfill(fill_char)
|
---|
| 80 |
|
---|
| 81 | // JDG [7/6/02 VC++ compatibility]
|
---|
| 82 | #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0
|
---|
| 83 | << buff;
|
---|
| 84 | #else
|
---|
| 85 | << frac_sec;
|
---|
| 86 | #endif
|
---|
| 87 | }
|
---|
| 88 | }// else
|
---|
| 89 | return ss.str();
|
---|
| 90 | }
|
---|
| 91 | //! Time duration to string -hh::mm::ss.fffffff. Example: 10:09:03.0123456
|
---|
| 92 | /*!\ingroup time_format
|
---|
| 93 | */
|
---|
| 94 | inline std::string to_simple_string(time_duration td) {
|
---|
| 95 | return to_simple_string_type<char>(td);
|
---|
| 96 | }
|
---|
| 97 |
|
---|
| 98 |
|
---|
| 99 | // template function called by wrapper functions:
|
---|
| 100 | // to_*_string(time_duration) & to_*_wstring(time_duration)
|
---|
| 101 | template<class charT>
|
---|
| 102 | inline std::basic_string<charT> to_iso_string_type(time_duration td)
|
---|
| 103 | {
|
---|
| 104 | std::basic_ostringstream<charT> ss;
|
---|
| 105 | if(td.is_special()) {
|
---|
| 106 | /* simply using 'ss << td.get_rep()' won't work on compilers
|
---|
| 107 | * that don't support locales. This way does. */
|
---|
| 108 | // switch copied from date_names_put.hpp
|
---|
| 109 | switch(td.get_rep().as_special()) {
|
---|
| 110 | case not_a_date_time:
|
---|
| 111 | //ss << "not-a-number";
|
---|
| 112 | ss << "not-a-date-time";
|
---|
| 113 | break;
|
---|
| 114 | case pos_infin:
|
---|
| 115 | ss << "+infinity";
|
---|
| 116 | break;
|
---|
| 117 | case neg_infin:
|
---|
| 118 | ss << "-infinity";
|
---|
| 119 | break;
|
---|
| 120 | default:
|
---|
| 121 | ss << "";
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | else {
|
---|
| 125 | charT fill_char = '0';
|
---|
| 126 | if(td.is_negative()) {
|
---|
| 127 | ss << '-';
|
---|
| 128 | }
|
---|
| 129 | ss << std::setw(2) << std::setfill(fill_char)
|
---|
| 130 | << date_time::absolute_value(td.hours());
|
---|
| 131 | ss << std::setw(2) << std::setfill(fill_char)
|
---|
| 132 | << date_time::absolute_value(td.minutes());
|
---|
| 133 | ss << std::setw(2) << std::setfill(fill_char)
|
---|
| 134 | << date_time::absolute_value(td.seconds());
|
---|
| 135 | //TODO the following is totally non-generic, yelling FIXME
|
---|
| 136 | #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0
|
---|
| 137 | boost::int64_t frac_sec =
|
---|
| 138 | date_time::absolute_value(td.fractional_seconds());
|
---|
| 139 | // JDG [7/6/02 VC++ compatibility]
|
---|
| 140 | charT buff[32];
|
---|
| 141 | _i64toa(frac_sec, buff, 10);
|
---|
| 142 | #else
|
---|
| 143 | time_duration::fractional_seconds_type frac_sec =
|
---|
| 144 | date_time::absolute_value(td.fractional_seconds());
|
---|
| 145 | #endif
|
---|
| 146 | if (frac_sec != 0) {
|
---|
| 147 | ss << "." << std::setw(time_duration::num_fractional_digits())
|
---|
| 148 | << std::setfill(fill_char)
|
---|
| 149 |
|
---|
| 150 | // JDG [7/6/02 VC++ compatibility]
|
---|
| 151 | #if (defined(BOOST_MSVC) && (_MSC_VER <= 1200)) // 1200 == VC++ 6.0
|
---|
| 152 | << buff;
|
---|
| 153 | #else
|
---|
| 154 | << frac_sec;
|
---|
| 155 | #endif
|
---|
| 156 | }
|
---|
| 157 | }// else
|
---|
| 158 | return ss.str();
|
---|
| 159 | }
|
---|
| 160 | //! Time duration in iso format -hhmmss,fffffff Example: 10:09:03,0123456
|
---|
| 161 | /*!\ingroup time_format
|
---|
| 162 | */
|
---|
| 163 | inline std::string to_iso_string(time_duration td){
|
---|
| 164 | return to_iso_string_type<char>(td);
|
---|
| 165 | }
|
---|
| 166 |
|
---|
| 167 | //! Time to simple format CCYY-mmm-dd hh:mm:ss.fffffff
|
---|
| 168 | /*!\ingroup time_format
|
---|
| 169 | */
|
---|
| 170 | template<class charT>
|
---|
| 171 | inline std::basic_string<charT> to_simple_string_type(ptime t)
|
---|
| 172 | {
|
---|
| 173 | // can't use this w/gcc295, no to_simple_string_type<>(td) available
|
---|
| 174 | std::basic_string<charT> ts = gregorian::to_simple_string_type<charT>(t.date());// + " ";
|
---|
| 175 | if(!t.time_of_day().is_special()) {
|
---|
| 176 | charT space = ' ';
|
---|
| 177 | return ts + space + to_simple_string_type<charT>(t.time_of_day());
|
---|
| 178 | }
|
---|
| 179 | else {
|
---|
| 180 | return ts;
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | inline std::string to_simple_string(ptime t){
|
---|
| 184 | return to_simple_string_type<char>(t);
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | // function called by wrapper functions to_*_string(time_period)
|
---|
| 188 | // & to_*_wstring(time_period)
|
---|
| 189 | template<class charT>
|
---|
| 190 | inline std::basic_string<charT> to_simple_string_type(time_period tp)
|
---|
| 191 | {
|
---|
| 192 | charT beg = '[', mid = '/', end = ']';
|
---|
| 193 | std::basic_string<charT> d1(to_simple_string_type<charT>(tp.begin()));
|
---|
| 194 | std::basic_string<charT> d2(to_simple_string_type<charT>(tp.last()));
|
---|
| 195 | return std::basic_string<charT>(beg + d1 + mid + d2 + end);
|
---|
| 196 | }
|
---|
| 197 | //! Convert to string of form [YYYY-mmm-DD HH:MM::SS.ffffff/YYYY-mmm-DD HH:MM::SS.fffffff]
|
---|
| 198 | /*!\ingroup time_format
|
---|
| 199 | */
|
---|
| 200 | inline std::string to_simple_string(time_period tp){
|
---|
| 201 | return to_simple_string_type<char>(tp);
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | // function called by wrapper functions to_*_string(time_period)
|
---|
| 205 | // & to_*_wstring(time_period)
|
---|
| 206 | template<class charT>
|
---|
| 207 | inline std::basic_string<charT> to_iso_string_type(ptime t)
|
---|
| 208 | {
|
---|
| 209 | std::basic_string<charT> ts = gregorian::to_iso_string_type<charT>(t.date());// + "T";
|
---|
| 210 | if(!t.time_of_day().is_special()) {
|
---|
| 211 | charT sep = 'T';
|
---|
| 212 | return ts + sep + to_iso_string_type<charT>(t.time_of_day());
|
---|
| 213 | }
|
---|
| 214 | else {
|
---|
| 215 | return ts;
|
---|
| 216 | }
|
---|
| 217 | }
|
---|
| 218 | //! Convert iso short form YYYYMMDDTHHMMSS where T is the date-time separator
|
---|
| 219 | /*!\ingroup time_format
|
---|
| 220 | */
|
---|
| 221 | inline std::string to_iso_string(ptime t){
|
---|
| 222 | return to_iso_string_type<char>(t);
|
---|
| 223 | }
|
---|
| 224 |
|
---|
| 225 |
|
---|
| 226 | // function called by wrapper functions to_*_string(time_period)
|
---|
| 227 | // & to_*_wstring(time_period)
|
---|
| 228 | template<class charT>
|
---|
| 229 | inline std::basic_string<charT> to_iso_extended_string_type(ptime t)
|
---|
| 230 | {
|
---|
| 231 | std::basic_string<charT> ts = gregorian::to_iso_extended_string_type<charT>(t.date());// + "T";
|
---|
| 232 | if(!t.time_of_day().is_special()) {
|
---|
| 233 | charT sep = 'T';
|
---|
| 234 | return ts + sep + to_simple_string_type<charT>(t.time_of_day());
|
---|
| 235 | }
|
---|
| 236 | else {
|
---|
| 237 | return ts;
|
---|
| 238 | }
|
---|
| 239 | }
|
---|
| 240 | //! Convert to form YYYY-MM-DDTHH:MM:SS where T is the date-time separator
|
---|
| 241 | /*!\ingroup time_format
|
---|
| 242 | */
|
---|
| 243 | inline std::string to_iso_extended_string(ptime t){
|
---|
| 244 | return to_iso_extended_string_type<char>(t);
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | #if !defined(BOOST_NO_STD_WSTRING)
|
---|
| 248 | //! Time duration to wstring -hh::mm::ss.fffffff. Example: 10:09:03.0123456
|
---|
| 249 | /*!\ingroup time_format
|
---|
| 250 | */
|
---|
| 251 | inline std::wstring to_simple_wstring(time_duration td) {
|
---|
| 252 | return to_simple_string_type<wchar_t>(td);
|
---|
| 253 | }
|
---|
| 254 | //! Time duration in iso format -hhmmss,fffffff Example: 10:09:03,0123456
|
---|
| 255 | /*!\ingroup time_format
|
---|
| 256 | */
|
---|
| 257 | inline std::wstring to_iso_wstring(time_duration td){
|
---|
| 258 | return to_iso_string_type<wchar_t>(td);
|
---|
| 259 | }
|
---|
| 260 | inline std::wstring to_simple_wstring(ptime t){
|
---|
| 261 | return to_simple_string_type<wchar_t>(t);
|
---|
| 262 | }
|
---|
| 263 | //! Convert to wstring of form [YYYY-mmm-DD HH:MM::SS.ffffff/YYYY-mmm-DD HH:MM::SS.fffffff]
|
---|
| 264 | /*!\ingroup time_format
|
---|
| 265 | */
|
---|
| 266 | inline std::wstring to_simple_wstring(time_period tp){
|
---|
| 267 | return to_simple_string_type<wchar_t>(tp);
|
---|
| 268 | }
|
---|
| 269 | //! Convert iso short form YYYYMMDDTHHMMSS where T is the date-time separator
|
---|
| 270 | /*!\ingroup time_format
|
---|
| 271 | */
|
---|
| 272 | inline std::wstring to_iso_wstring(ptime t){
|
---|
| 273 | return to_iso_string_type<wchar_t>(t);
|
---|
| 274 | }
|
---|
| 275 | //! Convert to form YYYY-MM-DDTHH:MM:SS where T is the date-time separator
|
---|
| 276 | /*!\ingroup time_format
|
---|
| 277 | */
|
---|
| 278 | inline std::wstring to_iso_extended_wstring(ptime t){
|
---|
| 279 | return to_iso_extended_string_type<wchar_t>(t);
|
---|
| 280 | }
|
---|
| 281 |
|
---|
| 282 | #endif // BOOST_NO_STD_WSTRING
|
---|
| 283 |
|
---|
| 284 |
|
---|
| 285 | } } //namespace posix_time
|
---|
| 286 |
|
---|
| 287 |
|
---|
| 288 | #endif
|
---|
| 289 |
|
---|