ошибка ptime input_facet с флагом% f при преобразовании строки в ptime - PullRequest
2 голосов
/ 30 июня 2011

У меня проблемы с чтением доли времени при преобразовании строки в ptime.Вот соответствующий код соучастия:

#include <iostream>
#include <string>
#include <boost/date_time.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/conversion.hpp>

int main( int, char * )
{
   using namespace std;
   using namespace boost;
   using namespace boost::posix_time;
   using namespace boost::local_time;
   using namespace boost::gregorian;

   time_facet*       output_facet = new time_facet();
   time_input_facet* input_facet  = new time_input_facet();

   stringstream sstream;
   sstream.imbue(locale(locale::classic(), output_facet));
   sstream.imbue(locale(sstream.getloc() , input_facet ));

   string format("%d/%m/%Y %H:%M:%S.%f"); //example works fine without %f!!!
   //format = "%H"; //-> works
   //format = "%f"; //-> error
   output_facet->format( format.c_str() );
   input_facet->format( format.c_str() );

   ptime dt = microsec_clock().local_time();

   sstream.str("");
   sstream << dt;
   cout << sstream.str() << endl;
   string time_string = sstream.str();

   ptime tgtDt( boost::date_time::not_a_date_time );
   sstream.str( time_string.c_str() );
   sstream >> tgtDt;
   cout << tgtDt << endl;
}

Программа работает нормально без% f внутри строки формата.Но с% f tgtDt это "не дата".Я использую boost 1.44, и документ говорит, что% f shold работает, потому что каждый флаг формата несовместимого ввода помечен знаком "!"в документации.

Есть идеи, как вытащить часть трещины из строки?

1 Ответ

1 голос
/ 01 июля 2011

Хорошо, разобрался (знал, что это как-то связано с точкой!) .. Входной и выходной фасеты ожидают разных форматов. Попробуйте следующее:

string inputformat("%d/%m/%Y %H:%M:%S%f"); //no dot before %f
string format("%d/%m/%Y %H:%M:%S.%f"); //dot before %f
output_facet->format( format.c_str() );
input_facet->format( inputformat.c_str() );
...