Я пытаюсь преобразовать boost::posix_time::ptime
в определенный формат строки (расширенный ISO), а затем обратно в boost::posix_time::ptime
.
Удивительно, но похоже, что boost::posix_time::time_facet
%f
означает от 000000
до 999999
(без десятичного разделителя). Но для boost::posix_time::time_input_facet
это означает от .000000
до .999999
(с десятичным разделителем).
См:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <iostream>
int main( int argc, char** argv )
{
auto now = boost::posix_time::second_clock::local_time();
std::stringstream outStr;
{
boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
facet->format("%Y-%m-%dT%H:%M:%S.%f");
outStr.imbue(std::locale(std::locale::classic(), facet));
outStr << now;
}
std::cout << outStr.str() << std::endl;
{
static const std::string format = "%Y-%m-%dT%H:%M:%S.%f";
const std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(format));
std::istringstream is(outStr.str());
is.imbue(loc);
boost::posix_time::ptime converted;
is >> converted;
std::cout << converted << std::endl;
}
{
static const std::string format = "%Y-%m-%dT%H:%M:%S%f";
const std::locale loc = std::locale(std::locale::classic(), new boost::posix_time::time_input_facet(format));
std::istringstream is(outStr.str());
is.imbue(loc);
boost::posix_time::ptime converted;
is >> converted;
std::cout << converted << std::endl;
}
return 0;
}
Это выводит :
2019-04-30T12:23:29.000000
not-a-date-time
2019-Apr-30 12:23:29
Пока я бы ожидал:
2019-04-30T12:23:29.000000
2019-Apr-30 12:23:29
not-a-date-time
Я использую Boost 1.68.
Я что-то не так делаю или это ошибка в boost::posix_time::time_facet
или boost::posix_time::input_time_facet
?
Примечание: у "% F" нет этой проблемы.