Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:45:13

0001 #ifndef POSIXTIME_FORMATTERS_HPP___
0002 #define POSIXTIME_FORMATTERS_HPP___
0003 
0004 /* Copyright (c) 2002-2004 CrystalClear Software, Inc.
0005  * Use, modification and distribution is subject to the
0006  * Boost Software License, Version 1.0. (See accompanying
0007  * file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt)
0008  * Author: Jeff Garland, Bart Garst
0009  * $Date$
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 /* NOTE: The "to_*_string" code for older compilers, ones that define
0022  * BOOST_DATE_TIME_INCLUDE_LIMITED_HEADERS, is located in
0023  * formatters_limited.hpp
0024  */
0025 
0026 namespace boost {
0027 
0028 namespace posix_time {
0029 
0030   // template function called by wrapper functions:
0031   // to_*_string(time_duration) & to_*_wstring(time_duration)
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       /* simply using 'ss << td.get_rep()' won't work on compilers
0037        * that don't support locales. This way does. */
0038       // switch copied from date_names_put.hpp
0039       switch(td.get_rep().as_special())
0040       {
0041       case not_a_date_time:
0042         //ss << "not-a-number";
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       //TODO the following is totally non-generic, yelling FIXME
0067 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0068       boost::int64_t frac_sec =
0069         date_time::absolute_value(td.fractional_seconds());
0070       // JDG [7/6/02 VC++ compatibility]
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           // JDG [7/6/02 VC++ compatibility]
0082 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0083             << buff;
0084 #else
0085         << frac_sec;
0086 #endif
0087       }
0088     }// else
0089     return ss.str();
0090   }
0091   //! Time duration to string -hh::mm::ss.fffffff. Example: 10:09:03.0123456
0092   /*!\ingroup time_format
0093    */
0094   inline std::string to_simple_string(time_duration td) {
0095     return to_simple_string_type<char>(td);
0096   }
0097 
0098 
0099   // template function called by wrapper functions:
0100   // to_*_string(time_duration) & to_*_wstring(time_duration)
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       /* simply using 'ss << td.get_rep()' won't work on compilers
0107        * that don't support locales. This way does. */
0108       // switch copied from date_names_put.hpp
0109       switch(td.get_rep().as_special()) {
0110       case not_a_date_time:
0111         //ss << "not-a-number";
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       //TODO the following is totally non-generic, yelling FIXME
0136 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0137       boost::int64_t frac_sec =
0138         date_time::absolute_value(td.fractional_seconds());
0139       // JDG [7/6/02 VC++ compatibility]
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           // JDG [7/6/02 VC++ compatibility]
0151 #if (defined(BOOST_MSVC) && (_MSC_VER < 1300))
0152             << buff;
0153 #else
0154         << frac_sec;
0155 #endif
0156       }
0157     }// else
0158     return ss.str();
0159   }
0160   //! Time duration in ISO 8601 format -hhmmss.fffffff. Example: 10:09:03.0123456
0161   /*!\ingroup time_format
0162    */
0163   inline std::string to_iso_string(time_duration td){
0164     return to_iso_string_type<char>(td);
0165   }
0166 
0167   //! Time to simple format CCYY-mmm-dd hh:mm:ss.fffffff
0168   /*!\ingroup time_format
0169    */
0170   template<class charT>
0171   inline std::basic_string<charT> to_simple_string_type(ptime t)
0172   {
0173     // can't use this w/gcc295, no to_simple_string_type<>(td) available
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   // function called by wrapper functions to_*_string(time_period)
0188   // & to_*_wstring(time_period)
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   //! Convert to string of form [YYYY-mmm-DD HH:MM::SS.ffffff/YYYY-mmm-DD HH:MM::SS.fffffff]
0198   /*!\ingroup time_format
0199    */
0200   inline std::string to_simple_string(time_period tp){
0201     return to_simple_string_type<char>(tp);
0202   }
0203 
0204   // function called by wrapper functions to_*_string(time_period)
0205   // & to_*_wstring(time_period)
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());// + "T";
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   //! Convert ISO 8601 short form YYYYMMDDTHHMMSS where T is the date-time separator
0219   /*!\ingroup time_format
0220    */
0221   inline std::string to_iso_string(ptime t){
0222     return to_iso_string_type<char>(t);
0223   }
0224 
0225 
0226   // function called by wrapper functions to_*_string(time_period)
0227   // & to_*_wstring(time_period)
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());// + "T";
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   //! Convert to form YYYY-MM-DDTHH:MM:SS where T is the date-time separator
0241   /*!\ingroup time_format
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   //! Time duration to wstring -hh::mm::ss.fffffff. Example: 10:09:03.0123456
0249   /*!\ingroup time_format
0250    */
0251   inline std::wstring to_simple_wstring(time_duration td) {
0252     return to_simple_string_type<wchar_t>(td);
0253   }
0254   //! Time duration in ISO 8601 format -hhmmss.fffffff. Example: 10:09:03.0123456
0255   /*!\ingroup time_format
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   //! Convert to wstring of form [YYYY-mmm-DD HH:MM::SS.ffffff/YYYY-mmm-DD HH:MM::SS.fffffff]
0264   /*!\ingroup time_format
0265    */
0266   inline std::wstring to_simple_wstring(time_period tp){
0267     return to_simple_string_type<wchar_t>(tp);
0268   }
0269   //! Convert ISO 8601 short form YYYYMMDDTHHMMSS where T is the date-time separator
0270   /*!\ingroup time_format
0271    */
0272   inline std::wstring to_iso_wstring(ptime t){
0273     return to_iso_string_type<wchar_t>(t);
0274   }
0275   //! Convert to form YYYY-MM-DDTHH:MM:SS where T is the date-time separator
0276   /*!\ingroup time_format
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 // BOOST_NO_STD_WSTRING
0283 
0284 
0285 } } //namespace posix_time
0286 
0287 
0288 #endif
0289