C++ 将字符串解析为boost::posix_time::ptime(毫秒)

C++ 将字符串解析为boost::posix_time::ptime(毫秒),c++,boost,C++,Boost,我有一组函数,允许我将ptime转换为字符串,并根据特定格式将字符串转换为ptime。 在我需要使用毫秒修饰符(%f)之前,这项工作正常 转换为字符串的操作正常: std::string strFormat = "%Y-%m-%d %H:%M:%S.%f"; ptime now = microsec_clock::universal_time(); auto str = ToString(strFormat, now); 将输出:2020-08-26 12:27:54.93

我有一组函数,允许我将ptime转换为字符串,并根据特定格式将字符串转换为ptime。 在我需要使用毫秒修饰符(%f)之前,这项工作正常

转换为字符串的操作正常:

std::string strFormat = "%Y-%m-%d %H:%M:%S.%f";
ptime now = microsec_clock::universal_time();
auto str = ToString(strFormat, now);
将输出:2020-08-26 12:27:54.938943

但恰恰相反:

auto pt = FromString(strFormat, str);
std::cout << to_simple_string(pt) << std::endl;
如您所见,删除%f修改器工作正常

我错过了什么?我尝试过不同的格式,但没有成功。使用boost1.70


编辑:正如@sugar在评论中指定的那样,使用%F而不是%F似乎是双向的。%f不应该被使用吗?

事实证明这是Boost date\u time库()

为了解决这个问题,我只需将所有出现的“%f”替换为“%f”:


看起来boost文档不正确,
%f
说明符无效。改为使用
%F
。实际上,使用%Y-%m-%d%H:%m:%S%F可以双向工作。但奇怪的是%f以一种方式工作,而不是以另一种方式工作。在这个问题上发现了一个问题:%f在时间面和时间面之间确实不一致
boost::posix_time::ptime ptime;
boost::posix_time::time_input_facet* infacet = new boost::posix_time::time_input_facet(informat.c_str());

std::stringstream ss;
ss.imbue(std::locale(ss.getloc(), infacet));

ss.str(time);
ss >> ptime;

return ptime;
boost::posix_time::ptime FromString(std::string informat, std::string time)
            {
                boost::replace_all(informat, ".%f", "%F"); // Get around #102
                boost::posix_time::ptime ptime;
                boost::posix_time::time_input_facet* infacet = new boost::posix_time::time_input_facet(informat.c_str());

                std::stringstream ss;
                ss.imbue(std::locale(ss.getloc(), infacet));

                ss.str(time);
                ss >> ptime;
    
                return ptime;
            }